PDA

View Full Version : Sending a signal & openinig a new window



Akame.L
13th March 2017, 19:06
Hi.
I have a (login) window and an (employe) window.
I used signals & slots to send one variable from the first window to the seconde
What I want is when I click the login button the value of the username should be sent to employe window and open it on the same time.
here's what I've done:

login.h


...
signals:
void notifyValueSentToEmp(const QString username);
...


login.cpp


...
void Login::on_pushButton_Login_clicked(){
...
employe e;
emit notifyValueSentToEmp(username);
e.show();
...
}

When I use (e.show()) in login.cpp the variable is not sent

employe.h


...
private slots:
void onValueSentToEmp(const QString usename);
...


employe.cpp


...
void employe::onValueSentToEmp(QString username){
// Here where I use the sent variable username
}
...


main.cpp


Login l;
employe e;
QObject::connect(&l,SIGNAL(notifyValueSentToEmp(QString)),&e,SLOT(onValueSentToEmp(QString)));
l.show();
e.show();

When I use (e.show()) in the main the value is sent but the second window appears in the same time with first window.

I hope you can help fix this.
thank u

d_stranz
14th March 2017, 04:03
void Login::on_pushButton_Login_clicked(){
...
employe e;
emit notifyValueSentToEmp(username);
e.show();
...
}

In this code, you are creating and showing a new employee window (on the stack). It is not the same instance of employee that you created in main() and which you hooked up to the signal. As soon as this on_pushButton_Login_clicked() slot exits, the new instance is destroyed. If you don't want this second window, then remove the lines of code that create and then show it.

Akame.L
14th March 2017, 09:57
I did not put both of them.
I'm using e.show in the main.cpp where I put the connect()
I was just trying to say that if I want the second window to appear after clicking the button than I should put the e.show() in the on_pushButton_Login_clicked(). But I can't use it in connect() in the main.
When I put it in the main with the connect it works but I have to show the two windows at the same time. And I don't want that.
Thank u

Lesiok
14th March 2017, 10:14
Something like this :
main.cpp
Login l;
employe e;
QObject::connect(&l,SIGNAL(notifyValueSentToEmp(QString)),&e,SLOT(onValueSentToEmp(QString)));
l.show();
login.cpp
void Login::on_pushButton_Login_clicked(){
...
emit notifyValueSentToEmp(username);
...
}
employe.cpp
void employe::onValueSentToEmp(QString username){
// Here where I use the sent variable username
show();
}