Using signal and slots to send data to another window
-
Hi. I am currently working on a small QT project for my university project. Now, I just need to send a signal to another window from the current window. Basically, I need to send the "username" as a QString to another window every time I press the "login" button in my login window. Would it be possible to do that?
I know it has something to do with Signals and Slots, but I couldn't do it myself since I am not familiar with the syntax. Appreciate the help.
-
@burrito1111
Although it is possible to do this with signals & slots, the more usual way would not. It would just have a aQDialog
for the "login window", withTextEdit
s for username & password, and an "OK"/"Login" button. The dialog would be run as "modal" viaQDialog::exec()
. When it exits via the user pressing "OK", the "calling" code would just access the username/password from the dialog instance (provide methods for that). -
@burrito1111 said in Using signal and slots to send data to another window:
theyre part of the same project. two main windows
So, same process with two main windows?
Then it is easy: simply connect a signal from one window to a slot in the other.
See https://doc.qt.io/qt-5/signalsandslots.html
Something like:LoginWindow *logWindow = new LoginWindow(); connect(logWindow, &LoginWindow::login, this, &MainWindow::doLogin);
-
@burrito1111
In that case since the login window is not a modal dialog you will need to attach a slot to theQPushButton::clicked
signal of the login window. You can either subclass so that you can create a new signal which passes the username/password, or you can have the slot in the other window access the username from the login window by it have a public "getter" method to call.Incidentally, since your "logon" window is not a modal dialog, what happens/is to stop user from logging on more than once? And how does the receiving window behave before the user has used the other window to login?