简体   繁体   English

Qt接口类

[英]Qt interfaces class

How can I create an interface class, something like this : 如何创建接口类,如下所示:

template<typename file_system_t>
    class ireciver_intervace : public QObject
    {
        public:
        typedef typename file_system_t::file_system_item file_system_item;
    public Q_SLOTS:
        virtual void dataChangeItem(file_system_item *item)=0;
        virtual void removeItem(file_system_item *item)=0;
        virtual void insertItem(file_system_item *item)=0;
    };

and derived class 和派生类

class FileItemModel:public QAbstractItemModel ,public ireciver_intervace<file_system>
        {
            Q_OBJECT
        }

When I inherit from this class I get an error mentioning ambiguous conversions. 当我继承本课程时,我会收到一个错误,提到模糊的转换。 I understand this is the right behavior of the compiler, but how can I get interfaces slots for my future classes? 我理解这是编译器的正确行为,但是如何为未来的类获取接口槽? Maybe I must use the Q_DECLARE_INTERFACE macro? 也许我必须使用Q_DECLARE_INTERFACE宏?

QObject subclasses with signal/slot functionality can't be templated. 具有信号/槽功能的QObject子类无法模板化。

You can simply drop the QObject inheritance of your interface, the rest will be fine. 您可以简单地删除接口的QObject继承,其余的都可以。 It's no problem to define a (pure) virtual function and then (in the subclass) make a slot out of it by putting it in the slots section of your subclass: 定义一个(纯)虚函数然后(在子类中)通过将它放在子类的slots部分中来创建一个槽是没有问题的:

template<typename file_system_t>
class ireceiver_interface
{
public:
    typedef typename file_system_t::file_system_item file_system_item;

    // pure virtual functions (NO SLOTS!):
    virtual void dataChangeItem(file_system_item *item)=0;
    virtual void removeItem(file_system_item *item)=0;
    virtual void insertItem(file_system_item *item)=0;
};

class FileItemModel: public QAbstractItemModel,
                     public ireceiver_interface<file_system_t>
{
    Q_OBJECT
    ...
public slots:
    // implementations (slots):
    virtual void dataChangeItem(file_system_item *item);
    virtual void removeItem(file_system_item *item);
    virtual void insertItem(file_system_item *item);
    ...
}

However, the idea of an interface is to define what functions a concrete class has to implement. 但是,接口的概念是定义具体类必须实现的功能。 Here, the interface can't force the subclass to define those functions as slots , it can also define them as non-slot functions . 这里,接口不能强制子类将这些函数定义为槽 ,它也可以将它们定义为非槽函数 This minor problem can't be solved. 这个小问题无法解决。 Even if the interface could be a QObject, the subclass could then decide to re-implement those methods as non-slots! 即使接口可能是QObject,子类也可以决定将这些方法重新实现为非槽!

Note that you have two typos in your class name: It's receiver , not reciver and it's interface , not intervace . 请注意,您有两个错字在你的类名:这是接收器 ,不reciver和它的接口 ,而不是intervace。

You can't inherit from more than one QObject . 您不能从多个QObject继承。 However, both classes you inherit from are QObjects . 但是,您继承的两个类都是QObjects

Some possibilities you could consider are the following: 您可以考虑的一些可能性如下:

  • Let the class ireciver_intervace inherit from QAbstractItemModel instead of QObject . 让类ireciver_intervace继承自QAbstractItemModel而不是QObject This way, FileItemModel can simply inherit from ireciver_intervace . 这样, FileItemModel可以简单地从ireciver_intervace继承。 (may not be possible depending on what you want to achieve) (可能无法取决于你想要达到的目标)
  • Use delegation for the QAbstractItemModel instead of inheritance. 使用委托进行QAbstractItemModel而不是继承。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM