Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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);
        midSplitter->setHandleWidth(1);
        midSplitter->setChildrenCollapsible(false);
    
        // plot on the left
        leftPanel = new QWidget();
        leftPanelLayout = new QVBoxLayout();
        leftPanel->setLayout(leftPanelLayout);
        leftPanelLayout->setSpacing(0);
        leftPanelLayout->setContentsMargins(0,0,0,0);
       
    
        // right panel
        rightPanel = new QWidget();
        rightPanelLayout = new QVBoxLayout();
        rightPanelLayout->setContentsMargins(0,0,0,0);
        rightPanelLayout->setSpacing(0);
        rightPanel->setLayout(rightPanelLayout);
        rightPanel->layout()->setAlignment(Qt::AlignCenter);
    
        // add support widget to subwindow
        setWidget(midSplitter);
        ...
    }
    

    right panel issue.png


  • Lifetime Qt Champion

    Hi,

    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;
        midSplitter->setSizes(list);
    
    }
    


  • 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);
       private:
          bool inited; // initialized false in constructor
          int labelPanelWidth; // initialized 0 in constructor 
          QSplitter * midSplitter;
    }
    
    ParentMdiSubWindow::ParentMdiSubWindow(QWidget *parent, Qt::WindowFlags flags):QMdiSubWindow(parent, flags),
    inited(false),
    labelPanelWidth(0)
    {
       midSplitter = new QSplitter();
       midSplitter->setHandleWidth(1);
       connect(midSplitter, SIGNAL(splitterMoved(int, int)), this, SLOT(splitterHandleMoved(int, int)));
    }
    
    void ParentMdiSubWindow::resizeEvent(QResizeEvent * resizeEvent)
    {   
        int width = this->width();
        int w = midSplitter->width();
        if(!inited)
        {
            labelPanelWidth = screenSize.width() / 192 * 18;
    
            if (labelPanelWidth >= width)
            { labelPanelWidth = width / 3; }
        }
        QList<int> list;
        list << w - labelPanelWidth;
        list << labelPanelWidth;
        midSplitter->setSizes(list);
        inited = true;
        QMdiSubWindow::resizeEvent(resizeEvent);
    }
    
    void ParentMdiSubWindow::splitterHandleMoved(int pos, int index)
    {
        labelPanelWidth = midSplitter->width() - pos - 1;
    }
    

Log in to reply