Signal and slot not working properly
-
Hey,
I have created a signal and slot that I send data from the second window to the main window
whenever the user clicks the button
But I not sending the signal
Here is the signal in is in main windowLogin log; connect(&log,SIGNAL(sendinfo(QString)),this,SLOT(recieveinfo(QString)));
receiveinfo function this also exist in main window
void Mainwindow::recieveinfo(QString username) { name=username; }
slot in mainwindow.h
public slots: void recieveinfo(QString name);
signal in second window.cpp
signals: void sendinfo(QString name);
sendinfo emit from push button
emit sendinfo(ui->name->text());
The string name doesn't change means there is a problem in signal and slots
Please help me
Thanks in advance -
@UG-SEP
Start withqDebug() << ui->name->text()
just above theemit
line andqDebug() << username
as first line inMainwindow::recieveinfo()
. Does the slot actually get called?It is confusing that you have
recieveinfo(QString name)
in the header declaration butMainwindow::recieveinfo(QString username)
in the definition. Keep those parameter names the same.In the longer run it is better to pass
const QString &
parameters thanQString
, but that won't be the problem here.And finally do yourself a favor and change over to New Signal Slot Syntax, it will help you in the long run.
-
@UG-SEP said in Signal and slot not working properly:
Login log;
connect(&log,SIGNAL(sendinfo(QString)),this,SLOT(recieveinfo(QString)));this seems like a lifetime problem.
I would bet, that log is beeing destroyed sometime later, at the ‚}
-
Hi
Killed by scope +1void buttonclick() { Login log; connect(&log,SIGNAL(sendinfo(QString)),this,SLOT(recieveinfo(QString))); } // log will be deleted here so sendinfo will never trigger make Login log; part of the "second window" as a member, not as a local variable