How to force layout update/resize when child it manages resizes?

前端 未结 2 1964
粉色の甜心
粉色の甜心 2021-02-07 09:31

I have a custom QTextEdit widget that has it\'s own resize grip in the corner. I can resize the text widget fine, however the layout it is inside does not change wi

2条回答
  •  耶瑟儿~
    2021-02-07 09:44

    The answer of leemes gave me some insight and I managed to achieve resize of a QTextWidget inside a layout among other controls using custom SizeGrip control. Note that at least one scroll bar should be visible in order to have visible size grip.

    It was necessary to reimplement the widget's sizeHint() method:

    QSize MultilineTextEdit::sizeHint() const
    {
        return size();
    }
    

    Then at the end of mouseMoveEvent() of my SizeGrip widget I added

    void SizeGrip::mouseMoveEvent(QMouseEvent * e)
    {
        // note that this is not the full code of the method
        ...
        // save the old minimum and maximum size of the widget (w) resized by the grip
        QSize oldMin = w->minimumSize();
        QSize oldMax = w->maximumSize();
    
        w->setFixedSize(nr.size()); // nr is the new rect of the widget
    
        w->setMinimumSize(oldMin);
        w->setMaximumSize(oldMax);
    }
    

    The SizeGrip widget was inspired by the QSizeGrip class which operates on the top level parent window directly. SizeGrip operates on its direct parent widget instead.

提交回复
热议问题