Important: Please read the Qt Code of Conduct -

Set QWidget default width

  • I need to set the initial width of a qwidget, which should be expandible horizontally, being inserted in a QSplitter, inside the parent widget constructor.
    I've tried to use rightPanel->resize(rightPanel->sizeHint()); but nothing changes
    I need to set a specific smaller widht.
    I find no way to do this. When parent widget shows the widget i'm intereset in is has a huge width much larger than its content.
    This is a problem I 've encountered in many different and more general situations NOT strictly related to qsplitter.

    ParentMdiSubWindow::ParentMdiSubWindow(): QMdiSubWindow()
        QSplitter *midSplitter = new QSplitter(Qt::Horizontal);
        // plot on the left
        leftPanel = new QWidget();
        leftPanelLayout = new QVBoxLayout();
        // right panel
        rightPanel = new QWidget();
        rightPanelLayout = new QVBoxLayout();
        // add support widget to subwindow

    right panel issue.png

  • Lifetime Qt Champion


    What about using QSplitter::setSizes ?

  • @SGaist thank you but I don't know the main widget size - a qdmisubwindow in this case - and I need the plot on the left to simply expand as much as it can. only the right panel should have an initial precise width. it's a situation very common which can be managed in many platform and GUI I've used so far. A simple command for setting a specific size thou not fixed would be very helpful

  • Lifetime Qt Champion

    You can also use setStretchFactor.

  • As workaround I tried, but when I maximize the QMdiSubWindow the stretch factor is different. I'd need at least the ratio is kept but it's not.

  • @Black-Imp
    This is purely thrown in as thought for you: given you say it does not work inside parent widget constructor, could you set it in whatever "show" event instead when it's first shown and maybe your code will work there? It may be quite unrelated to your situation, but I have had problems resizing when containers & children have not yet been made visible.

  • @JonB Thank you that's something I was searching for. It works. Unfortunately everytime that system calls qmdisubsindow's showEvent, size is applied again. I should intercept every call for minimizing, maximizing, tiling etc. for saving the current size of right panel and setting it again in showEvent.

    for those who may be interested in:

    void ParentMdiSubWindow::showEvent(QShowEvent * event)
        int w = midSplitter->width();
        // screeSize is taken from primary screen in constructor
        int labelPanelSize = screenSize.width() / 192 * 18; 
        int width = this->width();
        if (labelPanelSize >= width)
        { labelPanelSize = width / 3; }
        QList<int> list;
        list << w - labelPanelSize;
        list << labelPanelSize;

  • ok I came up with the complete solution:

    instead of overriding showEvent i override resizeEvent which is called before the showEvent.

    This whole code lets me hold the right panel width manually set by handle when resizing, maximizing, minimizing parent window, starting with a width based on primary screen

    class ParentMdiSubWindow: public QMdiSubWindow
       protected slots:
          void splitterHandleMoved(int pos, int index);
          bool inited; // initialized false in constructor
          int labelPanelWidth; // initialized 0 in constructor 
          QSplitter * midSplitter;
    ParentMdiSubWindow::ParentMdiSubWindow(QWidget *parent, Qt::WindowFlags flags):QMdiSubWindow(parent, flags),
       midSplitter = new QSplitter();
       connect(midSplitter, SIGNAL(splitterMoved(int, int)), this, SLOT(splitterHandleMoved(int, int)));
    void ParentMdiSubWindow::resizeEvent(QResizeEvent * resizeEvent)
        int width = this->width();
        int w = midSplitter->width();
            labelPanelWidth = screenSize.width() / 192 * 18;
            if (labelPanelWidth >= width)
            { labelPanelWidth = width / 3; }
        QList<int> list;
        list << w - labelPanelWidth;
        list << labelPanelWidth;
        inited = true;
    void ParentMdiSubWindow::splitterHandleMoved(int pos, int index)
        labelPanelWidth = midSplitter->width() - pos - 1;

Log in to reply