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. Get variable from a dialog to MainWindow
Forum Updated to NodeBB v4.3 + New Features

Get variable from a dialog to MainWindow

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 615 Views 2 Watching
  • 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.
  • S Offline
    S Offline
    Sucharek
    wrote on last edited by
    #1

    Hello, I know that this "type" of post has already been created several times, but me being the noob I am, can't understand these...
    So, I just want to know, how to get a value from a dialog, and give it to MainWindow.
    Thanks

    Pl45m4P 1 Reply Last reply
    0
    • S Sucharek

      Hello, I know that this "type" of post has already been created several times, but me being the noob I am, can't understand these...
      So, I just want to know, how to get a value from a dialog, and give it to MainWindow.
      Thanks

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @Sucharek said in Get variable from a dialog to MainWindow:

      get a value from a dialog, and give it to MainWindow.

      A QDialog class?

      Either with signals & slots or as return value.

      // MainWindow, where you start your dialog
      Dialog dlg;
      if (dlg.exec() == QDialog::Accepted)
      {
         // simply call the getters from your dialog class to get the values
         int x = dlg.getMyNum();
         QString s = dlg.getMyString();
      }
      

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      2
      • S Offline
        S Offline
        Sucharek
        wrote on last edited by
        #3

        How do I call the getters?

        Pl45m4P 1 Reply Last reply
        0
        • S Sucharek

          How do I call the getters?

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #4

          @Sucharek

          int x = dlg.getMyNum();
          QString s = dlg.getMyString();

          These are already the function calls.

          You have to have getMyNum() and getMyString() as public (getter) function in your dialog class. Then just return whatever you want to pass to your mainWindow (the return types have to match of course).

          One of those functions could look like this:

          // Dialog header
          public:
            // ctor
           // ...
           int getMyNum(){ return m_myNumber; }
          
          private:
              int m_myNumber;  // the private variable, you want to return
          
          

          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          S 1 Reply Last reply
          1
          • Pl45m4P Pl45m4

            @Sucharek

            int x = dlg.getMyNum();
            QString s = dlg.getMyString();

            These are already the function calls.

            You have to have getMyNum() and getMyString() as public (getter) function in your dialog class. Then just return whatever you want to pass to your mainWindow (the return types have to match of course).

            One of those functions could look like this:

            // Dialog header
            public:
              // ctor
             // ...
             int getMyNum(){ return m_myNumber; }
            
            private:
                int m_myNumber;  // the private variable, you want to return
            
            
            S Offline
            S Offline
            Sucharek
            wrote on last edited by
            #5

            Ok, so I don't have any errors now, but if I click OK on my dialog, nothing happens.
            I think that the
            if (setWorkingDirectory.exec() == QDialog::Accepted)
            doesn't work for me, because I put qDebug in there, and it doesn't print anything.

            Pl45m4P 2 Replies Last reply
            0
            • S Sucharek

              Ok, so I don't have any errors now, but if I click OK on my dialog, nothing happens.
              I think that the
              if (setWorkingDirectory.exec() == QDialog::Accepted)
              doesn't work for me, because I put qDebug in there, and it doesn't print anything.

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by Pl45m4
              #6

              @Sucharek said in Get variable from a dialog to MainWindow:

              if I click OK on my dialog, nothing happens.

              Is your Ok Button connected to accept()?? If you use the default button box, it should be the case by default. If you add your own buttons, you have to do it manually.

              Add accept() at the end of the slot, which is called when you click your Ok button to finish your dialog.

              • https://doc.qt.io/qt-5/qdialog.html#details

              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              1 Reply Last reply
              2
              • S Offline
                S Offline
                Sucharek
                wrote on last edited by
                #7

                It works now! Thanks
                I'm sorry you have to deal with me, not understanding simple stuff.
                Anyways, it all works now!
                Thanks again :D

                1 Reply Last reply
                0
                • S Sucharek

                  Ok, so I don't have any errors now, but if I click OK on my dialog, nothing happens.
                  I think that the
                  if (setWorkingDirectory.exec() == QDialog::Accepted)
                  doesn't work for me, because I put qDebug in there, and it doesn't print anything.

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by Pl45m4
                  #8

                  @Sucharek said in Get variable from a dialog to MainWindow:

                  if (setWorkingDirectory.exec() == QDialog::Accepted)

                  BTW: It looks like yoou want to set a directory there. You can use QFileDialog class for this. Then you dont have to worry about the return function and your dialog design.

                  • https://doc.qt.io/qt-5/qfiledialog.html
                  • https://doc.qt.io/qt-5/qfiledialog.html#getExistingDirectory

                  For example:

                  QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                                                                  "/home",
                                                                  QFileDialog::ShowDirsOnly
                                                                  | QFileDialog::DontResolveSymlinks);
                  

                  This can be called directly from your mainWindow without any additional QDialog class.


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  S 1 Reply Last reply
                  1
                  • Pl45m4P Pl45m4

                    @Sucharek said in Get variable from a dialog to MainWindow:

                    if (setWorkingDirectory.exec() == QDialog::Accepted)

                    BTW: It looks like yoou want to set a directory there. You can use QFileDialog class for this. Then you dont have to worry about the return function and your dialog design.

                    • https://doc.qt.io/qt-5/qfiledialog.html
                    • https://doc.qt.io/qt-5/qfiledialog.html#getExistingDirectory

                    For example:

                    QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                                                                    "/home",
                                                                    QFileDialog::ShowDirsOnly
                                                                    | QFileDialog::DontResolveSymlinks);
                    

                    This can be called directly from your mainWindow without any additional QDialog class.

                    S Offline
                    S Offline
                    Sucharek
                    wrote on last edited by
                    #9

                    Ok, I will look into it.
                    Thanks for the tip!

                    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