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. Pass variable value from Dialog to MainWindow
Forum Updated to NodeBB v4.3 + New Features

Pass variable value from Dialog to MainWindow

Scheduled Pinned Locked Moved Solved General and Desktop
34 Posts 5 Posters 6.4k 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.
  • TheCipo76T TheCipo76

    Hi,
    i have an application with a mainwindow and a dialog (modal)
    in mainwindow i have set some variable value and i have passed to a dialog
    all works..

    now in the dialog i have to change the variable value and return to mainwindow these value

    i have to use signal and slot or exists another solution??

    Pablo J. RoginaP Offline
    Pablo J. RoginaP Offline
    Pablo J. Rogina
    wrote on last edited by
    #2

    @TheCipo76 said in Pass variable value from Dialog to MainWindow:

    i have to use signal and slot or exists another solution??

    I guess signal & slot is a good way to go, as it allows for making your dialog more versatile in the sense that when the dialog knows that particular value was updated, it will send such signal and the mainwindow (which created the dialog object) will connect that signal to its own slot to update the value as needed.

    Upvote the answer(s) that helped you solve the issue
    Use "Topic Tools" button to mark your post as Solved
    Add screenshots via postimage.org
    Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

    1 Reply Last reply
    0
    • TheCipo76T TheCipo76

      Hi,
      i have an application with a mainwindow and a dialog (modal)
      in mainwindow i have set some variable value and i have passed to a dialog
      all works..

      now in the dialog i have to change the variable value and return to mainwindow these value

      i have to use signal and slot or exists another solution??

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #3

      @TheCipo76 said in Pass variable value from Dialog to MainWindow:

      and a dialog (modal)

      It depends a bit. Do you use exec() or show() for your dialog? As exec() blocks the main window anyway, signals&slots are no good solution. For show() they are perfect.

      Qt has to stay free or it will die.

      TheCipo76T 1 Reply Last reply
      0
      • aha_1980A aha_1980

        @TheCipo76 said in Pass variable value from Dialog to MainWindow:

        and a dialog (modal)

        It depends a bit. Do you use exec() or show() for your dialog? As exec() blocks the main window anyway, signals&slots are no good solution. For show() they are perfect.

        TheCipo76T Offline
        TheCipo76T Offline
        TheCipo76
        wrote on last edited by TheCipo76
        #4

        @aha_1980 at the moment i have used .exec() but i don't know how to do in this case..

        so i have to use dialog as modeless (.show())?? or exist another solution??

        aha_1980A 1 Reply Last reply
        0
        • TheCipo76T TheCipo76

          @aha_1980 at the moment i have used .exec() but i don't know how to do in this case..

          so i have to use dialog as modeless (.show())?? or exist another solution??

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #5

          @TheCipo76 In that case, I'd get the variables back from the dialog like you passed them to it.

          Hint: show your dialog code, makes discussion easier ;)

          Qt has to stay free or it will die.

          TheCipo76T 1 Reply Last reply
          1
          • aha_1980A aha_1980

            @TheCipo76 In that case, I'd get the variables back from the dialog like you passed them to it.

            Hint: show your dialog code, makes discussion easier ;)

            TheCipo76T Offline
            TheCipo76T Offline
            TheCipo76
            wrote on last edited by TheCipo76
            #6

            @aha_1980 this is an extract of my mainwindow code

            void MainWindow::on_actionImpostazioni_Porta_Seriale_triggered()
            {
                ImpostazioniSP SetImp (this, BaudRate, DataBits, Parity);
                SetImp.setModal(true);
                SetImp.exec();
            }
            

            and this is from dialog :

            ImpostazioniSP::ImpostazioniSP(QWidget *parent, const QString BaudRate, const QString DataBits, const QString Parity) :
                QDialog(parent),
                BaudRate(BaudRate),
                DataBits(DataBits),
                Parity(Parity),
                ui(new Ui::ImpostazioniSP)
            
            {
                ui->setupUi(this);
                ...
            }
            
            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #7

              Hi
              So one way would be

              void MainWindow::on_actionImpostazioni_Porta_Seriale_triggered()
              {
              ImpostazioniSP SetImp (this, BaudRate, DataBits, Parity);
              SetImp.setModal(true);
              if ( SetImp.exec() == QDialog::Accepted ) {
              BaudRate=SetImp.getBaudRate();
              DataBits = SetImp.getDataBits();
              ....
              }

              }

              TheCipo76T 1 Reply Last reply
              4
              • mrjjM mrjj

                Hi
                So one way would be

                void MainWindow::on_actionImpostazioni_Porta_Seriale_triggered()
                {
                ImpostazioniSP SetImp (this, BaudRate, DataBits, Parity);
                SetImp.setModal(true);
                if ( SetImp.exec() == QDialog::Accepted ) {
                BaudRate=SetImp.getBaudRate();
                DataBits = SetImp.getDataBits();
                ....
                }

                }

                TheCipo76T Offline
                TheCipo76T Offline
                TheCipo76
                wrote on last edited by TheCipo76
                #8

                @mrjj Interesting.. This is that i'm looking for..
                but what i have to implement in dialog to make it works??
                in dialog there's no member or function named "getBaudRate"

                mrjjM 1 Reply Last reply
                0
                • TheCipo76T TheCipo76

                  @mrjj Interesting.. This is that i'm looking for..
                  but what i have to implement in dialog to make it works??
                  in dialog there's no member or function named "getBaudRate"

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #9

                  @TheCipo76
                  Hi
                  well it would just be a getter function returning its internal variables.
                  int ImpostazioniSP::getBaudRate() { // note return type should be the same as the one BaudRate is declared with
                  return BaudRate;
                  }
                  or directly from its widgets if they not copy the values back to internal variables.
                  (which i assume they do )

                  TheCipo76T 1 Reply Last reply
                  3
                  • mrjjM mrjj

                    @TheCipo76
                    Hi
                    well it would just be a getter function returning its internal variables.
                    int ImpostazioniSP::getBaudRate() { // note return type should be the same as the one BaudRate is declared with
                    return BaudRate;
                    }
                    or directly from its widgets if they not copy the values back to internal variables.
                    (which i assume they do )

                    TheCipo76T Offline
                    TheCipo76T Offline
                    TheCipo76
                    wrote on last edited by TheCipo76
                    #10

                    @mrjj I have tried but don't works..

                    ImpostazioniSP SetImp (this, BaudRate, DataBits, Parity);
                        SetImp.setModal(true);
                        //SetImp.exec();
                        if ( SetImp.exec() == QDialog::Accepted ) {
                        BaudRate=SetImp.getBaudRate();
                        //DataBits = SetImp.getDataBits();
                        //Parity = SetImp.getParity();
                        }
                    

                    in dialog:

                    QString ImpostazioniSP::getBaudRate() {
                    return BaudRate;
                    }
                    

                    and declaration:

                    public:
                        explicit ImpostazioniSP(QWidget *parent = nullptr, QString BaudRate="", QString DataBits="", QString Parity="");
                        ~ImpostazioniSP();
                    
                        QString BaudRate, DataBits, Parity;
                        QString getBaudRate();
                        //QString getDataBits();
                        //QString getParity();
                    

                    Where i'm failing??

                    aha_1980A 1 Reply Last reply
                    0
                    • TheCipo76T TheCipo76

                      @mrjj I have tried but don't works..

                      ImpostazioniSP SetImp (this, BaudRate, DataBits, Parity);
                          SetImp.setModal(true);
                          //SetImp.exec();
                          if ( SetImp.exec() == QDialog::Accepted ) {
                          BaudRate=SetImp.getBaudRate();
                          //DataBits = SetImp.getDataBits();
                          //Parity = SetImp.getParity();
                          }
                      

                      in dialog:

                      QString ImpostazioniSP::getBaudRate() {
                      return BaudRate;
                      }
                      

                      and declaration:

                      public:
                          explicit ImpostazioniSP(QWidget *parent = nullptr, QString BaudRate="", QString DataBits="", QString Parity="");
                          ~ImpostazioniSP();
                      
                          QString BaudRate, DataBits, Parity;
                          QString getBaudRate();
                          //QString getDataBits();
                          //QString getParity();
                      

                      Where i'm failing??

                      aha_1980A Offline
                      aha_1980A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @TheCipo76

                      Where i'm failing??

                      Without an error message, how should we know what "doesn't work" means?

                      Qt has to stay free or it will die.

                      TheCipo76T 1 Reply Last reply
                      1
                      • aha_1980A aha_1980

                        @TheCipo76

                        Where i'm failing??

                        Without an error message, how should we know what "doesn't work" means?

                        TheCipo76T Offline
                        TheCipo76T Offline
                        TheCipo76
                        wrote on last edited by TheCipo76
                        #12

                        @aha_1980 no errors but the value was not passed

                        i've tried both variable and widget:

                        QString ImpostazioniSP::getBaudRate() {
                            return QString(ui->comboBox_BaudRate->currentText());
                            //return BaudRate;
                        }
                        
                        aha_1980A 1 Reply Last reply
                        0
                        • TheCipo76T TheCipo76

                          @aha_1980 no errors but the value was not passed

                          i've tried both variable and widget:

                          QString ImpostazioniSP::getBaudRate() {
                              return QString(ui->comboBox_BaudRate->currentText());
                              //return BaudRate;
                          }
                          
                          aha_1980A Offline
                          aha_1980A Offline
                          aha_1980
                          Lifetime Qt Champion
                          wrote on last edited by
                          #13

                          @TheCipo76 but that looks perfectly fine. the cast to QString is unneeded, by the way.

                          You should start the debugger to see what happens. Is the combobox filled with values?

                          Qt has to stay free or it will die.

                          TheCipo76T 1 Reply Last reply
                          2
                          • aha_1980A aha_1980

                            @TheCipo76 but that looks perfectly fine. the cast to QString is unneeded, by the way.

                            You should start the debugger to see what happens. Is the combobox filled with values?

                            TheCipo76T Offline
                            TheCipo76T Offline
                            TheCipo76
                            wrote on last edited by
                            #14

                            @aha_1980 Debugger says that value was passed fine
                            but variable is set to start value..

                            Start value 115200

                            in the dialog i've setted 9600

                            Debug print 9600

                            but in mainwindow BaudRate is still 115200

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #15

                              Do you mean the MainWindow still shows the old value ? If so, are you updating the related widget once you executed your dialog ?

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

                              TheCipo76T 1 Reply Last reply
                              0
                              • SGaistS SGaist

                                Do you mean the MainWindow still shows the old value ? If so, are you updating the related widget once you executed your dialog ?

                                TheCipo76T Offline
                                TheCipo76T Offline
                                TheCipo76
                                wrote on last edited by
                                #16

                                @SGaist there's no widget BaudRate is only a variable (not showed in mainwindow)

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #17

                                  So if you have something like:

                                  qDebug() << "Before" << BaudRate;
                                  ImpostazioniSP SetImp (this, BaudRate, DataBits, Parity);
                                  SetImp.setModal(true);
                                  if ( SetImp.exec() == QDialog::Accepted ) {
                                      BaudRate = SetImp.getBaudRate();
                                      qDebug() << "After" << BaudRate;
                                  }
                                  

                                  What's the result ?

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

                                  TheCipo76T 1 Reply Last reply
                                  0
                                  • SGaistS SGaist

                                    So if you have something like:

                                    qDebug() << "Before" << BaudRate;
                                    ImpostazioniSP SetImp (this, BaudRate, DataBits, Parity);
                                    SetImp.setModal(true);
                                    if ( SetImp.exec() == QDialog::Accepted ) {
                                        BaudRate = SetImp.getBaudRate();
                                        qDebug() << "After" << BaudRate;
                                    }
                                    

                                    What's the result ?

                                    TheCipo76T Offline
                                    TheCipo76T Offline
                                    TheCipo76
                                    wrote on last edited by TheCipo76
                                    #18

                                    @SGaist this is the result:

                                    Before "115200"
                                    After "115200"

                                    in the dialog qdebug print 9600 (as i've setted)
                                    but in the mainwindows result still 115200

                                    1 Reply Last reply
                                    0
                                    • SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #19

                                      Can you show the complete code of the current version of your dialog ?

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

                                      TheCipo76T 1 Reply Last reply
                                      1
                                      • SGaistS SGaist

                                        Can you show the complete code of the current version of your dialog ?

                                        TheCipo76T Offline
                                        TheCipo76T Offline
                                        TheCipo76
                                        wrote on last edited by
                                        #20

                                        @SGaist

                                        #include "impostazionisp.h"
                                        #include "ui_impostazionisp.h"
                                        #include <QSerialPort>
                                        #include <QSerialPortInfo>
                                        #include <QMessageBox>
                                        #include <QtDebug>
                                        
                                        static const char blankString[] = QT_TRANSLATE_NOOP("SettingsDialog", "N/A");
                                        
                                        //QSerialPort Serial;
                                        
                                        ImpostazioniSP::ImpostazioniSP(QWidget *parent, const QString BaudRate, const QString DataBits, const QString Parity) :
                                            QDialog(parent),
                                            BaudRate(BaudRate),
                                            DataBits(DataBits),
                                            Parity(Parity),
                                            ui(new Ui::ImpostazioniSP)
                                        
                                        {
                                            ui->setupUi(this);
                                        
                                            QMessageBox msg;
                                            ui->comboBox_Porta->clear();
                                            QString description;
                                            QString manufacturer;
                                            QString serialNumber;
                                            const auto infos = QSerialPortInfo::availablePorts();
                                            for (const QSerialPortInfo &info : infos) {
                                                QStringList list;
                                                description = info.description();
                                                manufacturer = info.manufacturer();
                                                serialNumber = info.serialNumber();
                                                list << info.portName()
                                                     << (!description.isEmpty() ? description : blankString)
                                                     << (!manufacturer.isEmpty() ? manufacturer : blankString)
                                                     << (!serialNumber.isEmpty() ? serialNumber : blankString)
                                                     << info.systemLocation()
                                                     << (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : blankString)
                                                     << (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : blankString);
                                        
                                                ui->comboBox_Porta->addItem(list.first(), list);
                                            }
                                            
                                            //Baud Rate
                                            ui->comboBox_BaudRate->addItem("9600");
                                            ui->comboBox_BaudRate->addItem("19200");
                                            ui->comboBox_BaudRate->addItem("38400");
                                            ui->comboBox_BaudRate->addItem("115200");
                                            ui->comboBox_BaudRate->setCurrentIndex(0);
                                            //Data Bits
                                            ui->comboBox_DataBits->addItem("5");
                                            ui->comboBox_DataBits->addItem("6");
                                            ui->comboBox_DataBits->addItem("7");
                                            ui->comboBox_DataBits->addItem("8");
                                             ui->comboBox_DataBits->setCurrentIndex(3);
                                            //Parity
                                            ui->comboBox_Parity->addItem("None");
                                            ui->comboBox_Parity->addItem("Even");
                                            ui->comboBox_Parity->addItem("Odd");
                                            ui->comboBox_Parity->addItem("Mark");
                                            ui->comboBox_Parity->addItem("Space");
                                            ui->comboBox_Parity->setCurrentIndex(0);
                                            //Stop Bits
                                            ui->comboBox_StopBits->addItem("1");
                                            ui->comboBox_StopBits->addItem("1.5");
                                            ui->comboBox_StopBits->addItem("2");
                                            ui->comboBox_StopBits->setCurrentIndex(0);
                                            //Flow Control
                                            ui->comboBox_FlowControl->addItem("None");
                                            ui->comboBox_FlowControl->addItem("RTS/CTS");
                                            ui->comboBox_FlowControl->addItem("XON/XOFF");
                                            ui->comboBox_FlowControl->setCurrentIndex(0);
                                        }
                                        
                                        ImpostazioniSP::~ImpostazioniSP()
                                        {
                                            delete ui;
                                        }
                                        
                                        void ImpostazioniSP::on_pushButton_Annulla_clicked()
                                        {
                                            close();
                                        }
                                        
                                        void ImpostazioniSP::on_pushButton_OK_clicked()
                                        {
                                            QMessageBox msg;
                                            //Imposta Valori Variabili
                                            // Baud Rate
                                            int Baud = ui->comboBox_BaudRate->currentText().toInt();
                                            switch (Baud) {
                                                case (9600):
                                                            BaudRate = "9600";
                                                            break;
                                                case (19200):
                                                            BaudRate ="19200";
                                                            break;
                                                case (38400):
                                                            BaudRate = "38400";
                                                            break;
                                                case (115200):
                                                            BaudRate = "115200";
                                                            break;
                                                default:
                                                            BaudRate = "9600";
                                            }
                                            getBaudRate();
                                            // Data Bits
                                            int Bits = ui->comboBox_DataBits->currentText().toInt();
                                            switch (Bits) {
                                                case (5) :
                                                        DataBits = "Data5";
                                                        break;
                                                case (6) :
                                                        DataBits="Data6";
                                                        break;
                                                case (7) :
                                                        DataBits="Data7";
                                                        break;
                                                case (8) :
                                                         DataBits="Data8";
                                                        break;
                                                default:
                                                        DataBits="Data8";
                                            }
                                            // Parity
                                            if (ui->comboBox_Parity->currentText() == "None") Parity="NoParity";
                                            if (ui->comboBox_Parity->currentText() == "Even") Parity="EvenParity";
                                            if (ui->comboBox_Parity->currentText() == "Odd") Parity="OddParity";
                                            if (ui->comboBox_Parity->currentText() == "Mark") Parity="MarkParity";
                                            if (ui->comboBox_Parity->currentText() == "Space") Parity="SpaceParity";
                                            msg.setText("OK: Settaggio Impostazioni Eseguito Correttamente");
                                            msg.exec();
                                            close();
                                        }
                                        
                                        QString ImpostazioniSP::getBaudRate() {
                                            qDebug() << "BaudRate:" << BaudRate;
                                            return BaudRate;
                                        }
                                        /*
                                        QString ImpostazioniSP::getDataBits() {
                                        return DataBits;
                                        }
                                        
                                        QString ImpostazioniSP::getParity() {
                                        return Parity;
                                        }
                                        */
                                        
                                        
                                        1 Reply Last reply
                                        0
                                        • mrjjM Offline
                                          mrjjM Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #21

                                          Hi
                                          Its a bit odd as you seem to set BaudRate in
                                          void ImpostazioniSP::on_pushButton_OK_clicked()
                                          so in
                                          QString ImpostazioniSP::getBaudRate() {
                                          qDebug() << "BaudRate:" << BaudRate;
                                          return BaudRate;
                                          }

                                          does it show the value you set with comboBox_BaudRate ?

                                          TheCipo76T 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