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. Set Qlabel from another thread.

Set Qlabel from another thread.

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 5.9k Views
  • 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.
  • D Offline
    D Offline
    dwsld
    wrote on last edited by
    #1

    When I try to set text of a label from another thread I get this error Cannot send events to objects owned by a different thread. Current thread 376e328. Receiver '' (of type 'QTextDocument') was created in thread.How to fix it?

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      Use the signal/slot method and give the text of the label as parameter. That should do the trick!

      Greetz, Jeroen

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dwsld
        wrote on last edited by
        #3

        Can you give an example.

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @
          QObject::connect(this, SIGNAL(setLabelText(const QString &)), label, SLOT(setText(const QString &), Qt::QueuedConnection);
          @

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dwsld
            wrote on last edited by
            #5

            When I try to use setLabelText it says"identifier not found ".

            1 Reply Last reply
            0
            • G Offline
              G Offline
              gimble
              wrote on last edited by
              #6

              I coded up an example. From there you should be able to figure out your error. Sorry, documentation not included. Note that this app has at least one bug ( QThread can be destroyed while the thread still running ), but your question wasn't about thread startup/shutdown.

              Counter.h
              @
              #ifndef COUNTER_H
              #define COUNTER_H
              #include <QObject>

              class Counter : public QObject
              {
              Q_OBJECT
              public:
              explicit Counter(QObject *parent = 0);

              signals:
              void setLabelText( const QString& newValue );
              void finished();

              public slots:
              void countto10();
              };
              #endif // COUNTER_H
              @

              Counter.cpp
              @
              #include "counter.h"
              #include <QThread>

              Counter::Counter(QObject *parent) :
              QObject(parent)
              {
              }

              void Counter::countto10()
              {
              for ( int i = 1; i <= 10; ++i )
              {
              emit setLabelText( QString::number(i) );
              QThread::sleep( 1 );
              }
              emit setLabelText( tr("Done") );
              emit finished();
              }
              @

              Window.h
              @
              #ifndef WINDOW_H
              #define WINDOW_H

              #include <QWidget>
              #include <QLabel>

              class Window : public QWidget
              {
              Q_OBJECT
              public:
              Window(QWidget *parent = 0);
              private:
              QLabel *textLabel;
              };

              #endif
              @

              Window.cpp
              @
              #include <QtWidgets>
              #include "window.h"
              #include "counter.h"

              Window::Window(QWidget *parent)
              : QWidget(parent)
              {
              textLabel = new QLabel(tr("I am a text label"));
              QGridLayout *mainLayout = new QGridLayout;
              mainLayout->setSizeConstraint(QLayout::SetNoConstraint);
              mainLayout->addWidget(textLabel, 1, 0);
              setLayout(mainLayout);

              QThread* thread = new QThread(this);
              Counter* counter = new Counter();
              counter->moveToThread(thread);
              connect(thread, SIGNAL(started()), counter, SLOT(countto10()));
              connect(counter, SIGNAL(finished()), thread, SLOT(quit()));
              connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
              
              connect( counter, SIGNAL(setLabelText(QString)),
                       textLabel, SLOT(setText(QString)) );
              
              thread->start();
              

              }
              @

              Main.cpp
              @
              #include <QApplication>

              #include "window.h"

              int main(int argc, char *argv[])
              {
              QApplication app(argc, argv);
              Window window;
              window.show();
              return app.exec();
              }
              @

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dwsld
                wrote on last edited by
                #7

                I sill have and problem - it doesn't work.
                @
                ...
                CursorListener *cursorWorker;
                ...
                someMethod()
                {
                emit setLabelText(qstr);
                }
                ...
                connect(cursorWorker, SIGNAL(setLabelText(const QString &)), myBrowser, SLOT(setText(const QString &)));
                ....
                @
                when I call someMethod my Browser doesn't change its text.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  gimble
                  wrote on last edited by
                  #8

                  The problem is relatively straightforward, so there are only a few possibilities that I can see.

                  1. The connect() method isn't being called prior to someMethod() being called.
                  2. The setLabelText() signal definition is not correct.
                  3. The connect() function is being called with parameters that fail at run time.
                  4. The connection is being disconnected elsewhere in the code
                  5. Some other problem is occurring in the source code that we cannot see.

                  If you're using Qt5, you can use the new connect() syntax to find errors at compile time in #2 or #3. See here:
                  http://qt-project.org/doc/qt-5.0/qtdoc/qt5-intro.html#new-connection-syntax
                  or here:
                  http://qt-project.org/wiki/New_Signal_Slot_Syntax
                  or even here:
                  http://woboq.com/blog/how-qt-signals-slots-work-part2-qt5.html

                  Other than that, there's not a lot of help I can give. "It doesn't work" with 10 lines of source is not much to go on.

                  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