Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

What is required for children of QSplitter to be resized when the QSplitter changes size?



  • Note that this isn't about what happens when the user moves the splitter position, this is about when the QSplitter widget is resized when the parent of the QSplitter changes layout.

    I have a dialog in which the layout of child widgets of a QSplitter needs special handling, i.e. standard layouts cannot be used. Hence I implemented according to headline "Manual Layout" on https://doc.qt.io/qt-5/layout.html for the children of the QSplitter. The result was that when the QSplitter is resized the children are not resized. When I remove the implementation for manual layout and use a box layout instead it works as intended when the QSplitter is resized.

    Hence, what must be fulfilled in order for the QSplitter to resize children when the QSplitter itself is resized?


  • Lifetime Qt Champion

    Hi,

    In your event method reimplementation you do not call the base class implementation for the events you do not handle, this is a bad idea.


  • Lifetime Qt Champion

    Hi,

    How exactly did you implement your manual layout ?



  • Header extract (dolayout is my own function)

    protected:
       bool event(QEvent *event) override;
       void resizeEvent(QResizeEvent *event) override;
       QSize	sizeHint() const override;
    private:
       void doLayout();
    

    Implementation

    bool YtScopeShelve::event(QEvent *event)
    {
       if (event->type() == QEvent::LayoutRequest)
       {
          doLayout();
          return true;
       }
       else
       {
          return false;
       }
    }
    
    void YtScopeShelve::resizeEvent(QResizeEvent *event)
    {
       QWidget::resizeEvent(event);
       doLayout();
    }
    
    QSize	YtScopeShelve::sizeHint() const
    {
       QWidget* yAxesWidget = findChild<QWidget*>("yAxesWidget");
       QWidget* plotAndTableWidget = findChild<QWidget*>("plotTableSplitter");
       // Height will be ignored by parent widgetlayout.
       return QSize(yAxesWidget->sizeHint().width() + plotAndTableWidget->sizeHint().width(), 100);
    }
    
    /*
     * According to https://doc.qt.io/qt-5/layout.html the child widgets shall be repositioned
     * on both QEvent::LayoutRequest and resizeEvent
     */
    void YtScopeShelve::doLayout()
    {
       QWidget* yAxesWidget = findChild<QWidget*>("yAxesWidget");
       QWidget* plotAndTableWidget = findChild<QWidget*>("plotTableSplitter");
       yAxesWidget->setGeometry(0, 0, yAxesWidget->sizeHint().width(), height());
       plotAndTableWidget->setGeometry(yAxesWidget->sizeHint().width(), 0, width() - yAxesWidget->sizeHint().width(), height());
    }
    

    resizeEvent is never called when parent of QSplitter resizes the QSplitter.


  • Lifetime Qt Champion

    Hi,

    In your event method reimplementation you do not call the base class implementation for the events you do not handle, this is a bad idea.



  • just detected that myself, thanks!


Log in to reply