Help sending data from dialog to main window
-
Hello everyone, am new to the forum.
Ok so I have two Windows in my project, main window and a dialog window called login. This login window just have a line edit called username and a push button called loginbtn. What I want is to have a QString variable called username in the main window that is equal to an empty string, so when I provide a username in the dialog window and click the login button, the text in the line edit get assigned to the empty username variable in the main window. Thank you. -
@Cantona
Provide a public getter and a setter for the text in the line edit in the dialog (subclassQDialog
). Include the dialog declaration via header file into main window, so it knows about it. Optionally set the value via setter to something (e.g. empty string) prior to invokingQDialog::exec()
. Upon return from that, check result to see whether user went "OK" or "Cancel". If "OK" assign to variable in main window via getter in dialog.In other words, just use public setters & getters to access text value in dialog from main window.
You could also use signal from dialog to pass username to slot in main window, but I don't see a need here, unless you prefer that to getter/setter. Getter/setter can be expanded more easily, e.g. if you decide to pass a password back as well as a username.
If this is a "play" project you could just use QInputDialog::getText() to get a single string returned from a dialog instead. But that is limited to just that.
-
@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.