Solved Pass variable value from Dialog to MainWindow
-
@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
-
Do you mean the MainWindow still shows the old value ? If so, are you updating the related widget once you executed your dialog ?
-
@SGaist there's no widget BaudRate is only a variable (not showed in mainwindow)
-
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 ?
-
@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 -
Can you show the complete code of the current version of your dialog ?
-
#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; } */
-
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 ?
-
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.
-
@mrjj Yes:
in dialog qdebug print BaudRate 9600
in mainwindow qdebug print 115200 both Before and After -
@SGaist OK, i've fixed it as you suggested
-
@TheCipo76
Hmm that seems almost impossible :)
I must be missing something.
So function seems to return expected result.
Just to 100% sure, please tryqDebug() << "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 ?
-
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" -
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 ?
-
@SGaist i close the dialog as ultimate instruction of OK pushbutton
-
You're calling close, not accept.
-
@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???
-
@SGaist OK, finally works!!! Thanks to all
-
There's no need to call close, accept will close the dialog for you.
Likely because you are calling
getBaudRate
in several places. -
Ah, i was fooled by
qDebug() << "After" << BaudRate;
as its inside the if for Accepted so i assumed that was in order :)
Good found!