Results 1 to 2 of 2

Thread: QMetaObject::InvokeMethod returns true but method is never invoked

  1. #1
    Join Date
    Oct 2009
    Posts
    29
    Thanks
    4

    Default QMetaObject::InvokeMethod returns true but method is never invoked

    I am having problems trying to invoke a method from an object running in another thread.

    I have a simple gui application that needs to be wrapped into a DLL for use with other software.
    I am using visual studio 2013 with the qt plugin for developement of the DLL.

    i use the DLLMain function to start the messageloop for the gui app with the following code:

    Qt Code:
    1. static HANDLE hThread;
    2.  
    3. BOOL APIENTRY DllMain(HINSTANCE hInstance, DWORD fdwReason, LPVOID lpReserved)
    4. {
    5. UNREFERENCED_PARAMETER(lpReserved);
    6.  
    7. switch (fdwReason)
    8. {
    9. case DLL_PROCESS_DETACH:
    10. qDebug() << "DLL is being unloaded" << QThread::currentThreadId();
    11. break;
    12. case DLL_PROCESS_ATTACH:
    13. {
    14. qDebug() << "DLL is being loaded" << QThread::currentThreadId();
    15. hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)main, (LPVOID)hInstance, 0, NULL);
    16. if (hThread != NULL)
    17. break;
    18. else
    19. return FALSE;
    20. }
    21. case DLL_THREAD_ATTACH:
    22. qDebug() << "Process creating thread" << QThread::currentThreadId();
    23. break;
    24. case DLL_THREAD_DETACH:
    25. qDebug() << "Process thread exiting" << QThread::currentThread();
    26. break;
    27. }
    28. return TRUE;
    29. }
    30.  
    31. int main(int argc, char *argv[])
    32. {
    33. qDebug() << QThread::currentThreadId();
    34. argc = 0;
    35. QApplication MyApp(argc, NULL);
    36. w = new Wrapper();
    37. return MyApp.exec();
    38. }
    To copy to clipboard, switch view to plain text mode 
    First of all, while debugging this does not give me any warning about the QApplication not being created in the main thread. While when using the same code in Qt creator does give warning (they are both using the same compiler from vs2013).

    the following function is used to invoke a method:

    Qt Code:
    1. unsigned __stdcall voxy_init(HWND hParent)
    2. {
    3. qDebug() << "";
    4. qDebug() << "voxy_init";
    5. qDebug() << QThread::currentThreadId();
    6.  
    7. unsigned errorState = 0;
    8. qRegisterMetaType<HWND>("HWND");
    9.  
    10. if (QMetaObject::invokeMethod(w, "connectDialog",
    11. Q_ARG(HWND, hParent))
    12. )
    13. qDebug() << "Invocation successful";
    14.  
    15. return errorState;
    16. }
    To copy to clipboard, switch view to plain text mode 
    When this function runs, invokeMethod() returns true but the method never runs!
    here is the method i am trying to invoke:
    Qt Code:
    1. class Wrapper : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. Wrapper();
    7.  
    8. private slots:
    9. void connectDialog(HWND);
    10. void debugDialog();
    11. };
    12.  
    13. void Wrapper::connectDialog(HWND parent)
    14. {
    15. qDebug() << "ConnectionDialog";
    16. qDebug() << QThread::currentThreadId();
    17.  
    18. ConnectWindow c;
    19. c.setWindowFlags(Qt::WindowStaysOnTopHint);
    20. ::SetParent((HWND)c.winId(), parent);
    21.  
    22. c.setGeometry(400,400,400,400);
    23. c.setWindowFlags(Qt::WindowStaysOnTopHint);
    24. c.setWindowModality(Qt::ApplicationModal);
    25.  
    26. int res = c.exec();
    27. if (res == QDialog::Accepted){
    28. qDebug() << "user accepted selection";
    29.  
    30. // Connect to the selection made by the user
    31. } else{
    32. return;
    33. }
    34.  
    35. return;
    36. }
    To copy to clipboard, switch view to plain text mode 
    Any hints on what could be my problem?

    EDIT: I found out that when building the DLL using QtCreator with the msvc compiler, it does work but only once. this makes it even more confusing...
    EDIT 2: After more debugging this i found out that after the invoked method (Wrapper::connectDialog) returns the thread that it lives on (QApplication thread) exits with the following output in the debug window: The thread 0xf28 has exited with code 0 (0x0).
    EDIT 3: More debugging fixed my problem with the invocation only working once. The application was exiting after closing the last open window. Still having the problem of the invoke not happening in vs2013...

    Thanks,
    Sisco
    Last edited by sisco; 20th June 2014 at 14:17.

  2. #2
    Join Date
    Oct 2009
    Posts
    29
    Thanks
    4

    Default Re: QMetaObject::InvokeMethod returns true but method is never invoked

    This problem can now be marked as fixed....

    It seems to be a problem with visual studio as the code works fine in QtCreator and realese builds from VS2013 but not when debugging. I will open a different thread for that problem next week.

Similar Threads

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

Tags for this Thread

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.