Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Signal and slot



  • 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);
    }
    


  • 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.



  • @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)



  • 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


Log in to reply