Does Qt support virtual pure slots?
Solution 1
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..
Solution 2
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();
};
Related videos on Youtube
ereOn
I love Golang, networking and security. If you need an open-source, free, multi-platform, VPN solution, you may want to check http://www.freelan.org which is basically how I spend my free time.
Updated on June 12, 2021Comments
-
ereOn almost 3 years
My GUI project in
Qt
has a lot of "configuration pages" classes which all inherit directly fromQWidget
.Recently, I realized that all these classes share 2 commons slots (
loadSettings()
andsaveSettings()
).Regarding this, I have two questions:
- Does it make sense to write a intermediate base abstract class (lets name 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 do the heavy change in my code (if I have to) : does Qt support virtual pure slots ? Is there anything I should be aware of ?
Here is a code example describing 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(); };
- Does it make sense to write a intermediate base abstract class (lets name it
-
ereOn almost 14 yearsThanks for your precise answer ! I'll test this as soon as possible ;)
-
fmuecke over 11 yearsRemoving the slot specification from the subclass prevents moc from calling subclass AND base class! - Thanks man!
-
Kyle Strand almost 8 yearsIn Qt 5, at least, if you're using the
obj-ptr, member-func-ptr, obj-ptr, member-func-ptr
version ofconnect
, none of your slots need to be declared as such. -
bardao over 5 yearshave to add some really weird behavior: when you mark the overriden methods as slots in the header of the subclass, slots get called all the time even with 0 connections to them. Go figure!!!
-
SPlatten over 3 yearsI'm doing the same but I get 'slot name' overrides a member function but is not marked 'override'
-
Parker Coates almost 3 yearsYes,
loadSettings
andsaveSettings
should be declared withoverride
inGeneralConfigurationPage
to ensure they actually override something. -
Vinci over 2 yearsIn my experience (Qt6) you absolutely must not mark the overriden methods as slots. A little test example I've written with a Q_PROPERTY and virtual slots produced a "TypeError: Cannot assign to read-only property" error whenever I've marked the accessor methods in the derived class as "slots".