jxgn
jxgn

Reputation: 811

Is there a way trigger a signal from another signal in Qt?

In my application, I am introducing a new signal, which has to be emitted when another signal is emitted. Is this possible in Qt?

Writing a slot just to emit this signal feels so primitive and lame.

I have to connect the button signalClicked() to my own signal, say sendSignal(enumtype), and send data with the second signal.

Upvotes: 17

Views: 19678

Answers (1)

Jablonski
Jablonski

Reputation: 18524

Yes, it is possible without creating additional slot. Just connect signal to signal:

connect(this,SIGNAL(someSignal()),this,SIGNAL(anotherSignal()));

More information in doc.

You can share data in connection as usual. Dirty example:

QWidget* obj = new QWidget;
obj->setWindowTitle("WindowTitle");
//share data, pass wrong data to the signal
QObject::connect(obj,SIGNAL(objectNameChanged(QString)),obj,SIGNAL(windowTitleChanged(QString)));
QObject::connect(obj,&QWidget::windowTitleChanged,[](QString str) {qDebug() << str;});
obj->setObjectName("ObjectName");
qDebug() << "But window title is"<< obj->windowTitle();
obj->show();

Output is:

"ObjectName" 
But window title is "WindowTitle" 

But there is no way to do something like:

connect(this,SIGNAL(someSignal()),this,SIGNAL(anotherSignal("with custom data")));

In this case, you need a separate slot.

Upvotes: 26

Related Questions