Reputation: 33
Why didn't the button object get the sigKK() signal when the button was clicked?
When a signal is emitted, can all qt objects receive this signal?
The code is as follows:
class PushButton : public QPushButton
{
Q_OBJECT
signals:
void sigKK();
};
The PushButton class inherits from QPushButton, but doesn't connect signals and slots here. Is this right?
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(){
resize(400,200);
connect(this,SIGNAL(sigKK()),this,SLOT(showRecv1()));
button = new PushButton();
button->setText("Hello,All");
button->resize(40,15);
connect(button, SIGNAL(clicked()),this,SLOT(buttonCK()));
connect(button, SIGNAL(sigKK()),this,SLOT(showRecv2()));
//**I can connect sigKK signal with showRecv2 slot here ?****
button->show();
}
~MainWindow(){
}
signals:
void sigKK();
public slots:
void showRecv1(){
cout<<"recved 1"<<endl;
resize(100,100);
}
void showRecv2(){
cout<<"recved 2"<<endl;
button->setText(".....");
}
void buttonCK(){
emit sigKK();
cout<<"emited"<<endl;
}
private:
PushButton *button ;
};
#endif
Upvotes: 2
Views: 2176
Reputation: 4424
When a signal is emitted, can all qt objects receive this signal ?
No. When a signal is emitted it is received only by QObjects with signals or slots connected to it.
Your MainWindow and your PushButton both have a signal with the same name... but they are still different signals. They are completely unrelated to each other. When MainWindow emits sigKK
, that has no effect on PushButton's sigKK
.
In your example, sigKK
seems to be entirely unneccessary. Perhaps you could instead connect clicked()
directly to the actions you want to perform?
connect(button, SIGNAL(clicked()),this,SLOT(showRecv1()));
connect(button, SIGNAL(clicked()),this,SLOT(showRecv2()));
Upvotes: 1