c++ QPushButton signal and slot

Aurimas_12 picture Aurimas_12 · Nov 8, 2014 · Viewed 8.7k times · Source

i have a problem creating QPushButton with signal and slot. First i was created a class where is slot :

class A : public QWidget{

  public slots:
void handleButton();

};

There is my handleButton function : in .cpp

   void A::handleButton(int row, int col){
    m_button->setText("Example");
  // resize button
    m_button->resize(100,100);
      }

Then i want to connect button.

           QObject::connect(m_button, SIGNAL(clicked()),qApp, SLOT(handleButton()));

But i got an error when i start application : "No such slot" Any one can help me?

Answer

Marco A. picture Marco A. · Nov 8, 2014

Make sure that qApp is an object of class A (i.e. where your slot is defined).

That said, the signatures are wrong: a signal links to a slot only if the signature match

http://qt-project.org/doc/qt-4.8/signalsandslots.html

The signals and slots mechanism is type safe: The signature of a signal must match the signature of the receiving slot.

And your slot hasn't the right signature:

http://qt-project.org/doc/qt-4.8/qabstractbutton.html#clicked

void QAbstractButton::clicked ( bool checked = false ) [signal]