Store variables from qdialog for use in qmainwindow

I created a dialog dialog.h, dialog.cpp and dialog.ui, and I have qlineedit in the dialog box, as well as an ok and cancel button, and I want to save this linedit information for use in mainwindow in another file. here is my dialogue code.

#include <QtGui/QApplication> #include "dialog.h" #include "ui_dialog.h" void Dialog::startplanevolume() { if (xMax==0) { ui->label_17->setText("Error: Can't start, invalid \nmeasures"); } else { this->accept(); } } // Define the length of the volume void Dialog::bmprange() { // Getting some proprieties for the lenght of the volume QString XMAX=ui->lineEdit->text(); xMax=XMAX.toDouble(); if (xMax==0) { ui->label_17->setText("Error: invalid measures"); } else { ui->label_17->setText("Valid measures"); } } Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); // Control volume measures // Making the lineedit objects only accept numbers ui->lineEdit->setValidator(new QIntValidator(this)); connect(ui->lineEdit, SIGNAL(textChanged(QString)), this, SLOT(bmprange())); // Start planevolume connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(startplanevolume())); connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(hide())); } Dialog::~Dialog() { delete ui; } void Dialog::changeEvent(QEvent *e) { QDialog::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; } } 

how can i use xMax value in mainwindow.cpp ??

here is my dialogue .h

 #ifndef DIALOG_H #define DIALOG_H #include <QDialog> namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: Dialog(QWidget *parent = 0); ~Dialog(); protected: void changeEvent(QEvent *e); private: Ui::Dialog *ui; double xMax, yMax, zMax, xMMax, yMMax, zMMax, list[6]; public slots: void bmprange(); void startplanevolume(); }; #endif // DIALOG_H 

here my main.cpp

 #include <QtGui/QApplication> #include "planevolume.h" #include "dialog.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); Dialog *dialog= new Dialog; if (dialog->exec()) { planevolume mainwindow; mainwindow.show(); return app.exec(); } return 0; } 

so I want to use xMax to calculate something in planevolume.cpp, mainwindow

+4
source share
2 answers

I think you can create a getter function in a dialog box. After closing the dialog box, you can access the variable using the created getter function.

A return variable that you can specify as a parameter for mainwindow (planvolume.cpp).

I hope this helps.

Edit:

In dialog.h / dialog.cpp you add the function:

 double Dialog::getXMax() { return xMax; } 

After that, you can access the variable in the main.cpp file:

 Dialog *dialog= new Dialog; if (dialog->exec()) { double xMax = dialog->getXMax(); planevolume mainwindow; mainwindow.show(); return app.exec(); } 
+1
source

Although you can declare xMax as an open Dialog member and just use it, a more elegant and stylish approach is to write a "getter" function declared in the public scope of your Dialog class

 (...) public: Dialog(QWidget *parent = 0); ~Dialog(); double getxMax() { return xMax; } (...) 

You will also need the setter function in your planevolume class, also declared in the public scope, as:

 void setxMax(double xMax); 

The body of this function will take care of doing everything necessary with the value xMax .

Finally, in your main() function, you should do the following:

 if (dialog->exec()) { planevolume mainwindow; mainwindow.setxMax(dialog->getxMax()); // This passes xMax to main window mainwindow.show(); return app.exec(); } 

Alternatively, you can simply overload the show() planevolume as

 planevolume::show(double xMax) { // Do whatever you want with XMax show(); // And call original show function } 

in this case you do not need the setter function and just call

 mainwindow.show(dialog->getxMax()); 
+1
source

All Articles