There is a class:

class RMS_Button : public QPushButton { Q_OBJECT public: RMS_Button(QWidget *p) : QPushButton(p) { connect(this, SIGNAL(clicked(int)), this, SLOT(click(int))); } 
signals: void clicked(int); public slots: void click(int i) { qDebug() << "clicked(int)"; emit clicked(i); } };
(QPushButton + = clicked (int) ).
Program:

 int main(int argc, char ** argv) { QApplication app(argc, argv); RMS_Button b(nullptr); b.show(); return app.exec(); } 

When you click on the button, it does not display clicked (int) . What am I doing wrong?

  • one
    QPushButton + = clicked (int) - this means: void MainWindow :: on_pushButton_clicked () {emit clicked (i); }? - Madisson
  • No, this is not a code, I just wanted to say that I added a signal to QPushButton - cipher_web
  • one
    QPushButton has no clicked(int) signal, it has clicked(bool) . I would check that connect returns. - KoVadim
  • one
    evileg.ru/baza-znanij/qt/signaly-i-sloty-v-qt5.html . This article is an example. What you need. - Madisson

1 answer 1

QPushButton has a void clicked(bool); signal void clicked(bool); which is triggered when a button is pressed

 class RMS_Button : public QPushButton { Q_OBJECT public: RMS_Button(QWidget *p) : QPushButton(p) { connect(this, SIGNAL(clicked(bool)), this, SLOT(click(bool))); } //signals: //void clicked(bool); public slots: void click(bool i) { qDebug() << "clicked(int)"; //emit clicked(i); } }; 

Well, if you need an int , then probably the only way:

 class RMS_Button : public QPushButton { Q_OBJECT public: RMS_Button(QWidget *p) : QPushButton(p) { connect(this, SIGNAL(clicked(bool)), this, SLOT(click(bool))); connect(this, SIGNAL(my_clicked(int)), this, SLOT(click(int))); } signals: void my_clicked(int); //void clicked(bool i) public slots: void click(bool) { qDebug() << "clicked(int)"; int i = 10; emit my_clicked(i); } void click(int i) {} }; 
  • abstract signal? this is something new. - KoVadim
  • Yes, I know that, but I need to take an int into the slot in order not to write a slot for all occasions - cipher_web
  • I wonder why slots can not be template? - cipher_web
  • They cannot be template because of moc'a. Although, you can try to improve the moc. - KoVadim
  • Try to create your own connect(this, SIGNAL(clicked(bool)), this, SLOT(my_click(bool))); which emit with int parameter - Nikolay