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. How to update a label from another thread (correct method of signal/slots & threading)?[SOLVED]

How to update a label from another thread (correct method of signal/slots & threading)?[SOLVED]

Scheduled Pinned Locked Moved General and Desktop
11 Posts 4 Posters 5.3k 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.
  • I Offline
    I Offline
    ion_knight
    wrote on last edited by
    #1

    below is code for a basic thread that counts to 100. How would i implement this console application in a gui application (specifically having the thread started and stoped by a push button and displaying the current number in a label), any help would be a massive help as struggling to get my head round this problem, i presume it's related to signal and slots however not sure how I would link the count method to a label. Anyone have any suggestions code listed below:

    main.cpp
    @#include <QCoreApplication>
    //#include <QThread>
    #include "object.h"

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    QThread thread;
    Object thread_object;
    
    thread_object.setup(thread);
    thread_object.moveToThread(&thread);
    
    thread.start();
    
    
    
    return a.exec&#40;&#41;;
    

    }
    @

    object.cpp
    @#include "object.h"

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

    void Object::setup(QThread &thread)
    {
    connect(&thread,SIGNAL(started()),this,SLOT(worker()));
    }

    void Object::worker()
    {
    for(int i = 0; i < 100; i++)
    {
    qDebug() << i;
    }
    }
    @

    object.h

    @#ifndef OBJECT_H
    #define OBJECT_H

    #include <QObject>
    #include <QDebug>
    #include <QThread>

    class Object : public QObject
    {
    Q_OBJECT
    public:
    explicit Object(QObject *parent = 0);
    void setup(QThread &thread);
    signals:

    public slots:
    void worker();
    };

    #endif // OBJECT_H
    @

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Please don't post the "almost same question":http://qt-project.org/forums/viewthread/31533/ multiple times.

      There have been several discussions on this forum about how to communicate between threads and the GUI thread recently. A little bit of searching should bring you what need

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • I Offline
        I Offline
        ion_knight
        wrote on last edited by
        #3

        Well firstly, I have been looking on here for a while for a basic example. Of the ones that do talk about threading don't include full code for example "TCP/ip Thread example":http://qt-project.org/forums/viewthread/31382/ doesn't include any code. Also "Qt Threading - How to manage a background process":http://qt-project.org/forums/viewthread/31503/ only contains the .cpp files. So unless you know of another thread that i've missed, don't see how these can help someone who is just trying to do a basic example to get an understanding of the process.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Santosh Reddy
          wrote on last edited by
          #4

          This might help
          "Updating GUI from QThread":http://www.qtcentre.org/wiki/index.php?title=Updating_GUI_from_QThread

          SS

          1 Reply Last reply
          0
          • I Offline
            I Offline
            ion_knight
            wrote on last edited by
            #5

            Thanks Santosh Reddy, exactly what I was looking for as you can imagine QThread are a bit frustrating especially when QT documentation doesn't even have the correct method.

            However just checked that link and it uses QThread as a subclass, along with this the code contains alot of errors. So still having a bit of problem with it.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Santosh Reddy
              wrote on last edited by
              #6

              Oops my post is lost !

              bq. However just checked that link and it uses QThread as a subclass, along with this the code contains alot of errors. So still having a bit of problem with it.

              What errors?
              The link has attached source files which will work in Qt 4.7

              SS

              1 Reply Last reply
              0
              • I Offline
                I Offline
                ion_knight
                wrote on last edited by
                #7

                Sadly using Qt5.1 so coming up with quite a few errors. Btw is it worth me keeping with the latest version or do you suggest a different version ?

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Santosh Reddy
                  wrote on last edited by
                  #8

                  For Qt 5.x just add "#include <QtWidgets>" in the header files, and "QT += widgets" in the project file.

                  SS

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    ion_knight
                    wrote on last edited by
                    #9

                    Cheers Santosh that fixed the main problems. Still had a few errors but they were easily fixable (specifically it details the word 'or' instead of ||) but once that was changed all worked fine (seems Qt doesn't like some of the C++ keywords. Now I have this example working starting to become alot clearer.

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

                      Hi,
                      There are some differences in C++11 etc. Some keywords got dropped, some got added. The OR keyword is to my knowledge not in the C++ standard, but might come from a different compiler option set. Don't blame Qt for following a standard and only the compiler will add extra options. So some keywords are dependent to your compiler.
                      If this post is solved, place [SOLVED] in front of your first post!
                      Greetz

                      Greetz, Jeroen

                      1 Reply Last reply
                      0
                      • I Offline
                        I Offline
                        ion_knight
                        wrote on last edited by
                        #11

                        I don't blame Qt i blame the noob who thought it was a good idea to use the non standard method for creating a or (just looked this up only reason they are even in the standard is because they defined it so that keyboards without the standard operators could still use the logical operators using an alias).

                        So it is part of the C++ standard but they are just not implemented in Qt it seems not like it matters anyway.

                        "Stack Overflow explaination":http://stackoverflow.com/questions/2393673/c-and-or-not-xor-keywords

                        "C++ keyword reference":http://en.cppreference.com/w/cpp/keyword

                        Just inserting these in for the next person so they know the reasoning.

                        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