Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLabel::setText() from a secondary thread
Forum Updated to NodeBB v4.3 + New Features

QLabel::setText() from a secondary thread

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 4.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JulienMaille
    wrote on last edited by
    #1

    Dear Qt users,
    I have always thought that it was not possible to update GUI QWidgets from any other thread than the main one.
    However I accidentally (I swear!) wrote a piece of code that calls QLabel::setText() from a secondary thread.
    And it works. How do you explain this?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      It is perfectly possible that non-threadsafe code works in multithreaded environments, however it is not guaranteed to do so. That's why you shouldn't modify any widgets outside the main thread - it may work in rare occasions, it might even work in most occasions, but it does not work in all occasions.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JulienMaille
        wrote on last edited by
        #3

        Ok but usually in that case your are warned by an error message: "Cannot send events to objects owned by a different thread"

        1 Reply Last reply
        0
        • M Offline
          M Offline
          miroslav
          wrote on last edited by
          #4

          While it would be possible to check for which thread is calling a function in debug builds, it would be too much overhead for release builds IMO. The ground rule is that if a method is not marked as being thread safe, it is not. For setting text, it will work unless the main thread is accessing the text at the same time. It will definitely crash once you ship the program to the client, says Murphy's law :-)

          Mirko Boehm | mirko@kde.org | KDE e.V.
          FSFE Fellow
          Qt Certified Specialist

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved