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. why my custom signal not call the custom slot ?
Forum Updated to NodeBB v4.3 + New Features

why my custom signal not call the custom slot ?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 556 Views 1 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.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on last edited by Qt embedded developer
    #1

    i have made custom signal and slot below way :

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
        void errorcodevalue ();
    
    signals:
       void mySignal(int myParameter);
    
    private slots:
        void on_send_button_clicked();
    
        void on_pushButton_clicked();
    
    private:
        Ui::MainWindow *ui;
    };
    #endif // MAINWINDOW_H
    

    definition of this signal slot added in below file :

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QDebug>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        connect(this, SIGNAL(mySignal()), this, SLOT(on_send_button_clicked));
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::errorcodevalue()
    {
        int i = 0;
        if(i==0)
        emit mySignal(i);
        else
            qDebug()<<"signal call failed";
    }
    
    void MainWindow::on_send_button_clicked()
    {
        qDebug()<<"signal call success";
    }
    
    
    void MainWindow::on_pushButton_clicked()
    {
    errorcodevalue();
    }
    
    
    

    so can any body tell me why my custom signal not get emitted on click on pushbutton of ui ?

    why my slot also not get called ?

    what change can make it work ?

    M 1 Reply Last reply
    0
    • JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by
      #2

      connect(this, SIGNAL(mySignal()), this, SLOT(on_send_button_clicked() ));
      but you can use lambda function for this;
      connect( this, &MainWindow::mySignal, this, &MainWindow::on_send_button_clicked );

      Q 1 Reply Last reply
      1
      • JoeCFDJ JoeCFD

        connect(this, SIGNAL(mySignal()), this, SLOT(on_send_button_clicked() ));
        but you can use lambda function for this;
        connect( this, &MainWindow::mySignal, this, &MainWindow::on_send_button_clicked );

        Q Offline
        Q Offline
        Qt embedded developer
        wrote on last edited by Qt embedded developer
        #3

        @JoeCFD said in why my custom signal not call the custom slot ?:

        connect( this, &MainWindow::mySignal, this, &MainWindow::on_send_button_clicked );

        Dear @JoeCFD ,

        your first solution not works for me but second solution works fine. can you tell me why first solution not working ?

        1 Reply Last reply
        0
        • Q Qt embedded developer

          i have made custom signal and slot below way :

          #ifndef MAINWINDOW_H
          #define MAINWINDOW_H
          
          #include <QMainWindow>
          
          QT_BEGIN_NAMESPACE
          namespace Ui { class MainWindow; }
          QT_END_NAMESPACE
          
          class MainWindow : public QMainWindow
          {
              Q_OBJECT
          
          public:
              MainWindow(QWidget *parent = nullptr);
              ~MainWindow();
          
              void errorcodevalue ();
          
          signals:
             void mySignal(int myParameter);
          
          private slots:
              void on_send_button_clicked();
          
              void on_pushButton_clicked();
          
          private:
              Ui::MainWindow *ui;
          };
          #endif // MAINWINDOW_H
          

          definition of this signal slot added in below file :

          #include "mainwindow.h"
          #include "ui_mainwindow.h"
          #include <QDebug>
          
          MainWindow::MainWindow(QWidget *parent)
              : QMainWindow(parent)
              , ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
          
              connect(this, SIGNAL(mySignal()), this, SLOT(on_send_button_clicked));
          }
          
          MainWindow::~MainWindow()
          {
              delete ui;
          }
          
          void MainWindow::errorcodevalue()
          {
              int i = 0;
              if(i==0)
              emit mySignal(i);
              else
                  qDebug()<<"signal call failed";
          }
          
          void MainWindow::on_send_button_clicked()
          {
              qDebug()<<"signal call success";
          }
          
          
          void MainWindow::on_pushButton_clicked()
          {
          errorcodevalue();
          }
          
          
          

          so can any body tell me why my custom signal not get emitted on click on pushbutton of ui ?

          why my slot also not get called ?

          what change can make it work ?

          M Offline
          M Offline
          mpergand
          wrote on last edited by mpergand
          #4

          @Qt-embedded-developer
          A little bit of coherence should solve your issue.

          you declare your signal with a parameter but not in connect:
          connect(this, SIGNAL(mySignal()), this, SLOT(on_send_button_clicked));

          your slot has no parameter anyway, should work if adding parenthesis
          SLOT(on_send_button_clicked () ));

          Look at the console, i'm sure there are connection errors messages.

          One would recomand you to use new syntax for connections instead.
          Doesn't resolve your logic errors anyway :)

          Q 1 Reply Last reply
          1
          • M mpergand

            @Qt-embedded-developer
            A little bit of coherence should solve your issue.

            you declare your signal with a parameter but not in connect:
            connect(this, SIGNAL(mySignal()), this, SLOT(on_send_button_clicked));

            your slot has no parameter anyway, should work if adding parenthesis
            SLOT(on_send_button_clicked () ));

            Look at the console, i'm sure there are connection errors messages.

            One would recomand you to use new syntax for connections instead.
            Doesn't resolve your logic errors anyway :)

            Q Offline
            Q Offline
            Qt embedded developer
            wrote on last edited by
            #5

            @mpergand said in why my custom signal not call the custom slot ?:

            SLOT(on_send_button_clicked () ));

            dear i have added below line but its not working

             connect(this, SIGNAL(mySignal()), this, SLOT(on_send_button_clicked () ));
            

            i am getting below warning :
            while code runs:

            QMetaObject::connectSlotsByName: No matching signal for on_send_button_clicked()
            QObject::connect: No such signal MainWindow::mySignal() in ../customsignalchange/mainwindow.cpp:17
            QObject::connect: (sender name: 'MainWindow')
            QObject::connect: (receiver name: 'MainWindow')

            M JonBJ 2 Replies Last reply
            0
            • Q Qt embedded developer

              @mpergand said in why my custom signal not call the custom slot ?:

              SLOT(on_send_button_clicked () ));

              dear i have added below line but its not working

               connect(this, SIGNAL(mySignal()), this, SLOT(on_send_button_clicked () ));
              

              i am getting below warning :
              while code runs:

              QMetaObject::connectSlotsByName: No matching signal for on_send_button_clicked()
              QObject::connect: No such signal MainWindow::mySignal() in ../customsignalchange/mainwindow.cpp:17
              QObject::connect: (sender name: 'MainWindow')
              QObject::connect: (receiver name: 'MainWindow')

              M Offline
              M Offline
              mpergand
              wrote on last edited by mpergand
              #6

              @Qt-embedded-developer said in why my custom signal not call the custom slot ?:
              connect(this, SIGNAL(mySignal(int)), this, SLOT(on_send_button_clicked () ));

              1 Reply Last reply
              2
              • Q Qt embedded developer

                @mpergand said in why my custom signal not call the custom slot ?:

                SLOT(on_send_button_clicked () ));

                dear i have added below line but its not working

                 connect(this, SIGNAL(mySignal()), this, SLOT(on_send_button_clicked () ));
                

                i am getting below warning :
                while code runs:

                QMetaObject::connectSlotsByName: No matching signal for on_send_button_clicked()
                QObject::connect: No such signal MainWindow::mySignal() in ../customsignalchange/mainwindow.cpp:17
                QObject::connect: (sender name: 'MainWindow')
                QObject::connect: (receiver name: 'MainWindow')

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @Qt-embedded-developer
                Why do you not use New Signal Slot Syntax? You would be doing yourself a favour. It is now a decade old anyway, and would prevent the problems you are having with SIGNAL/SLOT() macros.

                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