Does Qt support virtual clean slots?

In my GUI project in Qt there are many classes of “configuration pages” that all inherit directly from QWidget .

I recently realized that all these classes have 2 public slots ( loadSettings() and saveSettings() ).

In this regard, I have two questions:

  • Does it make sense to write an intermediate base abstract class (lets call it BaseConfigurationPage ) with these two slots as virtual pure methods? (Every possible configuration page will always have these two methods, so I would say yes)
  • Before I start to change much in my code (if necessary): Qt supports virtual clean slots? Is there anything I should know about?

Here is a sample code that describes everything:

 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. public slots: void loadSettings(); void saveSettings(); }; 
+78
c ++ inheritance qt signals-slots
Jun 08 2018-10-06T00:
source share
2 answers

Yes, just like normal C ++ virtual methods. The code generated by the MOC invokes pure virtual slots, but this is normal, since the base class cannot be created anyway ...

Again, like normal regular C ++ virtual methods, a class cannot be created until the methods get an implementation.

One thing: in a subclass, you don’t need to mark overridden methods as slots at all. Firstly, they are already implemented as slots in the base class. Secondly, you just create more work for the MOC and the compiler, since you add a (tiny) bit more code. Trivial, but whatever.

So go for it.

+131
Jun 08 2018-10-06
source share

Only slots in 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(); }; 
-one
Nov 22 '13 at 8:20
source share



All Articles