Group boxes and own widget not having correct size.



  • I am trying to understand why two group boxes from a legacy Qt 4.8.6 application that I am working on do not show as expected. I've tried to trimm down the problem, and my trimmed down code is in the public gist online at https://gist.github.com/BartVandewoestyne/fc89f94212644bfa7016

    My main looks as follows:

    int main(int argc, char* argv[])
    {
        QApplication app(argc, argv);
        MyOuterWidget outerWidget;
        outerWidget.show();
        return app.exec();
    }
    

    The OuterWidget is created as follows:

    MyOuterWidget::MyOuterWidget(
        QWidget* parent
    ) : QWidget(parent)
    {
        outerGroupBox = new QGroupBox("My Outer GroupBox", this);
        myInnerWidget = new MyInnerWidget();
        myButton = new QPushButton("Some text.");
    
        QVBoxLayout* vboxLayout = new QVBoxLayout(outerGroupBox);
        vboxLayout->addWidget(myInnerWidget);
        vboxLayout->addWidget(myButton);
    }
    

    The problem with this outer widget is that is does not follow the resizings of the main application window. It just sits there with its fixed size.

    The inner widget looks as follows:

    MyInnerWidget::MyInnerWidget(
        QWidget* parent
    ) : QWidget(parent)
    {
        innerGroupBox = new QGroupBox("InnerGroupBox", this);
        QGridLayout* gridLayout = new QGridLayout(innerGroupBox);
    
        helloALabel = new QLabel("Hello A");
        helloBLabel = new QLabel("Hello B");
        helloALineEdit = new QLineEdit("");
        helloBLineEdit = new QLineEdit("");
        worldALabel = new QLabel("World A");
        worldBLabel = new QLabel("World B");
    
        gridLayout->addWidget(helloALabel, 0, 0);
        gridLayout->addWidget(helloALineEdit, 0, 1);
        gridLayout->addWidget(worldALabel, 0, 2);
        gridLayout->addWidget(helloBLabel, 0, 3);
        gridLayout->addWidget(helloBLineEdit, 0, 4);
        gridLayout->addWidget(worldBLabel, 0, 5);
    
        // Without this line, this widget is not visible at all :-(
        setMinimumSize(400, 200);
    }
    

    The problem with this inner widget is the following: first of all, without the setMinimumSize statement, the widget is not visible at all. How can I make it visible without setting it to a fixed size? Secondly, this inner widget is 'cut off' on the right side and there is too much space below the inner widget and the button below it. How do I make it nicely fit into the outer widget?

    So to summerize, these are my main questions:

    1. How do I make the outer widget follow the resizings of the main window instead of sitting there with a fixed size?
    2. How do I make the inner widget nicely fit into the outer widget (and automagically reduce the space between the bottom of the inner widget and the button below it)?

    See also my public Gist online at https://gist.github.com/BartVandewoestyne/fc89f94212644bfa7016

    Thanks for your feedback,
    Bart


  • Qt Champions 2016

    Hi
    In main you create MyOuterWidget with no parent.
    This means that it will become the window.
    So there is no other main window, it -is- the window.
    so it just use its default size.
    If u resize it, it will be bigger.

    
    int main(int argc, char* argv[])
    {
        QApplication app(argc, argv);
    
        // QUESTION 1: Why doesn't the outer group box follow the resizings of the main window?
        // It always stays there, having the same size.
        MyOuterWidget outerWidget;
        outerWidget.resize(500,500); // use other size than default size
    
        outerWidget.show();
    
        return app.exec();
    }
    
    

    and +1 for code and good write up.
    So if you used a mainwindow/any other widget as parent,
    you would add layout to it and then add MyOuterWidget to this layout
    Then it would follow the window/dialog/otherwidget.
    Right now it has nothing to follow :)


  • Qt Champions 2016

    @Bart_Vandewoestyne
    To add to what @mrjj noted, your outerGroupBox is not added to a layout (your outer widget doesn't have a layout), so it will not resize automatically. Similarly, your inner widget doesn't have a layout, so it doesn't know how big it should be (this is probably why it's not visible without setting a minimum size).



  • @kshegunov said:

    @Bart_Vandewoestyne
    [...], your outerGroupBox is not added to a layout (your outer widget doesn't have a layout), so it will not resize automatically. [...]

    That's something I don't quite understand. In my constructor for MyOuterWidget, I create the groupbox as follows:

    outerGroupBox = new QGroupBox("My Outer GroupBox", this);
    

    so the groupbox has the MyOuterWidget as parent. Then, further in the same constructor, I give the outerGroupBox a QVBoxLayout using

    QVBoxLayout* vboxLayout = new QVBoxLayout(outerGroupBox);
    

    so from this I would say that the outerGroupBox does have a layout. What am I misunderstanding here?


  • Qt Champions 2016

    Yes, your outerGroupBox has a layout, but is not added to the parent's layout (because you don't create it). Your group box is a widget, to have it resized automatically, you have to add it to a layout. Giving it a parent is just not enough, this only ensures that when the parent is destroyed the child will be destroyed as well. You should create a layout for your outer widget and then add the group box to it, e.g.:

    MyOuterWidget::MyOuterWidget(QWidget * parent)
        : QWidget(parent)
    {
        ...
        // Create a layout for MyOuterWidget
        QVBoxLayout * layout = new QVBoxLayout(this);
        // Add the group box to that layout
        layout->addWidget(outerGroupBox);
        ...
    }
    

    The same goes for your inner widget.



  • @kshegunov said:

    Yes, your outerGroupBox has a layout, but is not added to the parent's layout (because you don't create it). Your group box is a widget, to have it resized automatically, you have to add it to a layout. Giving it a parent is just not enough, this only ensures that when the parent is destroyed the child will be destroyed as well. You should create a layout for your outer widget and then add the group box to it [...]

    OK, thanks! The mistake I made was indeed that I assumed that if the outerGroupBox has MyOuterWidget as a parent, it would automatically be resized. I now realize that I have to create a main layout for MyOuterWidget and add my QGroupBox to that. I see you used a QVBoxLayout, but if I understand things correctly, it could've as well been a QHBoxLayout or QGridLayout because there's only one widget added?


  • Qt Champions 2016

    @Bart_Vandewoestyne

    I see you used a QVBoxLayout, but if I understand things correctly, it could've as well been a QHBoxLayout or QGridLayout because there's only one widget added?

    Yes of course, you can use whatever layout suits your needs. Also there is no restriction that you have to put one single widget in it, it can as easily accomodate many child widgets. QMainWindow is bit different in that respect, as it doesn't have a layout itself, but instead relies on a central widget (the client area) to do the laying out. I hope that clears things. :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.