Results 1 to 3 of 3

Thread: Force all widgets in a QGridLayout to be visible

  1. #1
    Join Date
    Jan 2014
    Posts
    36
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Force all widgets in a QGridLayout to be visible

    I feel like this should be a simple layout question, but I'm having trouble getting it to work...

    I've got a custom widget that inherits from QWidget and has a QGridLayout member that is set on this widget itself
    Qt Code:
    1. myContainer::myContainer(QWidget* parent):QWidget(parent)
    2. {
    3. m_layout = new QGridLayout;
    4. setLayout(m_layout);
    5. }
    To copy to clipboard, switch view to plain text mode 
    I've got two types of widgets that go into the grid, so the grid is going to be an (n,2) grid (n rows, 2 columns). As the application is resized, I want the height of the widgets in my grid to be roughly (1/N * height of myContainer). So basically I want the application's layouts to figure out what how many pixels to give myContainer, and once that's figured out, then resize all the rows so that all the rows are visible. Instead it seems to be working from the inside out, where the items in the rows are getting some default height, which then pushes the height of myContainer to be N * that height, which then makes the application taller than the height of an actual monitor which means some of the rows aren't visible.

    I've tried setting the vertical size policies of all my widgets to QSizePolicy::Maximum, but that doesn't seem to get it to work correctly.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Force all widgets in a QGridLayout to be visible

    You are describing the default behaviour of the grid layout if all the widgets in it have no minimum size constraint and the same stretch factors (see example). Perhaps you need to tell us what the widget are, how you are adding them etc.
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Container: public QWidget {
    4. public:
    5. explicit Container(QWidget *p = 0): QWidget(p) {
    6. QGridLayout *l = new QGridLayout(this);
    7. for (int r = 0; r < 5; ++r) {
    8. for (int c = 0; c < 5; ++c) {
    9. QFrame *frame = new QFrame(this);
    10. frame->setFrameStyle(QFrame::Box | QFrame::Plain);
    11. l->addWidget(frame, r, c);
    12. }
    13. }
    14. }
    15. };
    16.  
    17. int main(int argc, char **argv) {
    18. QApplication app(argc, argv);
    19. Container c;
    20. c.resize(640, 480);
    21. c.show();
    22. Container b;
    23. b.resize(800, 600);
    24. b.show();
    25. return app.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisW67; 29th January 2014 at 21:34.

  3. #3
    Join Date
    Jan 2014
    Posts
    36
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Force all widgets in a QGridLayout to be visible

    I've removed my container widget, which wasn't really doing anything beyond wrapping a QGridLayout inside a widget anyways.

    So now, the parent widget is a QTabWidget, my two widgets that I'm putting in the grid are a QwtPlot (col 0) and a QwtLegend (col 1). I've included the relevant code below.
    Qt Code:
    1. QGridLayout* grid = new QGridLayout();
    2. ui->tab_2->setLayout(grid);
    3.  
    4. int i=0;
    5. foreach (QwtPlot* plot, plots)
    6. {
    7. plot->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Maximum);
    8. grid->addWidget(plot,i,0);
    9. QwtLegend* legend = new QwtLegend();
    10. legend->setMaxColumns(1);
    11. legend->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
    12. grid->addWidget(legend, i, 1);
    13. i++;
    14. }
    To copy to clipboard, switch view to plain text mode 

    This code is in my MainWindow constructor, and when the application launches, it takes up more vertical space than I'd expect it to. In the Qt Creator Designer tab, my MainWindow class has a geometry of 1158x738. When the application launches, the MainWindow size is 1158 x 1997...

Similar Threads

  1. QGridLayout force column creation
    By Teermit in forum Qt Programming
    Replies: 2
    Last Post: 19th February 2013, 20:36
  2. QGridLayout: force all widget to the same size
    By Spooky in forum Qt Programming
    Replies: 2
    Last Post: 23rd March 2011, 13:58
  3. Replies: 2
    Last Post: 16th December 2010, 11:52
  4. Widgets in QGridLayout placed at top left
    By anarion in forum Newbie
    Replies: 2
    Last Post: 2nd September 2010, 15:06
  5. how to force widgets update
    By Onanymous in forum Newbie
    Replies: 5
    Last Post: 14th June 2010, 17:37

Tags for this Thread

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.