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. Signal and slot
Qt 6.11 is out! See what's new in the release blog

Signal and slot

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 371 Views 2 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.
  • R Offline
    R Offline
    Robocop
    wrote on last edited by
    #1

    Hi, I'm creating a game launcher that has a lable which needs to be updated accordingly. I've read about signals and slots and I managed to change the label by emitting a signal and providing text to the signal obviously. This all worked fine untill I added a thread to my program. Since the addition of the thread the label isn't being updated anymore.

    Here is the code:

    main.cpp
    
    #include "mainwindow.h"
    #include "downloadpatch.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    downloadPatch p;
    versionCompare v;
    
    MainWindow::connect(&v, SIGNAL(sendNewLabel(QString)), &w, SLOT(updateLabel(QString)));
    p.start();
    
    w.show();
    return a.exec();
    }
    
    downloadpatch.h
    #include <QThread>
    #include "versioncompare.h"
    
    class downloadPatch : public QThread
    {
        Q_OBJECT
    public:
        downloadPatch();
        void run();
    };
    
    
    downloadpatch.cpp
    #include "downloadpatch.h"
    
    void downloadPatch::run()
    {
    versionCompare v;
    v.versionMain();
    this->quit();
    }
    
    versioncompare.h
    #include <QObject>
    
    class versionCompare : public QObject
    {
        Q_OBJECT
    public:
        versionCompare();
        void versionMain();
    signals:
        void sendNewLabel(QString);
    };
    
    
    versioncompare.cpp
    #include "versioncompare.h"
    
    void versionCompare::versionMain()
    {
    emit sendNewLabel("blabla");
    }
    
    mainwindow.h
    #include <QApplication>
    #include <QMainWindow>
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    private slots:
        void updateLabel(QString msg);
    };
    
    
    mainwindow.cpp (GUI)
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    void MainWindow::updateLabel(QString msg)
    {
    ui->label->setText(msg);
    }
    
    Pl45m4P 1 Reply Last reply
    0
    • JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      You connect() the signal on object versionCompare v, which is a local variable in main().

      You emit the signal on object versionCompare v, which is a local variable in downloadPatch::run().

      These two variables/objects/instances are totally distinct. You have not connect()ed the signal on the local variable living in downloadPatch::run() which you are using to emit the signal.

      1 Reply Last reply
      4
      • R Robocop

        Hi, I'm creating a game launcher that has a lable which needs to be updated accordingly. I've read about signals and slots and I managed to change the label by emitting a signal and providing text to the signal obviously. This all worked fine untill I added a thread to my program. Since the addition of the thread the label isn't being updated anymore.

        Here is the code:

        main.cpp
        
        #include "mainwindow.h"
        #include "downloadpatch.h"
        #include <QApplication>
        
        int main(int argc, char *argv[])
        {
        QApplication a(argc, argv);
        MainWindow w;
        downloadPatch p;
        versionCompare v;
        
        MainWindow::connect(&v, SIGNAL(sendNewLabel(QString)), &w, SLOT(updateLabel(QString)));
        p.start();
        
        w.show();
        return a.exec();
        }
        
        downloadpatch.h
        #include <QThread>
        #include "versioncompare.h"
        
        class downloadPatch : public QThread
        {
            Q_OBJECT
        public:
            downloadPatch();
            void run();
        };
        
        
        downloadpatch.cpp
        #include "downloadpatch.h"
        
        void downloadPatch::run()
        {
        versionCompare v;
        v.versionMain();
        this->quit();
        }
        
        versioncompare.h
        #include <QObject>
        
        class versionCompare : public QObject
        {
            Q_OBJECT
        public:
            versionCompare();
            void versionMain();
        signals:
            void sendNewLabel(QString);
        };
        
        
        versioncompare.cpp
        #include "versioncompare.h"
        
        void versionCompare::versionMain()
        {
        emit sendNewLabel("blabla");
        }
        
        mainwindow.h
        #include <QApplication>
        #include <QMainWindow>
        
        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        public:
            MainWindow(QWidget *parent = nullptr);
            ~MainWindow();
        private slots:
            void updateLabel(QString msg);
        };
        
        
        mainwindow.cpp (GUI)
        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        
        void MainWindow::updateLabel(QString msg)
        {
        ui->label->setText(msg);
        }
        
        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by
        #3

        @Robocop

        Why do you create separate classes for downloadPatch and versionCompare.?
        Normally these would be functions.

        (Maybe an Update class with functions to check the current version and then start downloading if needed)


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        1
        • R Offline
          R Offline
          Robocop
          wrote on last edited by
          #4

          Thank you @JonB for your explanation I understand the problem right now.
          @Pl45m4 I created two seperate classes, because those classes have more functions to them and I didn't want to overload a single class with multiple functions. Perhaps I should refactor my code a bit more and delete unnecessary stuff. Thank you for the suggestion :P

          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