Solved Pass variable value from Dialog to MainWindow
-
@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 ;)
-
@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); ... }
-
Hi
So one way would bevoid 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();
....
}}
-
@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" -
@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 ) -
@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??
-
Where i'm failing??
Without an error message, how should we know what "doesn't work" means?
-
@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; }
-
@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?
-
@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