12

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.

jxgn
  • 741
  • 2
  • 15
  • 36

1 Answers1

21

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.

Jablonski
  • 18,083
  • 2
  • 46
  • 47
  • Thanks for the answer. I forgot to add a detail. Please see my edit. – jxgn Oct 07 '15 at 13:20
  • 1
    something like `connect(this,SIGNAL(someSignal()),this,SIGNAL(anotherSignal("with custom data")));` can be accomplished using `QSignalMapper` – Nicolas Holthaus Oct 07 '15 at 14:24
  • 2
    QSignalMapper is obsolete, but its doc shows a way with lambda: `connect(button, &QPushButton::clicked, [=] { clicked(text); });` https://doc.qt.io/qt-5/qsignalmapper.html#details – user1712200 Aug 19 '19 at 11:37