Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 405
Xavier Geoffrey Avatar asked Oct 07 '15 13:10

Xavier Geoffrey


People also ask

Can a signal be connected to another signal Qt?

This ensures that truly independent components can be created with Qt. You can connect as many signals as you want to a single slot, and a signal can be connected to as many slots as you need. It is even possible to connect a signal directly to another signal.

Are Qt signals synchronous?

Signals are neither synchronous nor asynchronous. It's the slots that might be one of the two. Slots are by default synchronous when the object emitting the signal and the object having the receiving slot both live in the same thread.

What is QObject :: connect?

[static] QMetaObject::Connection QObject::connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type = Qt::AutoConnection) Creates a connection of the given type from the signal in the sender object to the method in the receiver object.


1 Answers

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.

like image 90
Kosovan Avatar answered Sep 27 '22 16:09

Kosovan