Does Qt support virtual pure slots?

前端 未结 2 1375
借酒劲吻你
借酒劲吻你 2021-01-30 12:16

My GUI project in Qt has a lot of \"configuration pages\" classes which all inherit directly from QWidget.

Recently, I realized that all these

相关标签:
2条回答
  • 2021-01-30 12:41

    Only slots in the BaseConfigurationPage

    class BaseConfigurationPage : public QWidget
    {
      // Some constructor and other methods, irrelevant here.
    
      public slots:
    
        virtual void loadSettings() = 0;
        virtual void saveSettings() = 0;
    };
    
    class GeneralConfigurationPage : public BaseConfigurationPage
    {
      // Some constructor and other methods, irrelevant here.
    
        void loadSettings();
        void saveSettings();
    };
    
    0 讨论(0)
  • 2021-01-30 12:51

    Yes, just like regular c++ pure virtual methods. The code generated by MOC does call the pure virtual slots, but that's ok since the base class can't be instantiated anyway...

    Again, just like regular c++ pure virtual methods, the class cannot be instantiated until the methods are given an implementation.

    One thing: in the subclass, you actuallly don't need to mark the overriden methods as slots. First, they're already implemented as slots in the base class. Second, you're just creating more work for the MOC and compiler since you're adding a (tiny) bit more code. Trivial, but whatever.

    So, go for it..

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