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

I'm lost with signals and slots



  • On a sender side
    sys.h

    #ifndef SYS_H
    #define SYS_H
    
    #include <QObject>
    
    class sys : public QObject
    {
        Q_OBJECT
    
        public:
        sys();
    
        signals:
        void SendMessage(QString msg);
    
        private:
        void SysParamInit(void);
    
        public:
        void Print(QString msg);
    };
    
    #endif // SYS_H
    

    sys.cpp

    #include "sys.h"
    
    sys::sys()
    {
    }
    
    void sys::Print(QString msg)
    {
        emit(SendMessage(msg));
    }
    

    on a receiver side
    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    public slots:
        void DisplayTerminalMessage(QString msg);
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    void MainWindow::DisplayTerminalMessage(QString msg)
    {
        ui->textEditTerminalRx->setText(msg);
    }
    

    and finally

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        sys system;
    
        QObject::connect(&system, SIGNAL(SendMessage(QString msg)), &w, SLOT(DisplayTerminalMessage(QString msg));
    
        system.Print("Hello");
    
        return a.exec();
    }
    

    But I don't see "Hello" in textEditTerminalRx.


  • Moderators

    @jenya7
    hi, a couple of points

    first of, your sender is derived from QObject so at the very least initialize the base class properly

    you should use the new qt5 syntax not the old one

    QObject::connect(&system, &sys::SendMessage, &w, &MainWindow::DisplayTerminalMessage);

    if you want to use the old syntax, do not name your arguments

    QObject::connect(&system, SIGNAL(SendMessage(QString)), &w, SLOT(DisplayTerminalMessage(QString));

    You should have gotten a runtime warning that your connect failed to establish.



  • @J-Hilk said in I'm lost with signals and slots:

    @jenya7
    hi, a couple of points

    first of, your sender is derived from QObject so at the very least initialize the base class properly

    you should use the new qt5 syntax not the old one

    QObject::connect(&system, &sys::SendMessage, &w, &MainWindow::DisplayTerminalMessage);

    if you want to use the old syntax, do not name your arguments

    QObject::connect(&system, SIGNAL(SendMessage(QString)), &w, SLOT(DisplayTerminalMessage(QString));

    You should have gotten a runtime warning that your connect failed to establish.

    Thank you.
    This way
    QObject::connect(&system, &sys::SendMessage, &w, &MainWindow::DisplayTerminalMessage);
    it works.

    first of, your sender is derived from QObject so at the very least initialize the base class properly - how do I do that?


  • Moderators

    @jenya7 said in I'm lost with signals and slots:

    first of, your sender is derived from QObject so at the very least initialize the base class properly - how do I do that?

    Usually you give your constructor a QObject argument and forward that to the Baseclass constructor.

    public:
        sys(QObject *parent = nullptr);
    //.cpp
    sys::sys(QObject parent) : QObject(parent)
    {
    }
    
    


  • Thank you.


Log in to reply