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. Emitting custom signal
Forum Updated to NodeBB v4.3 + New Features

Emitting custom signal

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 11.7k Views 3 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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    Hi,
    I have the following mainwindow.h:

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

    and the following mainwindow.cpp:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget* parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow) {
      ui->setupUi(this);
      connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(display()));
    }
    
    MainWindow::~MainWindow() {
      delete ui;
    }
    
    void MainWindow::display() {
      qDebug() << "Entered display"; //works
      emit myMessage ("Signal emitted from pushbutton");
    }
    
    void MainWindow::on_pushButton_clicked() {
    
      emit myMessage ("Signal emitted from pushbutton");
      qDebug() << "Entered on_pushButton_clicked";//works
    }
    

    Everything works except it does not emit the myMessage signal. Where did I make a mistake?
    Thank you fro your help.

    1 Reply Last reply
    0
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by
      #2

      Hi gabor53 ,

      Where did you connect the emitted signal? The signal is sent, but you didn't 'receive' it
      Do something like this in the constructor :
      ```
      connect(this, SIGNAL(myMessage(QString)), this, SLOT(showMessage(QString)));

      where showMessage is a slot that takes the QString from you signal and shows it id with qDebug().

      Qt Certified Specialist
      www.edalsolutions.be

      G 1 Reply Last reply
      4
      • EddyE Eddy

        Hi gabor53 ,

        Where did you connect the emitted signal? The signal is sent, but you didn't 'receive' it
        Do something like this in the constructor :
        ```
        connect(this, SIGNAL(myMessage(QString)), this, SLOT(showMessage(QString)));

        where showMessage is a slot that takes the QString from you signal and shows it id with qDebug().
        G Offline
        G Offline
        gabor53
        wrote on last edited by
        #3

        @Eddy
        This is what worked for me:

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        
        MainWindow::MainWindow(QWidget* parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow) {
          ui->setupUi(this);
          connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(display()));
        }
        
        MainWindow::~MainWindow() {
          delete ui;
        }
        
        void MainWindow::display() {
          qDebug() << "Entered display"; //works
          QString msg = "Signal emitted from pushbutton.";
          emit myMessage (msg);
        }
        
        void MainWindow::on_pushButton_clicked() {
        
          emit myMessage ("Signal emitted from pushbutton");
          qDebug() << "Entered on_pushButton_clicked";//works
          connect(this, SIGNAL(myMessage(QString)), this, SLOT(showMessage(QString)));
        }
        
        void MainWindow::showMessage(QString msg) {
          qDebug() << "Signal message: " << msg;
        }
        

        Thank you for your help.

        1 Reply Last reply
        0
        • EddyE Offline
          EddyE Offline
          Eddy
          wrote on last edited by
          #4

          @gabor53 well done.

          You could also use other signal slots mechanisms by fine tuning some things. More info can be found in the docs signalsandslots-syntaxes

          Qt Certified Specialist
          www.edalsolutions.be

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

            Hi,

            Watch out, in on_pushButton_clicked you'll be calling the connect method every time you click on the button so you are going to have multiple calls the the slot cumulating.

            That connect statement should be in the constructor of your MainWindow class.

            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
            1

            • Login

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