Important: Please read the Qt Code of Conduct -

Resize window and grow subwidget

  • Hi

    I have a window which has three sub sections. ListView is at center widget and there are two dockWidget on left and right of ListView as you can see in A . When I resize window by dragging right side, ListView grows as B. My goal is to let Dock2 grow while Dock1 and ListView do not change as C. Is there simple way to do that?

  • Lifetime Qt Champion

    @samdol Did you try to change the sizePolicy property?

  • use QWidget::setSizePolicy set it to QSizePolicy::Expanding for the widgets you want to grow and QSizePolicy::Preferred for the rest. in your case listView->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Expanding); dock2->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

  • @VRonin
    That works well if those three widgets are like groupBoxes. But if the widget on left or right is the dockWidget, it did not work as expected. But interestingly, dockWidget also has setSizePolicy() functions. Would it possibly be a bug in Qt?

  • if listView is the central widget, try setting listView->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Expanding); after you made it a central widget

  • @VRonin
    It works partially. When I drag righthand side, initially listView grows while docks does not grow. After listView reaches the certain size, it does not grow while docks grow. Here is the code I tried,

    MainWindow::MainWindow(QWidget *parent) :
        this->resize(300, 300);
        groupBox_main = new QGroupBox(this);
        QVBoxLayout *vlayout = new QVBoxLayout;
        list_view = new QListView;
        dockWidget_1 = new QDockWidget(this);
        addDockWidget(Qt::LeftDockWidgetArea, dockWidget_1);
        dockWidget_2 = new QDockWidget(this);
        this->addDockWidget(Qt::RightDockWidgetArea, dockWidget_2);

    Interesting thing is that if I don't include vlayout->addWidget(list_view); listview never grows while I drag the righthand side which is what I wanted.

  • @samdol said in Resize window and grow subwidget:

    After listView reaches the certain size

    That certain size is listView->sizeHint() you can call listView->setSizeHint(/*QSize*/) to decide how much it should be

  • @VRonin
    Sorry, I can see QListView::sizeHint(), but I don't see the method QListView::setSizeHint(). It is quite strange.

  • Sorry, my bad, use setMaximumSize instead

  • @VRonin
    I also tried that. But setMaximumSize limits the size of listView. I don't want to limit the size of listView. For example, I intend that if I drag handler between listView and dock2, listView can grow wihout limit while dragging right hand side only makes dock2 grow.

  • ok then just use setSizePolicy.

    use QSizePolicy::Preferred for the listView and QSizePolicy::Maximum for the left dock

  • @VRonin
    That also makes no difference. listView grows upto sizeHint and then docks start grow. It is quite weird that listView and groupBox have sizeHint() but they don't have setSizeHint(). Then where those initial sizeHint values come from? Why they don't allow users to change it?

  • you can still subclass QListView and reimplement sizeHint() to return what you prefer

Log in to reply