Help sending data from dialog to main window
-
@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.