How to catch event right before QWidget is hidden?
-
Basically, i need to show user a QMessageBox dialog and ask if he wants to apply changes that he made right before QWidget will be hidden. I've tried to reimplement hideEvent() and add QMessageBox in it, but it doesn't work the way i need. I think there's must be an easy workaround, but i can't find anything.
Sample code:
class MyWidget: public QWidget { Q_OBJECT public: explicit MyWidget(const QString oldText, QWidget *parent = 0); ~MyWidget(); protected: inline void hideEvent(QHideEvent *event) { if (newString_ != oldString_){ //ask user if he wants to save changes //... } } private: QString oldString_; QString newString_; Ui::UserPrivilegiesSubwidget *ui;
-
Basically, i need to show user a QMessageBox dialog and ask if he wants to apply changes that he made right before QWidget will be hidden. I've tried to reimplement hideEvent() and add QMessageBox in it, but it doesn't work the way i need. I think there's must be an easy workaround, but i can't find anything.
Sample code:
class MyWidget: public QWidget { Q_OBJECT public: explicit MyWidget(const QString oldText, QWidget *parent = 0); ~MyWidget(); protected: inline void hideEvent(QHideEvent *event) { if (newString_ != oldString_){ //ask user if he wants to save changes //... } } private: QString oldString_; QString newString_; Ui::UserPrivilegiesSubwidget *ui;
@dream_captain
isMyWidget
a top-level widget (window)?The hide event is just a notification, but you can't stop the hiding of the widget. I asked if it's a top-level widget, because for it is possible using the
closeEvent()
handler. -
@dream_captain
isMyWidget
a top-level widget (window)?The hide event is just a notification, but you can't stop the hiding of the widget. I asked if it's a top-level widget, because for it is possible using the
closeEvent()
handler.@raven-worx no, the widget is in another widget (in layout). I have so-called 'main window' and a lot of widgets inside other widgets, organized in a hierarchy. These widgets can be hidden and i must handle that event somehow.
@VRonin thanks, but my problem is something different :)
-
@raven-worx no, the widget is in another widget (in layout). I have so-called 'main window' and a lot of widgets inside other widgets, organized in a hierarchy. These widgets can be hidden and i must handle that event somehow.
@VRonin thanks, but my problem is something different :)
@dream_captain said in How to catch event right before QWidget is hidden?:
These widgets can be hidden and i must handle that event somehow.
Then you need to show the message dialog before you would hide the (sub-)widget.
-
Basically, i need to show user a QMessageBox dialog and ask if he wants to apply changes that he made right before QWidget will be hidden. I've tried to reimplement hideEvent() and add QMessageBox in it, but it doesn't work the way i need. I think there's must be an easy workaround, but i can't find anything.
Sample code:
class MyWidget: public QWidget { Q_OBJECT public: explicit MyWidget(const QString oldText, QWidget *parent = 0); ~MyWidget(); protected: inline void hideEvent(QHideEvent *event) { if (newString_ != oldString_){ //ask user if he wants to save changes //... } } private: QString oldString_; QString newString_; Ui::UserPrivilegiesSubwidget *ui;
@dream_captain said in How to catch event right before QWidget is hidden?:
but it doesn't work the way i need
What is "the way you need"?
-
@dream_captain said in How to catch event right before QWidget is hidden?:
These widgets can be hidden and i must handle that event somehow.
Then you need to show the message dialog before you would hide the (sub-)widget.
@raven-worx @VRonin imagine that user opens a subwidget and makes some changes in it (for example add text, set QCheckBox). Then he wants to leave this subwidget , so he clicks on another subwidget. I must hide first subwidget and show another subwidget. But before that i must check if there's changes that user made and show him a QMessageBox. It's easy to implement in a main widget via closeEvent(), but i can't do it in child-subwidget.
-
Hi,
you could use a QTabWidget and there connect to thevoid QTabWidget::currentChanged(int index)
signal.
-Michael.@m.sue said in How to catch event right before QWidget is hidden?:
you could use a QTabWidget and there connect to the void QTabWidget::currentChanged(int index) signal.
this helps how? The same issue remains using this approach.
The problem is that the hide event is the wrong approach for this. And also the currentChanged() signal is too late and also you still have no way to "cancel the hiding". -
Hi,
- With a tab widget there is no need to explictly hide sub-widgets, the tab widget just does it for you.
- @raven-worx: I did not understand that @dream_captain wants to stop the hiding. I suppose, though, that you could send a signal with singleshot timer with 0s that reverts the change of the tab widget in the slot connected to the currentChanged signal.
-Michael.
-
I think that my approach is completely wrong. The subwidget is hiding by the outside event (user clicks buttons, or some items in table). So i will connect click/activated signal) of these controls to the checkChanges() slot. It will let me check subwidget for changes and then call hide() from the outside.
-
I think that my approach is completely wrong. The subwidget is hiding by the outside event (user clicks buttons, or some items in table). So i will connect click/activated signal) of these controls to the checkChanges() slot. It will let me check subwidget for changes and then call hide() from the outside.
@dream_captain said in How to catch event right before QWidget is hidden?:
I think that my approach is completely wrong. The subwidget is hiding by the outside event (user clicks buttons, or some items in table). So i will connect click/activated signal) of these controls to the checkChanges() slot. It will let me check subwidget for changes and then call hide() from the outside.
and thats the approach i was talking about and the only one available for you.
-
@dream_captain said in How to catch event right before QWidget is hidden?:
I think that my approach is completely wrong. The subwidget is hiding by the outside event (user clicks buttons, or some items in table). So i will connect click/activated signal) of these controls to the checkChanges() slot. It will let me check subwidget for changes and then call hide() from the outside.
and thats the approach i was talking about and the only one available for you.
@raven-worx yeah, tried to implement the easy-approach. Thanks guys.