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 detect a Qwidget is closed (not destroyed) from another class?

How to detect a Qwidget is closed (not destroyed) from another class?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 655 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.
  • KaguroK Offline
    KaguroK Offline
    Kaguro
    wrote on last edited by aha_1980
    #1

    Hi Guys! I would like to achive that i press F2 in a QlineEdit whats in class A. Show a Class B Qwidget where u can pick a text from it with enter. When i press enter the widget is closed (but not destoryed) and Class A detect it, and the text, and put it into the Class A QlineEdit.
    I could do that detect when closed the Class B widget but just so that destroy the whole class B.
    Any ideas?
    (Sorry for my bad englsih)

    kshegunovK 1 Reply Last reply
    0
    • KaguroK Kaguro

      @Bonnie I like that idea! I have a QtableView in Class B (Qdialog now). But i am new in Qt, can u give me an example for that solution? If u have enugh of time for this, and mood of course! :)

      B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #5

      @Kaguro
      Ah...we still need class B, but instead of subclass QWidget, make it subclass QDialog.

      class B : public QDialog
      {
          Q_OBJECT
      public:
          explicit B(QWidget *parent = nullptr);
          QString getText() const;
      private:
          QString text;
      };
      
      B::B(QWidget *parent) : QDialog(parent)
      {
          //Remove the default "?" in the title bar
          setWindowFlag(Qt::WindowContextHelpButtonHint, false);
      }
      
      QString B::getText() const
      {
          return text;
      }
      

      In class B, when you press enter / get the result from the tableview

      //assign the result to "text"
      text = ...;
      //instead of calling "close()", call "accept()", it sets the result of "exec()" to 1
      accept();
      

      So when you want the user to get the result from dialog B:

      B b(this);
      if(b.exec()) {
          //This is when you call "accept()" (with the result)
          QString result = b.getText();
      } else {
         //This is when the dialog is closed by the user (without setting the result)
      }
      
      KaguroK 1 Reply Last reply
      5
      • B Offline
        B Offline
        Bonnie
        wrote on last edited by Bonnie
        #2

        From your description, I feel that using a QDialog as class B would be easier, if you can accept a modal window.
        When you press F2, show the dialog with QDialog::exec(), it will wait until the dialog closes, so you can get your result after that.
        Just like the predefined standard dialogs (QInputDialog, QFileDialog, etc...)

        If you don't want that, then the other way would be reimplement closeEvent() of class B, add a signal there. Then you can connect that signal to detect the closing.

        KaguroK 1 Reply Last reply
        0
        • KaguroK Kaguro

          Hi Guys! I would like to achive that i press F2 in a QlineEdit whats in class A. Show a Class B Qwidget where u can pick a text from it with enter. When i press enter the widget is closed (but not destoryed) and Class A detect it, and the text, and put it into the Class A QlineEdit.
          I could do that detect when closed the Class B widget but just so that destroy the whole class B.
          Any ideas?
          (Sorry for my bad englsih)

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #3

          Install an event filter and respond to the QCloseEvent that the other object receives (without actually capturing it).

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          3
          • B Bonnie

            From your description, I feel that using a QDialog as class B would be easier, if you can accept a modal window.
            When you press F2, show the dialog with QDialog::exec(), it will wait until the dialog closes, so you can get your result after that.
            Just like the predefined standard dialogs (QInputDialog, QFileDialog, etc...)

            If you don't want that, then the other way would be reimplement closeEvent() of class B, add a signal there. Then you can connect that signal to detect the closing.

            KaguroK Offline
            KaguroK Offline
            Kaguro
            wrote on last edited by
            #4

            @Bonnie I like that idea! I have a QtableView in Class B (Qdialog now). But i am new in Qt, can u give me an example for that solution? If u have enugh of time for this, and mood of course! :)

            B 1 Reply Last reply
            0
            • KaguroK Kaguro

              @Bonnie I like that idea! I have a QtableView in Class B (Qdialog now). But i am new in Qt, can u give me an example for that solution? If u have enugh of time for this, and mood of course! :)

              B Offline
              B Offline
              Bonnie
              wrote on last edited by Bonnie
              #5

              @Kaguro
              Ah...we still need class B, but instead of subclass QWidget, make it subclass QDialog.

              class B : public QDialog
              {
                  Q_OBJECT
              public:
                  explicit B(QWidget *parent = nullptr);
                  QString getText() const;
              private:
                  QString text;
              };
              
              B::B(QWidget *parent) : QDialog(parent)
              {
                  //Remove the default "?" in the title bar
                  setWindowFlag(Qt::WindowContextHelpButtonHint, false);
              }
              
              QString B::getText() const
              {
                  return text;
              }
              

              In class B, when you press enter / get the result from the tableview

              //assign the result to "text"
              text = ...;
              //instead of calling "close()", call "accept()", it sets the result of "exec()" to 1
              accept();
              

              So when you want the user to get the result from dialog B:

              B b(this);
              if(b.exec()) {
                  //This is when you call "accept()" (with the result)
                  QString result = b.getText();
              } else {
                 //This is when the dialog is closed by the user (without setting the result)
              }
              
              KaguroK 1 Reply Last reply
              5
              • B Bonnie

                @Kaguro
                Ah...we still need class B, but instead of subclass QWidget, make it subclass QDialog.

                class B : public QDialog
                {
                    Q_OBJECT
                public:
                    explicit B(QWidget *parent = nullptr);
                    QString getText() const;
                private:
                    QString text;
                };
                
                B::B(QWidget *parent) : QDialog(parent)
                {
                    //Remove the default "?" in the title bar
                    setWindowFlag(Qt::WindowContextHelpButtonHint, false);
                }
                
                QString B::getText() const
                {
                    return text;
                }
                

                In class B, when you press enter / get the result from the tableview

                //assign the result to "text"
                text = ...;
                //instead of calling "close()", call "accept()", it sets the result of "exec()" to 1
                accept();
                

                So when you want the user to get the result from dialog B:

                B b(this);
                if(b.exec()) {
                    //This is when you call "accept()" (with the result)
                    QString result = b.getText();
                } else {
                   //This is when the dialog is closed by the user (without setting the result)
                }
                
                KaguroK Offline
                KaguroK Offline
                Kaguro
                wrote on last edited by
                #6

                @Bonnie And its working!! :DD Many many thanks !!!! I'll come to you with a beer! :D Thanks again! :)

                1 Reply Last reply
                1

                • Login

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