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. [Solved] Return value from dialog

[Solved] Return value from dialog

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 20.2k 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.
  • J Offline
    J Offline
    jocala
    wrote on last edited by Chris Kawa
    #1

    I need to return two values to my mainwindow from a dialog, a line edit and a checkbox. What is the proper way to do this?

    Call dialog:

    uninstallDialog uninstalldialog;
    uninstalldialog.setModal(true);
    uninstalldialog.exec();
    
    #include "uninstalldialog.h"
    #include "ui_uninstalldialog.h"
    
    uninstallDialog::uninstallDialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::uninstallDialog)
    {
        ui->setupUi(this);
    }
    
    uninstallDialog::~uninstallDialog()
    {
        delete ui;
    }
    
    void uninstallDialog::on_cancelButton_clicked()
    {
        close();
    }
    
    void uninstallDialog::on_okButton_clicked()
    {
        // return ui->packagename->text(),ui->keepBox->checkState();
    }
    
    
    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      zibicoder
      wrote on last edited by Chris Kawa
      #2

      I think you can use slot and signal for do this. Add signal in dialog class, e.g. okButtonResponse(QString text, bool cbxState) and emit this signal to slot connected in mainwindow.

      void uninstallDialog::on_okButton_clicked()
      {
          // return ui->packagename->text(),ui->keepBox->checkState();
           emit okButtonResponse(ui->packagename->text(), ui->keepBox->checkState());
      }
      
      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        You can use two getters, one for each value you're interested in

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #4

          First of all you should not return anything from a void function (on_okButton_clicked). The proper way to close the dialog and return from exec() is to call accept() or reject(). This will terminate the exec with a return code of QDialog::Accepted or QDialog::Rejected.

          You can use signal/slot as zibicoder suggested, but it's not very convenient.

          The "usual" method to do it is to create separate getters for the actual values. The invocation would then look something like this:

          UninstallDialog dialog;
          dialog.setModal(true);
          if(dialog.exec() == QDialog::Accepted)
          {
              QString foo = dialog.getFoo();
              bool bar = dialog.getBar();
              ... //do something with foo and bar
          }
          

          If you'd like a single getter for these values you can always pack them in a QPair or some other structure, eg.

          class UninstalDialog : public QDialog {
             ...
          public:
             struct Result {
                 QString foo;
                 boool bar;
             }
             Result getFooBar() const;
          }
          
          UninstalDialog::Result UninstalDialog::getFooBar() const {
              Result result { ui->packagename->text(), ui->keepBox->isChecked() };
              return result;
          }
          
          1 Reply Last reply
          4
          • J Offline
            J Offline
            jocala
            wrote on last edited by Chris Kawa
            #5

            Thanks for your replies and patience.

              uninstallDialog dialog;
                dialog.setModal(true);
                if(dialog.exec() == QDialog::Accepted)
                {
                  QString pname = dialog.packagename();
                  bool keep = dialog.keepBox();
            
                }
            

            I'm missing something. The code above fails with:

            mainwindow.cpp:204: error: no member named 'packagename' in 'uninstallDialog'
                  QString pname = dialog.packagename();
                                  ~~~~~~ ^
            
            mainwindow.cpp:205: error: no member named 'keepBox' in 'uninstallDialog'
                  bool keep = dialog.keepBox();
                              ~~~~~~ ^
            
            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by Chris Kawa
              #6

              packagename() and keepBox() are a regular member functions. You need to define them like any other. They won't do that themselves:

              // .h
              class UninstalDialog : public QDialog {
                 ...
              public:
                 QString packageName();
                 bool keepBox();
              }
              
              // .cpp
              QString UninstalDialog::packageName() { 
                 return ui->packagename->text();
              }
              bool UninstalDialog::keepBox() {
                 return ui->keepBox->isChecked();
              }
              

              Btw. checkState() does not return bool. It returns an enum that represents one of the 3 checkbox states. To get bool for a 2-state checkbox use isChecked() method.

              1 Reply Last reply
              1
              • J Offline
                J Offline
                jocala
                wrote on last edited by
                #7

                Thanks! This is working perfectly now.

                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