Results 1 to 6 of 6

Thread: update widget from separate thread

  1. #1
    Join Date
    Jul 2009
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default update widget from separate thread

    hello, i am new to QT and i am having some difficulties getting a widget to update as desired.

    i have a separate thread that acquires images from a camera, and i would like to display a live feed from the camera in my gui.

    i am calling widget->update after a new image has been acquired, but the widget does not repaint.

    calling repaint or some other more direct method to repaint the widget fails a qt thread-safety assertion.

    can someone please point me in the right direction?

    thanks!

  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: update widget from separate thread

    You can't access methods from widgets from worker threads, it will eventually lead to a crash. You need to pass the data to the main thread (using events or signals) and update the widget there.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2009
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: update widget from separate thread

    hi wysota, thanks for replying.

    i understand that is the mechanism i need to use, but i am a little unclear of the details of how to go about emitting the signal in this instance.

    a good strategy for my application seems to be to create a function in the main GUI class that will then emit the proper signal, but i'm having trouble locating the section of the documentation that demonstrates the proper use of the emit keyword.

  4. #4
    Join Date
    Jul 2009
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: update widget from separate thread

    i shoudl clarify, i can't find anything that demonstrates the use of signals and slots across different threads.


    if i call the function:

    Qt Code:
    1. void myWidget::emitUpdate()
    2. {
    3. emit update();
    4. }
    To copy to clipboard, switch view to plain text mode 

    from my worker thread, nothing happens.

    if i emit repaint() insteand, i fail a thread-safety assertion.

    my current strategy is to overload paintEvent and recursively call update(). this gives me very fluid video, but it is very wasteful of CPU; i'd like to update only when i have new frames available.

  5. #5
    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: update widget from separate thread

    Connect a signal to update() (which is a slot not a signal so don't emit it). It will always be called in the context of the main thread (regardless where you emit a signal connected to it). The rest is just transferring data there so that update has anything to do.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    method (10th July 2009)

  7. #6
    Join Date
    Jul 2009
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: update widget from separate thread

    got it, thanks!

Similar Threads

  1. QDockWidget inside another widget in the center?
    By Antebios in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2010, 07:06
  2. new QWidget in separate thread
    By magland in forum Qt Programming
    Replies: 15
    Last Post: 7th February 2008, 12:32
  3. handling paintGL in a separate thread
    By al101 in forum Qt Programming
    Replies: 1
    Last Post: 15th May 2007, 17:04
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  5. Replies: 10
    Last Post: 20th March 2007, 22:19

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.