C++ Qt signal and slot not firing

前端 未结 3 1602
慢半拍i
慢半拍i 2020-11-27 08:23

I am having difficulty in my Qt program with connecting button signals to my slots. My code is:

Main.cpp

#include 
#include         


        
相关标签:
3条回答
  • 2020-11-27 09:00

    Add Q_OBJECT to your class, like this:

    class MainWidget : public QWidget
    {
        Q_OBJECT
    

    You also have to run moc to generate some helper code. qmake does that automatically for your, but if you compile this yourself, you need to run moc.

    0 讨论(0)
  • 2020-11-27 09:00

    Edited:

    Compiled your code and all the slots were correctly called. It was just the Q_OBJECT macro that was missing.

    0 讨论(0)
  • 2020-11-27 09:16

    When I started with Qt, I had this problem a lot. As I see it your slots are defined wrong. If you look at the signature for the signal (Qt Clicked Signal Docs), you will see that the argument list is (bool clicked = false).

    The way Qt's signal & slots connect work at run time, is that it will only connect the signal and slot if they have the exact same signatures. If they don't match exactly, no connection.

    so in MainWidget.h

     public slots:
            void bAdvice_clicked(bool);
    

    In MainWidget.cpp

      connect(bAdvice, SIGNAL(clicked(bool)), this, SLOT(bAdvice_clicked(bool)));
    

    Things will start working for you.

    0 讨论(0)
提交回复
热议问题