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

QLabel update from thread

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

    Hi all, I'm new to QT, I have searched a lot on internet for a solution but nothing.
    I have a QLabel and I want to update it from inside another thread.
    If I use

    ui->label1->setText("Hello Qt!");

    all is working fine, but if I want to change text color

    ui->label1->setText("<h2><i>Hello</i><font color=red>Qt!</font></h2>");

    I have a cross threading exception and I have to close the application.
    The two examples woks fine outside a thread.
    I use Visual C++ 2008, not managed code, the target device is Windows CE;

    Tank you

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

      You cannot manipulate a widget from a secondary thread.

      Have your surrounding class a setText() slot which is called from the secondary thread, and forward the text to the setText() slot of the label.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sidewinder
        wrote on last edited by
        #3

        Hi,
        you can't access gui elements from other threads. Simplest way to solve your problem is to use signals and slots. In your not main thread emit signal which will trigger slot in your main thread and update necessary gui elements.

        edit: Awww, late again... :(

        "Never memorize what you can look up in books."
        Albert Einstein

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tomma
          wrote on last edited by
          #4

          Or use "QMetaObject::invokeMethod":http://qt-project.org/doc/qt-4.8/qmetaobject.html#invokeMethod with Qt::QueuedConnection

          @
          QMetaObject::invokeMethod(ui->label1, "setText" Qt::QueuedConnection Q_ARG(QString, <stringToSend>));
          @

          1 Reply Last reply
          1
          • G Offline
            G Offline
            gmgunderground
            wrote on last edited by
            #5

            Perfect, now is working

            Thank you

            [quote author="Tomma" date="1344511439"]Or use "QMetaObject::invokeMethod":http://qt-project.org/doc/qt-4.8/qmetaobject.html#invokeMethod with Qt::QueuedConnection

            @
            QMetaObject::invokeMethod(ui->label1, "setText" Qt::QueuedConnection Q_ARG(QString, <stringToSend>));
            @[/quote]

            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