How to pass qobject as argument from signal to slot in qt connect

My source code passed a QStringList from the signal to the slot, and then returned a QList. Everything worked fine, but I needed to change the QStringList and QList to 2 different subclasses of QObjects. Since then, I get errors, such as the "synthesized method first required here", or just crashing without an error message.

I understand that qt copies all the arguments passed in the queue, and qobject cannot be copied. Therefore, instead of returning a qobject, I thought that I would create both q objects before the signal was issued. Then I will pass links to each object, change one of them in the slot function and invalidate the return value. Unfortunately, the application is still crashing, no matter how I code the signal and slot functions. How can I encode signal / slot functions and connect them to either pass both q objects as arguments or return qobject?

MyQObject1 obj1("a","b","c"); MyQObject2 obj2(); emit sendSignal(&obj1, &obj2); // or MyQObject2 obj2 = emit sendSignal(&obj1); connect(someObj, SIGNAL(sendSignal(const QObject&)), this, SLOT(receiveSignal(const QObject&))); 

The getSignal () function does not directly create or modify any qobject. It must pass qobjects to another function, which then either modifies obj2 or creates and returns it. Any code examples are greatly appreciated.

+4
source share
1 answer

Typically, a QObject is passed in with a pointer, not a reference (note that a QObject cannot be copied and cannot be passed by value). QObject* is registered as a meta type by default. Therefore, creating a signal and a slot with the QObject* argument is enough to make them work:

 private slots: void test_slot(QObject* object); signals: void test_signal(QObject* object); 

Initialization:

 connect(this, SIGNAL(test_signal(QObject*)), this, SLOT(test_slot(QObject*))); 

Emmitting:

 QObject* object = new QObject(); emit test_signal(object); 

Of course, the signal and slot can be in different classes.

+9
source

Source: https://habr.com/ru/post/1495025/


All Articles