简体   繁体   中英

Signal to it's own instance doesn't work

I connected the clicked(bool) event from QPushButton to a private slot mySlot() of my own Widget. But the slot is never called (I placed a breakpoint in mySlot()). I'm using c++ and Qt5. I wrote a minimal version of my code:

MyLayout.h

class MyLayout : public QWidget
{
Q_OBJECT
public:
    MyLayout(QWidget* parent = NULL);
private:
    QPushButton *next;
private slots:
    void mySlot();
}

MyLayout.cpp

MyLayout::MyLayout(QWidget* parent) : QWidget(parent)
{
    next = new QPushButton("Next Step");
    QObject::connect(next, SIGNAL(clicked(bool)), this, SLOT(mySlot()));
}
void MyLayout::mySlot() { /* do something */ }

Any ideas?

You created a parentless button and never showed it. Start by giving it parent ( this ), so it gets shown together with your widget:

next = new QPushButton("Next Step", this);

Then learn how to use layouts .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM