Help sending data from dialog to main window
-
@Cantona
You have used it many times, Qt uses this pattern for all its class variables. It's just simple C++ (or Python):// .h file class UsernameDialog : public QDialog { public: const QString &username() const; // "getter" for username void setUsername(const QString &username); // "setter" for username, probably not used for this case } // .cpp file const QString &UsernameDialog::username() const { return ui->lineEdit.text(); } void UsernameDialog::setUsername(const QString &username) { ui->lineEdit.setText(username); } // mainwindow cpp file QString username; UsernameDialog *usernameDialog = new UsernameDialog; if (usernameDialog->exec()) username = usernameDialog->username();
-
Hi,
What exactly did not work now ?
-
@Cantona said in Help sending data from dialog to main window:
variable username still is not getting any data assigned to it
Unpleasant answer but no secret or surprise:
You need to know C++ if you want to get somewhere with Qt :)
Learn the basics of C++ and OOP and you will understand better and learn faster how to work with Qt.@JonB said in Help sending data from dialog to main window:
// mainwindow cpp file QString username; UsernameDialog *usernameDialog = new UsernameDialog; if (usernameDialog->exec()) username = usernameDialog->username();
This answer by @JonB is basically everything you need to do to return your variable from your dialog and assign it to something within your
QMainWindow
class. -
@Cantona You can achieve this by emitting a signal from the login dialog and connecting it to a slot in the main window. In LoginDialog, emit a signal like emit sendUsername(username); when the button is clicked. Then, in MainWindow, connect it with connect(loginDialog, &LoginDialog::sendUsername, this, &MainWindow::setUsername);.
Hope these steps help you out!
-
@alexjordan_now said in Help sending data from dialog to main window:
You can achieve this by emitting a signal from the login dialog and connecting it to a slot in the main window
That's one way that was also already mentioned before :)
Though I think if you want to return one value only, setting up a signal/slot connection is more "work" than just returning the value directly to the caller.