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.
  • D Offline
    D Offline
    dream_captain
    wrote on 21 Nov 2016, 11:34 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;
    
    R V 2 Replies Last reply 21 Nov 2016, 11:39
    0
    • D dream_captain
      21 Nov 2016, 11:34

      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;
      
      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 21 Nov 2016, 11:39 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

      D 1 Reply Last reply 21 Nov 2016, 11:47
      1
      • V Offline
        V Offline
        VRonin
        wrote on 21 Nov 2016, 11:40 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
        • R raven-worx
          21 Nov 2016, 11:39

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

          D Offline
          D Offline
          dream_captain
          wrote on 21 Nov 2016, 11:47 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 :)

          R 1 Reply Last reply 21 Nov 2016, 11:49
          0
          • D dream_captain
            21 Nov 2016, 11:47

            @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 :)

            R Offline
            R Offline
            raven-worx
            Moderators
            wrote on 21 Nov 2016, 11:49 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

            D 1 Reply Last reply 21 Nov 2016, 12:00
            0
            • D dream_captain
              21 Nov 2016, 11:34

              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;
              
              V Offline
              V Offline
              VRonin
              wrote on 21 Nov 2016, 11:49 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
              • R raven-worx
                21 Nov 2016, 11:49

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

                D Offline
                D Offline
                dream_captain
                wrote on 21 Nov 2016, 12:00 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 21 Nov 2016, 12:14 last edited by
                  #8

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

                  R 1 Reply Last reply 21 Nov 2016, 12:18
                  0
                  • m.sueM m.sue
                    21 Nov 2016, 12:14

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

                    R Offline
                    R Offline
                    raven-worx
                    Moderators
                    wrote on 21 Nov 2016, 12:18 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 21 Nov 2016, 12:28 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
                      • D Offline
                        D Offline
                        dream_captain
                        wrote on 21 Nov 2016, 12:35 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.

                        R 1 Reply Last reply 21 Nov 2016, 12:54
                        1
                        • D dream_captain
                          21 Nov 2016, 12:35

                          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.

                          R Offline
                          R Offline
                          raven-worx
                          Moderators
                          wrote on 21 Nov 2016, 12:54 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

                          D 1 Reply Last reply 21 Nov 2016, 14:34
                          0
                          • R raven-worx
                            21 Nov 2016, 12:54

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

                            D Offline
                            D Offline
                            dream_captain
                            wrote on 21 Nov 2016, 14:34 last edited by
                            #13

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

                            1 Reply Last reply
                            0

                            1/13

                            21 Nov 2016, 11:34

                            • Login

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