Results 1 to 11 of 11

Thread: Widgets must be created in the GUI thread.

  1. #1
    Join Date
    Jun 2010
    Posts
    102
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Widgets must be created in the GUI thread.

    hi all, i dont understand why my program error Widgets must be created in the GUI thread.

    it use Qthread loop WaitForSingleObject(hEvent, INFINITE);

    when i run test-gui-event-error project and after run sendevent project then it error :

    xxx.jpg


    message error: ASSERT failure in QWidget: "Widgets must be created in the GUI

    note: my source code project: Desktop.zip --> you must run test-gui-event-error project and after run sendevent , it will show error message
    thread.", file kernel/qwidget.cpp
    Attached Files Attached Files
    Contact: Skype: sonnh89
    Yahoo: nhs_0702@yahoo.com

    Liên hệ SKype: sonnh89

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: Widgets must be created in the GUI thread.

    Quote Originally Posted by Thà nh Viên Mới View Post
    hi all, i dont understand why my program error Widgets must be created in the GUI thread.

    it use Qthread loop WaitForSingleObject(hEvent, INFINITE);
    I don't understand either, but I do not see any reference to WaitForSingleObject in the C++ source.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Widgets must be created in the GUI thread.

    Run you project in your debugger. When is asserts look back through the backtrace of the non-main thread(s) until you find the line in your code that is causing the problem.

  4. #4
    Join Date
    Jun 2010
    Posts
    102
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Widgets must be created in the GUI thread.

    Quote Originally Posted by mvuori View Post
    I don't understand either, but I do not see any reference to WaitForSingleObject in the C++ source.
    no, haven't you read my source code attach ???

    Qt Code:
    1. #include "threadwaitforaddgroupevent.h"
    2.  
    3. ThreadWaitForAddGroupEvent::ThreadWaitForAddGroupEvent()
    4. {
    5. hEvent = CreateEventA(NULL, FALSE, FALSE, EVENT_ADD_NEW_GROUP);
    6. hMapFile = CreateFileMappingA(
    7. INVALID_HANDLE_VALUE, // use paging file
    8. NULL, // default security
    9. PAGE_READWRITE, // read/write access
    10. 0, // max. object size
    11. BUF_SIZE, // buffer size
    12. MAP_FILE_ADD_NEW_GROUP); // name of mapping object
    13. if (hMapFile == NULL)
    14. {
    15. return ;
    16. }
    17. }
    18.  
    19. void ThreadWaitForAddGroupEvent::ProcessDataFromMapFile()
    20. {
    21. HANDLE hMapFile = OpenFileMappingA(
    22. FILE_MAP_ALL_ACCESS, // read/write access
    23. FALSE, // do not inherit the name
    24. MAP_FILE_ADD_NEW_GROUP); // name of mapping object
    25. if (hMapFile == NULL)
    26. {
    27. return ;
    28. }
    29. char *pBuf = (char*) MapViewOfFile(
    30. hMapFile, // handle to map object
    31. FILE_MAP_ALL_ACCESS, // read/write permission
    32. 0,
    33. 0,
    34. BUF_SIZE);
    35.  
    36. if (pBuf == NULL)
    37. {
    38. return ;
    39. }
    40. QString dataFromMapFile(pBuf);
    41. QList<QString> lUserNewGroup = dataFromMapFile.split('|');
    42. emit AddNewGroup(lUserNewGroup);
    43.  
    44. }
    45.  
    46. void ThreadWaitForAddGroupEvent::run(){
    47. while(1){
    48. WaitForSingleObject(hEvent, INFINITE);
    49. ProcessDataFromMapFile();
    50. }
    51. }
    52.  
    53. ThreadWaitForAddGroupEvent::~ThreadWaitForAddGroupEvent(){
    54. UnmapViewOfFile(pBuf);
    55. CloseHandle(hMapFile);
    56. }
    To copy to clipboard, switch view to plain text mode 



    SendEvent:

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QtGui/QMessageBox>
    3. #include <QString>
    4. #include <windows.h>
    5.  
    6. #define BUF_SIZE 1024
    7. #define MAP_FILE_ADD_NEW_GROUP "Local\\MyFileMappingAddNewGroup"
    8. #define EVENT_ADD_NEW_GROUP "Local\\eventaddnewgroup"
    9.  
    10.  
    11. int main(int argc, char *argv[])
    12. {
    13. QCoreApplication a(argc, argv);
    14. HANDLE hMapFile;
    15. char* pBuf;
    16.  
    17. QString qstrDataSend = "ADDKJHGFDSDFGHJ";
    18. hMapFile = OpenFileMappingA(
    19. FILE_MAP_ALL_ACCESS, // read/write access
    20. FALSE, // do not inherit the name
    21. MAP_FILE_ADD_NEW_GROUP); // name of mapping object
    22. if (hMapFile == NULL)
    23. {
    24. QMessageBox::information( NULL, "L?i ph?n m?m","L?i OpenFileMapping, B?n hãy kh?i d?ng l?i ph?n m?m" );
    25. return 1;
    26. }
    27. pBuf = (char*) MapViewOfFile(hMapFile, // handle to map object
    28. FILE_MAP_ALL_ACCESS, // read/write permission
    29. 0,
    30. 0,
    31. BUF_SIZE);
    32. if (pBuf == NULL)
    33. {
    34. QMessageBox::information( NULL, "loi","khong the map view file" );
    35. return 2;
    36. }
    37. CopyMemory((PVOID) pBuf, qstrDataSend.toStdString().data(), qstrDataSend.length());
    38. HANDLE hEvent = OpenEventA(EVENT_ALL_ACCESS, FALSE, EVENT_ADD_NEW_GROUP);
    39. SetEvent(hEvent);
    40. return a.exec();
    41. }
    To copy to clipboard, switch view to plain text mode 
    Contact: Skype: sonnh89
    Yahoo: nhs_0702@yahoo.com

    Liên hệ SKype: sonnh89

  5. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Widgets must be created in the GUI thread.

    If you want to use QtGui, then you have to instantiate QApplication object, QCoreApplication is not enough to use message windows, change this
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. ...
    3. QCoreApplication a(argc, argv);
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. #include <QApplication>
    2. ...
    3. QApplication a(argc, argv);
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jun 2010
    Posts
    102
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Widgets must be created in the GUI thread.

    thanks you, i was fix Widgets must be created in the GUI thread on SendEvent, but when i run test-gui-event-error project and after run sendevent project then it still error on test-gui-event-error

    this is error debug on QtCreator


    in c:\ndk_buildrepos\qt-desktop\src\corelib\global\qglobal.cpp
    0x6a10e211 <+000>: push %ebp
    0x6a10e212 <+001>: mov %esp,%ebp
    0x6a10e214 <+003>: sub $0x18,%esp
    2027 in c:\ndk_buildrepos\qt-desktop\src\corelib\global\qglobal.cpp
    0x6a10e217 <+006>: mov 0x10(%ebp),%eax
    0x6a10e21a <+009>: mov %eax,0xc(%esp)
    0x6a10e21e <+013>: mov 0xc(%ebp),%eax
    0x6a10e221 <+016>: mov %eax,0x8(%esp)
    0x6a10e225 <+020>: mov 0x8(%ebp),%eax
    0x6a10e228 <+023>: mov %eax,0x4(%esp)
    0x6a10e22c <+027>: movl $0x6a2e3e50,(%esp)
    0x6a10e233 <+034>: call 0x6a10ebf0 <qFatal(const char *, ...)>
    2028 in c:\ndk_buildrepos\qt-desktop\src\corelib\global\qglobal.cpp
    0x6a10e238 <+039>: leave
    0x6a10e239 <+040>: ret

    QtCenter-VietNam.zip
    Contact: Skype: sonnh89
    Yahoo: nhs_0702@yahoo.com

    Liên hệ SKype: sonnh89

  7. #7
    Join Date
    Jun 2010
    Posts
    102
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Widgets must be created in the GUI thread.

    can you help me ?
    Contact: Skype: sonnh89
    Yahoo: nhs_0702@yahoo.com

    Liên hệ SKype: sonnh89

  8. #8
    Join Date
    Jun 2010
    Posts
    102
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Widgets must be created in the GUI thread.

    can you help me ??? (
    Contact: Skype: sonnh89
    Yahoo: nhs_0702@yahoo.com

    Liên hệ SKype: sonnh89

  9. #9
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Widgets must be created in the GUI thread.

    try to change

    Qt Code:
    1. connect(tWAGE,SIGNAL(AddNewGroup(QList<QString>)),this,SLOT(prepareAddGroup(QList<QString>)),Qt::DirectConnection );
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. connect(tWAGE,SIGNAL(AddNewGroup(QList<QString>)),this,SLOT(prepareAddGroup(QList<QString>)));
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. connect(tWAGE,SIGNAL(AddNewGroup(QList<QString>)),this,SLOT(prepareAddGroup(QList<QString>)), Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 

    see http://doc.trolltech.com/4.5/threads...across-threads

  10. #10
    Join Date
    Jun 2010
    Posts
    102
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Widgets must be created in the GUI thread.

    it still error


    when i play QmessageBOx in Qthread then it still also error

    help me resolve
    Contact: Skype: sonnh89
    Yahoo: nhs_0702@yahoo.com

    Liên hệ SKype: sonnh89

  11. #11
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Widgets must be created in the GUI thread.

    Yes of course. Please read http://doc.qt.nokia.com/4.7-snapshot/thread-basics.html

    As mentioned, each program has one thread when it is started. This thread is called the "main thread" (also known as the "GUI thread" in Qt applications). The Qt GUI must run in this thread. All widgets and several related classes, for example QPixmap, work in secondary threads.
    To be clear: Its (by the design of Qt) not possible to work with widgets in threads. The work only in the main thread! Inform the main thread from your worker thread to show the message box.

Similar Threads

  1. delete runtime-created widgets causes crash
    By jonasbalmer in forum Qt Programming
    Replies: 5
    Last Post: 10th February 2010, 21:36
  2. How to access Widgets created in Tab View
    By kapoorsudhish in forum Newbie
    Replies: 5
    Last Post: 23rd October 2009, 13:12
  3. Reading widgets from thread
    By sirlore in forum Qt Programming
    Replies: 3
    Last Post: 24th June 2009, 21:58
  4. Accesing widgets from separate thread
    By msmihai in forum Qt Programming
    Replies: 2
    Last Post: 8th December 2008, 11:48
  5. Replies: 6
    Last Post: 3rd September 2008, 14:27

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.