How to pass custom class reference to QMetaObject::invokeMethod
-
Hey folks, I hope you all are doing well.
I stuck in a scenario on which I have created a class reference but not able to handle it properly and getting errors like No such method of such type, unregistered data type, etc..
Can you help me get over this issue as I am sharing the code scenario :
I am having a class ABC for which I have created its reference here :
ABC* child;
As I am calling this function from some another class :
QMetaObject::invokeMethod(this, "demo", Qt::QueuedConnection, Q_ARG(ABC*, obj), Q_ARG(bool, true))
Method :
void demo(ABC* ref, bool var) { ...}
-
@Asperamanca Thanks for replying but even using the Q_DECLARE_METATYPE I am getting this : QMetaObject::invokeMethod: No such method demo(ABC*).
Or maybe I am using it in a wrong way as I am new to this can you please help me out with this.
-
@SJoshi said in How to pass custom class reference to QMetaObject::invokeMethod:
for which I have created its reference here
You created a pointer, not reference.
Where is this demo() method actually defined?
Because you have to pass pointer to class instance to invokeMethod() where demo() is defined. -
@jsulm Hi I am sorry that I used reference word, actually, demo() is on some another class I didn't mention it on here and it's a public slot too. Also, I have passed the actual object of the class where the demo is put instead of this. But I don't know where I am doing the mistake.
-
@jsulm Sure actually it's a bit complex code so I am just sharing the main logic with you :
Q_DECLARE_METATYPE(ABC*)
void MainWindow::senData() { ABC* temp; some logic applied on temp QMetaObject::invokeMethod(pv, "demo", Qt::QueuedConnection, Q_ARG(ABC*, temp), Q_ARG(bool, false)) ... }
In the above logic PV is an object pageView class and created as: pageView* pv = new PageView().
void pageView::demo(ABC* obj, bool handle) { ... }
Error : QMetaMethod::invoke: Unable to handle unregistered datatype 'ABC*
Please let me know If I am missing something.
-
This post is deleted!
-
@Asperamanca Yes I declared Q_DECLARE_METATYPE just after the include statements and I'm not using any namespaces.
-
You already have the answer at hand
@SJoshi said in How to pass custom class reference to QMetaObject::invokeMethod:
Unable to handle unregistered datatype 'ABC*
@Asperamanca said in How to pass custom class reference to QMetaObject::invokeMethod:
You need to register the class using [...] qRegisterMetaType()
-
@SJoshi said in How to pass custom class reference to QMetaObject::invokeMethod:
For now, I have used it on the very first line of MainWindow::senData() method.
There is no need to re-register it every time. The ctor of this class is fine.
-
@Christian-Ehrlicher Thank You!