Results 1 to 4 of 4

Thread: How to call a method of extended QApplication with QMetaObject::invokeMethod?

  1. #1
    Join Date
    Apr 2013
    Location
    milan
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to call a method of extended QApplication with QMetaObject::invokeMethod?

    I'm trying to use QMetaObject::invokeMethod() in a dynamic library if I do this:


    if (QMetaObject::invokeMethod(qApp, "callMe", Qt:irectConnection,
    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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to call a method of extended QApplication with QMetaObject::invokeMethod?

    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,
    _

  3. #3
    Join Date
    Apr 2013
    Location
    milan
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to call a method of extended QApplication with QMetaObject::invokeMethod?

    I'll try to explain better what is my problem, the application is composed by these three layers:

    1. BrowserApp generic class that extended QApplication embedding a simple Web Kit browser
    2. 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.
    3. 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:

    1. 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
    2. 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) !)
    3. 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

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to call a method of extended QApplication with QMetaObject::invokeMethod?

    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,
    _

Similar Threads

  1. Problems with QMetaObject::invokeMethod
    By AlphaWolf in forum Qt Programming
    Replies: 3
    Last Post: 13th September 2012, 11:57
  2. Closing dialog by QMetaObject::invokeMethod - safe?
    By Blood9999 in forum Qt Programming
    Replies: 7
    Last Post: 16th August 2012, 12:48
  3. Replies: 0
    Last Post: 19th January 2012, 15:52
  4. How to use QMetaObject::invokeMethod?
    By niko in forum Qt Programming
    Replies: 3
    Last Post: 26th January 2008, 16:02
  5. Using QMetaObject::invokeMethod() doesn't work
    By bruccutler in forum Qt Programming
    Replies: 2
    Last Post: 21st February 2007, 22:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.