7
votes

I already have an application in place and am tweaking it now. In this regard, 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...

Detailing further, I have to connect the button signalClicked() to my own signal say sendSignal(enumtype)...

EDIT: Forgot to mention that I need to send a data with the second signal.

1
Just connect one signal to another: connect(SIGNAL(signal1()), SIGNAL(signal2());vahancho

1 Answers

17
votes

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

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

More information in doc.

Edit:

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.