Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to catch event right before QWidget is hidden?

How to catch event right before QWidget is hidden?

Scheduled Pinned Locked Moved General and Desktop
13 Posts 4 Posters 8.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • dream_captainD Offline
    dream_captainD Offline
    dream_captain
    wrote on last edited by dream_captain
    #1

    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;
    
    raven-worxR VRoninV 2 Replies Last reply
    0
    • dream_captainD dream_captain

      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;
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @dream_captain
      is MyWidget 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.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      dream_captainD 1 Reply Last reply
      1
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3
        if (newString_ != oldString_){
           if(QMessageBox::question(this,tr("Save Changes"),tr("Do you want to save the changes?"))==QMessageBox::Yes){
        //save the changes
        }
           }
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        0
        • raven-worxR raven-worx

          @dream_captain
          is MyWidget 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_captainD Offline
          dream_captainD Offline
          dream_captain
          wrote on last edited by
          #4

          @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-worxR 1 Reply Last reply
          0
          • dream_captainD dream_captain

            @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-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            @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.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            dream_captainD 1 Reply Last reply
            0
            • dream_captainD dream_captain

              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;
              
              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              @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"?

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              0
              • raven-worxR raven-worx

                @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.

                dream_captainD Offline
                dream_captainD Offline
                dream_captain
                wrote on last edited by
                #7

                @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.

                1 Reply Last reply
                0
                • m.sueM Offline
                  m.sueM Offline
                  m.sue
                  wrote on last edited by
                  #8

                  Hi,
                  you could use a QTabWidget and there connect to the void QTabWidget::currentChanged(int index) signal.
                  -Michael.

                  raven-worxR 1 Reply Last reply
                  0
                  • m.sueM m.sue

                    Hi,
                    you could use a QTabWidget and there connect to the void QTabWidget::currentChanged(int index) signal.
                    -Michael.

                    raven-worxR Offline
                    raven-worxR Offline
                    raven-worx
                    Moderators
                    wrote on last edited by raven-worx
                    #9

                    @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".

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    1 Reply Last reply
                    0
                    • m.sueM Offline
                      m.sueM Offline
                      m.sue
                      wrote on last edited by m.sue
                      #10

                      Hi,

                      1. With a tab widget there is no need to explictly hide sub-widgets, the tab widget just does it for you.
                      2. @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.

                      1 Reply Last reply
                      0
                      • dream_captainD Offline
                        dream_captainD Offline
                        dream_captain
                        wrote on last edited by
                        #11

                        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.

                        raven-worxR 1 Reply Last reply
                        1
                        • dream_captainD dream_captain

                          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.

                          raven-worxR Offline
                          raven-worxR Offline
                          raven-worx
                          Moderators
                          wrote on last edited by
                          #12

                          @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.

                          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                          If you have a question please use the forum so others can benefit from the solution in the future

                          dream_captainD 1 Reply Last reply
                          0
                          • raven-worxR raven-worx

                            @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_captainD Offline
                            dream_captainD Offline
                            dream_captain
                            wrote on last edited by
                            #13

                            @raven-worx yeah, tried to implement the easy-approach. Thanks guys.

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved