FuzzyCat444
FuzzyCat444

Reputation: 367

Is there a Qt event for when a QLayout has finished redoing geometry?

I have a custom class which extends QWidget and installs a vertical box layout on itself, as well as child widgets which are added to said layout. In this sense, it is a composite widget.

I have a QPainterPath as a widget member variable which represents the graphical representation of the widget and is drawn in paintEvent(). This path needs to be updated based on the positions and sizes of the child widgets, which are managed by the layout.

I have been warned not to place calculations in the paintEvent() function, so I decided to update the path before paintEvent() but after the layout has been computed.

As far as I know, however, there is no event representing the completion of layout calculations. The QEvent::LayoutRequest event is only called before the layout is updated. The QEvent::Resize event is usually called after the layout is updated, but only if the layout calculation was induced by a change in widget size.

I am looking for an event that is fired precisely when the layout is done being calculated, no matter the trigger.

So far, my best idea was to extend the QVBoxLayout class and emit a signal after the layout's setGeometry() function is done, since that is when the widgets have their final position:

class NotifyQVBoxLayout : public QVBoxLayout
{
    Q_OBJECT
public:
    explicit NotifyQVBoxLayout() : QVBoxLayout() {}

    void setGeometry(const QRect &rect) override
    {
        QVBoxLayout::setGeometry(rect);
        emit doneGeometry();
    }

signals:
    void doneGeometry();
};

doneGeometry() then activates a slot on the widget, which immediately computes the new path to graphically reflect the new layout. This seems to work, but I'm wondering if there is a more idiomatic way of doing this in Qt.

Another idea I had was to intercept the QEvent::LayoutRequest and QEvent::Resize events and call the layout's setGeometry() function manually, followed by updating the path, but this produces buggy behavior.

Upvotes: 1

Views: 81

Answers (0)

Related Questions