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.3k 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.
  • 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
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #22

                  Why are you using that switch ? You already have the baud rate as a string in the combo box.

                  As already suggest, returning the combo box selected text is shorter and less error prone.

                  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
                  2
                  • mrjjM mrjj

                    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 Offline
                    TheCipo76T Offline
                    TheCipo76
                    wrote on last edited by
                    #23

                    @mrjj Yes:
                    in dialog qdebug print BaudRate 9600
                    in mainwindow qdebug print 115200 both Before and After

                    mrjjM 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Why are you using that switch ? You already have the baud rate as a string in the combo box.

                      As already suggest, returning the combo box selected text is shorter and less error prone.

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

                      @SGaist OK, i've fixed it as you suggested

                      1 Reply Last reply
                      0
                      • TheCipo76T TheCipo76

                        @mrjj Yes:
                        in dialog qdebug print BaudRate 9600
                        in mainwindow qdebug print 115200 both Before and After

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

                        @TheCipo76
                        Hmm that seems almost impossible :)
                        I must be missing something.
                        So function seems to return expected result.
                        Just to 100% sure, please try

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

                        and tell what "from func" says ?

                        TheCipo76T 1 Reply Last reply
                        0
                        • mrjjM mrjj

                          @TheCipo76
                          Hmm that seems almost impossible :)
                          I must be missing something.
                          So function seems to return expected result.
                          Just to 100% sure, please try

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

                          and tell what "from func" says ?

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

                          @mrjj

                          Before "115200" (mainwindow)
                          BaudRate: "9600" (dialog)

                          "from func" was not showed

                          i've modify you code:

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

                          and this is result:

                          Before "115200"
                          BaudRate: "9600"
                          BaudRate: "9600"
                          from func "9600"

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

                            What is stranger is that @mrjj's code is mostly the same as mine :-D

                            Which means that you didn't accept the dialog properly. Are you clicking on some OK button the closes it ? Or are you just clicking the X button ?

                            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
                            2
                            • SGaistS SGaist

                              What is stranger is that @mrjj's code is mostly the same as mine :-D

                              Which means that you didn't accept the dialog properly. Are you clicking on some OK button the closes it ? Or are you just clicking the X button ?

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

                              @SGaist i close the dialog as ultimate instruction of OK pushbutton

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

                                You're calling close, not accept.

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

                                TheCipo76T 2 Replies Last reply
                                4
                                • SGaistS SGaist

                                  You're calling close, not accept.

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

                                  @SGaist i have called accept before close

                                  this is qdebug message:

                                  Before "115200"
                                  BaudRate: "9600"
                                  BaudRate: "9600"
                                  BaudRate: "9600"
                                  from func "9600"

                                  why show BaudRate: "9600" 3 times???

                                  1 Reply Last reply
                                  0
                                  • SGaistS SGaist

                                    You're calling close, not accept.

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

                                    @SGaist OK, finally works!!! Thanks to all

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

                                      There's no need to call close, accept will close the dialog for you.

                                      Likely because you are calling getBaudRate in several places.

                                      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
                                      • mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #33

                                        Ah, i was fooled by
                                        qDebug() << "After" << BaudRate;
                                        as its inside the if for Accepted so i assumed that was in order :)
                                        Good found!

                                        1 Reply Last reply
                                        0
                                        • TheCipo76T Offline
                                          TheCipo76T Offline
                                          TheCipo76
                                          wrote on last edited by
                                          #34

                                          Thanks to all!!

                                          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