Results 1 to 8 of 8

Thread: Problem with QLabel and setText

  1. #1
    Join Date
    Oct 2007
    Posts
    27
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Problem with QLabel and setText

    I have 2 threads. One main thread and one created thread. I'm creating thread in constructor of class and pass it "this" pointer. In a thread I'm accessing to class via pointer and call a public method (function) of this class. In this method I'm calling setText method in QLabel widget. It does not work. Program is crashed. When I use setText in another method of this class it works. I've tried another methods of QLabel and some works, and some doesn't. Any method who has "set" in its name doesn't work, and clear too, but methods like adjustSize() works. I tried another widgets and problem is same.

    I've created thread with winapi CreateThread function, and name of class is MainWin.

    Code of created thread:

    DWORD WINAPI ItemReceiver(LPVOID param)
    {
    MainWin *mwin = (MainWin*)param;
    while(true)
    {
    WaitForSingleObject(SEM,INFINITE);
    mwin->ReceiveItem();
    }

    return 0;
    }

    Way of calling setText function:

    lblCommand->setText("Command: ");

    lblCommand is pointer to QLabel.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with QLabel and setText

    You can't access QObject subclasses from within a worker thread.

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QLabel and setText

    Is this a Qt application or just winapi / MFC /other application?
    If this is a Qt application then why not use QThread?

    Also, the way you are doing it is not good.
    Either use an event or a queued signal to update the label.
    OR
    If you really have to access the variable from a another thread use a QMutex.

    Also, please make sure your read the docs about multi threading with Qt.
    Qt makes multi threading much easier then MFC/WinApi.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  4. #4
    Join Date
    Oct 2007
    Posts
    27
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QLabel and setText

    This is a QT application. I've used winapi because it's similar to me. I've never used QThread, but I will try.

    Thanks

  5. #5
    Join Date
    Oct 2007
    Posts
    27
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QLabel and setText

    "wysota: You can't access QObject subclasses from within a worker thread."

    Sometimes I can:

    lblCommand->adjustSize(); //pass
    lblCommand->childAt(20,20); //pass
    lblCommand->setVisible(true); //crash

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problem with QLabel and setText

    In other words, you MUST NOT touch GUI from a worker thread.

    QObject Reentrancy:
    Although QObject is reentrant, the GUI classes, notably QWidget and all its subclasses, are not reentrant. They can only be used from the main thread. As noted earlier, QCoreApplication::exec() must also be called from that thread.

    In practice, the impossibility of using GUI classes in other threads than the main thread can easily be worked around by putting time-consuming operations in a separate worker thread and displaying the results on screen in the main thread when the worker thread is finished. This is the approach used for implementing the Mandelbrot and the Blocking Fortune Client example.
    J-P Nurmi

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with QLabel and setText

    Quote Originally Posted by jambrek View Post
    Sometimes I can:

    lblCommand->adjustSize(); //pass
    lblCommand->childAt(20,20); //pass
    lblCommand->setVisible(true); //crash
    Try running those in a loop and do something else with the GUI in between (like move the window with your mouse). While childAt() might suceed as it doesn't touch much of the structures, adjustSize() is bound to fail eventually. setVisible() is an obvious candidate for an immediate crash though

  8. #8
    Join Date
    Oct 2007
    Posts
    27
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QLabel and setText

    Problem is solved by using signal and slots. I've created signals which connected to setText slots for each label. It works.

    Thanks a lot.

Similar Threads

  1. Replies: 1
    Last Post: 26th November 2006, 09:32

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.