Qt Connecting SIGNAL and SLOT in object member of MainWindow

后端 未结 2 1293
一生所求
一生所求 2021-01-24 12:34

I have a class MyClass with:

  - private: 
      pushButton *button;
      void connectSignalAndSlot();
  - private slot: 
      void buttonAction();


        
相关标签:
2条回答
  • 2021-01-24 13:05

    You must have MyClass inherit from QObject AND add Q_OBJECT macro in your MyClass definition (header file) to have slots/signals work.

    class MyClass : public QObject
    {
         Q_OBJECT
    
    public:
         ....
    };
    
    0 讨论(0)
  • 2021-01-24 13:22

    Inheriting QObject is the right way, but your still missing Qt-Meta Object Code. Your header-file for your class should look like this:

    #ifndef MYCLASS_H
    #define MYCLASS_H
    
    class MyClass : public QObject {
        Q_OBJECT
        // your methods, variables, slots and signals
    }
    
    #endif
    

    Don't forget to create the moc file, the easiest way is to use qmake or the QtCreator IDE.

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