Pass variable value from Dialog to MainWindow
-
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??
-
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??
@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.
-
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??
@TheCipo76 said in Pass variable value from Dialog to MainWindow:
and a dialog (modal)
It depends a bit. Do you use
exec()
orshow()
for your dialog? Asexec()
blocks the main window anyway, signals&slots are no good solution. Forshow()
they are perfect. -
@TheCipo76 said in Pass variable value from Dialog to MainWindow:
and a dialog (modal)
It depends a bit. Do you use
exec()
orshow()
for your dialog? Asexec()
blocks the main window anyway, signals&slots are no good solution. Forshow()
they are perfect. -
@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??
@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 ;)
-
@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();
....
}}
-
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 ) -
@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??
-
@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?
-
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?
-
@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?
-
Do you mean the MainWindow still shows the old value ? If so, are you updating the related widget once you executed your dialog ?
-
Do you mean the MainWindow still shows the old value ? If so, are you updating the related widget once you executed your dialog ?
-
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 ?
-
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 ?
-
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; } */