Qt是否支持虚拟纯插槽? [英] Does Qt support virtual pure slots?

查看:103
本文介绍了Qt是否支持虚拟纯插槽?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的GUI项目在 Qt 有很多配置页类,它们都直接从 QWidget 继承。

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

最近,我意识到所有这些类共享2个公用槽( loadSettings() saveSettings())。

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

关于这一点,我有两个问题:

Regarding this, I have two questions:


  • 将这两个插槽作为虚拟纯方法编写一个中间基础抽象类(让它名为 BaseConfigurationPage )有意义吗? (每个可能的配置页将始终有这两种方法,所以我会说是)

  • 在我对代码进行重大更改之前必须):Qt支持虚拟纯插槽?

  • 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();
};


推荐答案

是的,就像普通的c ++纯虚拟方法一样。 MOC生成的代码确实调用了纯虚拟槽,但是这是确定的,因为基类无法被实例化...

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...

再次,就像普通的c ++ pure虚方法,类不能被实例化直到方法被赋予一个实现。

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

有一件事:在子类中,你真的不需要标记overriden方法插槽。一,它们已经实现为基类中的槽。二,你只是为MOC和编译器创造更多的工作,因为你添加了一个(微小的)更多的代码。无论如何。

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

所以,去吧。

这篇关于Qt是否支持虚拟纯插槽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆