Does Qt support virtual pure slots?

32,666

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();
};
Share:
32,666

Related videos on Youtube

ereOn
Author by

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, 2021

Comments

  • ereOn
    ereOn almost 3 years

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

    Recently, I realized that all these classes share 2 commons slots (loadSettings() and saveSettings()).

    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();
    };
    
  • ereOn
    ereOn almost 14 years
    Thanks for your precise answer ! I'll test this as soon as possible ;)
  • fmuecke
    fmuecke over 11 years
    Removing the slot specification from the subclass prevents moc from calling subclass AND base class! - Thanks man!
  • Kyle Strand
    Kyle Strand almost 8 years
    In Qt 5, at least, if you're using the obj-ptr, member-func-ptr, obj-ptr, member-func-ptr version of connect, none of your slots need to be declared as such.
  • bardao
    bardao over 5 years
    have 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
    SPlatten over 3 years
    I'm doing the same but I get 'slot name' overrides a member function but is not marked 'override'
  • Parker Coates
    Parker Coates almost 3 years
    Yes, loadSettings and saveSettings should be declared with override in GeneralConfigurationPage to ensure they actually override something.
  • Vinci
    Vinci over 2 years
    In 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".