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. Modifying QLabel text from another thread
Forum Updated to NodeBB v4.3 + New Features

Modifying QLabel text from another thread

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 4 Posters 448 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.
  • 0 Offline
    0 Offline
    0x4D6F652E51
    wrote on last edited by 0x4D6F652E51
    #1

    This code below modifies QLabel text from another thread, but doesn't throw cross thread exception, as i remember Qt throws an exception if gui was modified from another thread, or I'm missing something!!

    class myWidget: public QWidget
    {
      myWidget():QWidget(){
      m_label = new QLabel("text from main thread");
      // setup UI
      }
    
    public slots:
      void onButtonClick(){
        QtConcurrent::task([](QLabel* label){
           label->setText("text from thread pool");
           })
          .args(m_label)
          .spawn(QtConcurrent::FutureResult::Ignore);  
      }
    };
    

    Thanks in advance.

    JonBJ Christian EhrlicherC S 3 Replies Last reply
    0
    • 0 0x4D6F652E51

      This code below modifies QLabel text from another thread, but doesn't throw cross thread exception, as i remember Qt throws an exception if gui was modified from another thread, or I'm missing something!!

      class myWidget: public QWidget
      {
        myWidget():QWidget(){
        m_label = new QLabel("text from main thread");
        // setup UI
        }
      
      public slots:
        void onButtonClick(){
          QtConcurrent::task([](QLabel* label){
             label->setText("text from thread pool");
             })
            .args(m_label)
            .spawn(QtConcurrent::FutureResult::Ignore);  
        }
      };
      

      Thanks in advance.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @0x4D6F652E51 said in Modifying QLabel text from another thread:

      as i remember Qt throws an exception if gui was modified from another thread

      I'm not sure it does, or maybe that it is not guaranteed to do so and behaviour might have changed or whatever.

      I thought you are simply not supposed to alter a UI object from a secondary thread, and it is your responsibility not to do so. Are you just complaining that you know you must not do this but are disappointed not to get a warning?

      1 Reply Last reply
      0
      • 0 0x4D6F652E51

        This code below modifies QLabel text from another thread, but doesn't throw cross thread exception, as i remember Qt throws an exception if gui was modified from another thread, or I'm missing something!!

        class myWidget: public QWidget
        {
          myWidget():QWidget(){
          m_label = new QLabel("text from main thread");
          // setup UI
          }
        
        public slots:
          void onButtonClick(){
            QtConcurrent::task([](QLabel* label){
               label->setText("text from thread pool");
               })
              .args(m_label)
              .spawn(QtConcurrent::FutureResult::Ignore);  
          }
        };
        

        Thanks in advance.

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @0x4D6F652E51 said in Modifying QLabel text from another thread:

        as i remember Qt throws an exception if gui was modified from another thread

        No, it does not.
        You're on your own checking all gui related stuff is done in the main (gui) thread.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        1
        • 0 0x4D6F652E51

          This code below modifies QLabel text from another thread, but doesn't throw cross thread exception, as i remember Qt throws an exception if gui was modified from another thread, or I'm missing something!!

          class myWidget: public QWidget
          {
            myWidget():QWidget(){
            m_label = new QLabel("text from main thread");
            // setup UI
            }
          
          public slots:
            void onButtonClick(){
              QtConcurrent::task([](QLabel* label){
                 label->setText("text from thread pool");
                 })
                .args(m_label)
                .spawn(QtConcurrent::FutureResult::Ignore);  
            }
          };
          

          Thanks in advance.

          S Offline
          S Offline
          SimonSchroeder
          wrote on last edited by
          #4

          @0x4D6F652E51 said in Modifying QLabel text from another thread:

          as i remember Qt throws an exception if gui was modified from another thread

          From my experience: Sometimes it crashes when you do this. Maybe most of the time it doesn't. But even if it doesn't it most likely will on the next computer. Qt does not give any warning.

          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