Results 1 to 3 of 3

Thread: Widget access from a QThread

  1. #1
    Join Date
    Dec 2019
    Posts
    23
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Widget access from a QThread

    I understand that you can't access gui widgets from a thread. But I have found that I can call setText for widgets (QPushButton, QLabel etc) from a thread and it seems to work. Is this by design and I can count on it? Or should I go through the hassle of creating signal/slot to do this?

    Thanks much.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Widget access from a QThread

    AFAIK, there is no rule against accessing QObject-based instances from threads (if there was, the entire cross-thread signal/slot protocol would fall apart).

    What you can't do is create any QWidget-based objects anywhere except the main thread (where QApplication lives) and you can't perform any painting operations directly on them from another thread. You can do things like draw on off-screen paint devices (like QImage) and pass them back to the main thread for display.

    So calling methods that set QWidget properties should in general be OK since any GUI updates that occur as a result will be done through the event loop running in the main thread.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    spike6479 (30th September 2020)

  4. #3
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Widget access from a QThread

    Quote Originally Posted by d_stranz View Post
    AFAIK, there is no rule against accessing
    So calling methods that set QWidget properties should in general be OK since any GUI updates that occur as a result will be done through the event loop running in the main thread.
    No since the functions like e.g. setText() are not thread-safe.

  5. The following user says thank you to ChristianEhrlicher for this useful post:

    d_stranz (1st October 2020)

Similar Threads

  1. Ho two access ui of another widget?
    By unix7777 in forum Qt Programming
    Replies: 8
    Last Post: 16th December 2011, 12:33
  2. Own widget access causes crash
    By p3t3 in forum Newbie
    Replies: 1
    Last Post: 24th March 2011, 02:13
  3. how can i access other form widget
    By crissti_cta in forum Newbie
    Replies: 2
    Last Post: 14th December 2010, 21:51
  4. access the widget dynamically
    By mohanakrishnan in forum Qt Programming
    Replies: 4
    Last Post: 26th November 2009, 13:37
  5. QThread Data Access Method
    By schan117 in forum Qt Programming
    Replies: 7
    Last Post: 18th August 2009, 07:25

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.