Results 1 to 3 of 3

Thread: Displaying large amount of widgets in different styles

  1. #1
    Join Date
    Sep 2011
    Posts
    20
    Qt products
    Qt5
    Platforms
    Windows

    Default Displaying large amount of widgets in different styles

    I'm working on "yet another" media manager for movies. So I have a list of movie data in some data structure, including cover images. The user has the option to display these movie items (widgets) in different modes, e.g. "cover only", "cover + title" or "cover + title + year" and so forth. So the first problem is what kind of pattern would I use for easily adding new display modes? Would it be something like this (not tested or anything, just to give an idea of what I've thought):

    Qt Code:
    1. typedef QMap<QString, QString> MovieData;
    2.  
    3. QWidget *coverAndTitle(const MovieData &data)
    4. {
    5. QWidget *w = new QWidget;
    6. // Build the widget out of data so that it contains title and cover image
    7. return w;
    8. }
    9.  
    10. QWidget *coverOnly(const MovieData &data)
    11. {
    12. QWidget *w = new QWidget;
    13. // Build the widget out of data so that it contains only cover
    14. return w;
    15. }
    16.  
    17. QList<MovieData> movieDataList;
    18. enum { DisplayMode_Cover, DisplayMode_CoverAndTitle };
    19.  
    20. // ...
    21.  
    22. for(int i = 0; i < movieDataList.size(); ++i)
    23. {
    24. MovieData md = movieDataList.at(i);
    25.  
    26. QWidget *w = nullptr;
    27. if(displayMode == DisplayMode_CoverAndTitle)
    28. {
    29. w = coverAndTitle(md);
    30. }
    31. else if(displayMode == DisplayMode_Cover)
    32. {
    33. w = coverOnly(md);
    34. }
    35.  
    36. // proceed to add this widget to some container where it's displayed
    37. }
    38. }
    To copy to clipboard, switch view to plain text mode 

    So that's a pretty simplistic solution (if it even works). Would appreciate some feedback on this solution and on better solutions.

    Then another problem is if my widgets e.g. take 100x150 pixels of space + 15px margin. So that would probably only display cover image for every movie. I'd want them to behave like this in the container. Drag the vertical bar sideways and you'll see how the yellow boxes behave as the space changes. How would I go about implementing it this way when the user resizes the window the widgets would behave similarly?

    Thanks!

  2. #2
    Join Date
    Sep 2011
    Posts
    20
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Displaying large amount of widgets in different styles

    I solved the second problem with FlowLayout from Qt site: http://developer.qt.nokia.com/doc/qt...lowlayout.html

    But how can I get the widget called root to expand in this code below?

    Qt Code:
    1. QScrollArea *content = new QScrollArea(this);
    2.  
    3. FlowLayout *flowLayout = new FlowLayout;
    4.  
    5. for(int i = 0; i < 20; ++i)
    6. {
    7. QWidget *w = new QWidget;
    8. w->setFixedSize(100, 150);
    9. w->setStyleSheet("margin: 15px; background-color: #000000");
    10.  
    11. flowLayout->addWidget(w);
    12. }
    13.  
    14. QWidget *root = new QWidget;
    15. root->setStyleSheet("background-color: #ffff66");
    16. root->setLayout(flowLayout);
    17.  
    18. content->setStyleSheet("background-color: #ff6600");
    19. content->setWidget(root);
    20.  
    21. setCentralWidget(content);
    To copy to clipboard, switch view to plain text mode 

    QScrollArea *content is orange
    QWidget *root is yellow



    If I remove the root QWidget and set flowLayout as a direct child of the content QScrollArea, then it works fine, but there's no scrollbar even if I force it.

    EDIT: I re-implemented QMainWindow's resizeEvent() and the QWidget *root is resized every time that event takes place. Not sure if that's a good solution?

    EDIT2: Well that didn't work too well. I should somehow connect the widget's size to the flowLayout probably.

    EDIT3: This solution works:

    Qt Code:
    1. void MainWindow::resizeEvent(QResizeEvent *ev)
    2. {
    3. int cWidth = content->viewport()->width();
    4. int lNeededHeight = flowLayout->minimumHeightForWidth(cWidth);
    5.  
    6. root->setFixedWidth(cWidth);
    7. root->setFixedHeight(lNeededHeight);
    8. }
    To copy to clipboard, switch view to plain text mode 

    Any feedback on this approach? Now at leas thet root QWidget resizes itself properly:

    Last edited by themagician; 3rd January 2012 at 03:12.

  3. #3
    Join Date
    Sep 2011
    Posts
    20
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Displaying large amount of widgets in different styles

    That was not a good solution! All I had to do was call QScrollArea::setWidgetResizeable(true); to make the root QWidget fill the QScrollArea.

    Still have to tackle with the first problem: How to make, e.g. in the above screenshots, the black boxes change their appearance when the user e.g. selects from the menu View -> Blue Boxes, Double-size and it would change the appearance of the black boxes to "blue and double the size".

    Should I:

    1) Keep the movie data (see OP) in a simple data structure and construct new QWidgets every time the user requests their appearance to change via some factory pattern that returns whatever template (as in layout) generator function/object that takes the movie data as an argument and spits the desired QWidgets.
    or 2) Sub-class QWidgets that have methods for changing their own appearance, perhaps combined with the above pattern suggestion, that are called when the user wants to use another layout for the movie items
    or 3) Something else?

    Thanks again.

Similar Threads

  1. tree view displaying widgets as items
    By paksas in forum Qt Programming
    Replies: 9
    Last Post: 28th January 2013, 13:58
  2. Replies: 1
    Last Post: 12th May 2011, 22:41
  3. Replies: 4
    Last Post: 18th August 2009, 19:53
  4. QScrollArea not displaying large number of images
    By lpkincaid in forum Qt Programming
    Replies: 1
    Last Post: 31st May 2009, 09:58
  5. Sending large datagrams(very large)
    By marcel in forum General Programming
    Replies: 1
    Last Post: 16th February 2008, 21:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.