PDA

View Full Version : How to call a method of extended QApplication with QMetaObject::invokeMethod?



fanoI
8th May 2013, 12:08
I'm trying to use QMetaObject::invokeMethod() in a dynamic library if I do this:


if (QMetaObject::invokeMethod(qApp, "callMe", Qt::DirectConnection,
Q_ARG(QString, "Test")))

I could successfully call the method callMe of the QApplication, but if I create a class that extends QApplication addiing another method I cannot call it, as qApp continue to point to the class the does run() I suppose..

One possible soluction would be to use dynamic cast, but I cannot as the library shouldn't know the classes of the main application... there's a way to find the pointer to this class? I've tried to give a name, but I cannot find a method to find an object by name (I can find its children, but he is the "Grand Dad" of all), I've tried to loop on parent, but the recursion stopped... and effectively the QMainWindow has not as parent qApp... I've tried to use setParent() but the compiler doesn't liked it (it said something as QObject is not a QWidget???)...

So how find this object?

I've tried to create a slot and a signal and call emit in my callback... but it says that it doesn't know what is (it wants the header, but this defeat the purpose!).

Ideas? I've no more...

Thanks

anda_skoa
8th May 2013, 13:59
Just checking a few things:
1) You've created an instance of your QApplication class, e.g. check qApp->metaObject()->className()
2) Your new method is either in a Q_SLOTS section or a Q_INVOKABLE

Cheers,
_

fanoI
8th May 2013, 19:23
I'll try to explain better what is my problem, the application is composed by these three layers:


BrowserApp generic class that extended QApplication embedding a simple Web Kit browser
libcallback in which are implemented C++ callback called via the "magic" JavaScript / Qt hybrid. This library operates only on HTML fields and don't have to know the classes of the Application! It used to check the validity of the data written on forms.
The main the creates a new BrowserApp and calls its exec() method. The main(), for now could be considered a C application...


To communicate with the rest of the applications I need to implement 2 slots in the main application Read() and Write() in which depending on the application I could read/write on a Socket, use RPC, pipe, real time queues or our special communication mechanism.

Until now I've not needed to use this communication system on the callback library but now to check if a data in a field is correct I need to make a query in the DB and get the result and I don't want to give this knowing to it (the library should be used by other applications that link this libraries but have a different "main") so I thought to use InvokeMethod() to call a function written in the main() (as I don't want to modify BrowserApp it needs to remain generic for the same motive) and to do this I have thought extended BrowserApp so in the end I have a third extension:

MyApp --> BrowserApp --> QApplication (!)

and I've created a simple method declared with the QINVOKABLE attribute but using the qApp pointer I cannot find the method in MyApp, but I can find the methods in BrowserApp, why? Maybe I needed to inherit form QApplication, too? It seems strange to me in the end it inherit from it... implicitly!

In the end the KLUDGE that I implemented:


Added a new generic slot that the main should implement that did a Write() and Read() the response. It takes a json as argument that will be modified with the result
emitted the signal from JavaScript (sadly to say but it is really more simple. JavaScript has the 'pointer' to the application without the need to extend classes calling ugly functions and so on... I simple write ifObj->myFunction(json) !)
The main implements this slot, in the json there's a string that indicates what function he has to call, the rest of the json are the args...


To note that as, in the end, the function is in BrowserApp now I could use InvokeMethod() now, but in the end JavaScript seems more simple that is strange... calling a C++ from JavaScript more easy that from javascipt?

But I continue to not understand why Qt doesn't like to call my functions using InvokeMethod :crying:

anda_skoa
9th May 2013, 15:37
Your MyApp class declarations did have the Q_OBJECT marker, right?
And you did create and instance of MyApp and not just of BrowserApp?

Cheers,
_