1
votes

To explain the situation, I have a QMainWindow with plenty of stuff inside. My goal is to make some kind of a notification that appears at the bottom right corner of this window.
The notification widget must be over every other widgets.

So far I've created that Notification widget that inherits from QWidget. By setting the QMainWindow as its parent I've got it 'floating' on top of every other widgets. It doesn't belong to any layout. Now the big issue is to make it stick to the bottom right corner. I managed to place it manually but what I want now is to move it upon a resize event.

Unfortunately since it doesn't belong to any layout it doesn't receive any resizeEvent, so I can't overload the resizeEvent() function. An attempt was to make the QMainWindow emit a signal in its resizeEvent method but I am not really happy with this method. Especially because in the Notification widget constructor I have this connect(static_cast<MainWindow*>(parent), &MainWindow::resized, this, &Notification::update_position);, and it kind of breaks the genericity of the widget since its parent must be a MainWindow widget.

So the question is how can one widget react to another widget event ? In my case here how can the notification widget be notified when its parent is getting resized ? And I forgot to mention that I don't want the MainWindow widget to know anything about this notification widget. The notification widget can be managed on its own. You just call something like new Notification(parent) and it will do its stuff on its own

Any ideas are welcome, Thanks in advance :)

1
One way is to react in the QMainWindow instead knowing that your QWidget that needs to change size is a child of your main window so it's pointer can easily be found. - drescherjm

1 Answers

5
votes

You can install the notification widget as an event filter for its parent, e.g.:

NotificationWidget::NotificationWidget(QWidget *parent) : QWidget(parent) {
    parent->installEventFilter(this);
}

bool NotificationWidget::eventFilter(QObject *obj, QEvent *event) {
    // normal event handling
    bool ret = QObject::eventFilter(obj, event);

    if (event->type() == QEvent::Resize) {
        // Parent resized, adjust position
    }
    return ret;
}