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. I'm lost with signals and slots

I'm lost with signals and slots

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 354 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.
  • J Offline
    J Offline
    jenya7
    wrote on last edited by jenya7
    #1

    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.

    J.HilkJ 1 Reply Last reply
    0
    • J jenya7

      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.

      J.HilkJ Online
      J.HilkJ Online
      J.Hilk
      Moderators
      wrote on last edited by
      #2

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


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      J 1 Reply Last reply
      5
      • J.HilkJ J.Hilk

        @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 Offline
        J Offline
        jenya7
        wrote on last edited by jenya7
        #3

        @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?

        J.HilkJ 1 Reply Last reply
        0
        • J jenya7

          @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?

          J.HilkJ Online
          J.HilkJ Online
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @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)
          {
          }
          
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          5
          • J Offline
            J Offline
            jenya7
            wrote on last edited by
            #5

            Thank you.

            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved