Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ QPushButton signal and slot

I have a problem creating a QPushButton and linking its signal to my slots. First, I created a class with the slot:

class A : public QWidget{

  public slots:
    void handleButton();

};

There is my handleButton function in the .cpp

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

Then I want to connect the button.

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

But I got an error when I start the application:

"No such slot"
like image 481
Aurimas_12 Avatar asked Oct 19 '22 22:10

Aurimas_12


2 Answers

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]
like image 160
Marco A. Avatar answered Oct 22 '22 11:10

Marco A.


You have a few errors in this code, if you define "void handlebutton()" then you must implement void handlebutton() NOT void handlebutton(inx x, int y) this code should not even compile.

More: in QT you CAN ONLY connect SIGNALS and SLOTS with the same parameters so you can connect SIGNAL(clicked()) with SLOT(handlebutton()) but not SIGNAL(clicked() with SLOT(handleButton(int, int)).

Another problem is that connect is executed at runtime so You must compile and run before Qt can show you the error.

So a possible solution is:

define and implement the slot void handlebutton() and connect that to the signal clicked(), then define another method handleButton (int x, int y) that you will call from inside handleButton().

I really hope that makes sense to you.

like image 27
Marco Avatar answered Oct 22 '22 11:10

Marco