【问题标题】:Right way to inherit interface by interface按接口继承接口的正确方法
【发布时间】:2023-04-02 03:54:02
【问题描述】:

我有接口class IHistory。我想实现QAbstractTableModel。 我的代码调用undefined reference to vtable 错误,运行qmake 无法解决。

(ihistory.h)

class IHistory: public QAbstractTableModel
{
    ...
    // QAbstractItemModel interface
public:
    virtual int rowCount(const QModelIndex &parent) const Q_DECL_OVERRIDE ;
    virtual int columnCount(const QModelIndex &parent) const Q_DECL_OVERRIDE ;
    virtual QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE ;
    virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const Q_DECL_OVERRIDE ;
    ...
};

在子班我写了(history.h)

class History : public IHistory
{
    //Q_OBJECT
    Q_INTERFACES(IHistory)

public:
    History();
    ~History();

    ...
    // QAbstractItemModel interface
public:
    int rowCount(const QModelIndex &parent) const;
    int columnCount(const QModelIndex &parent) const;
    QVariant data(const QModelIndex &index, int role) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    ...
};

有没有正确的方法,或者我应该在class History中实现QAbstractTableModel

【问题讨论】:

    标签: qt interface qt-creator abstract-class qabstracttablemodel


    【解决方案1】:

    我找到了正确的方法! 只需删除接口中的重新定义,并在history.cpp中添加所有抽象函数的实现即可。

    所以,类似规则:

    如果你想从 QT 中的其他接口继承接口类,就像往常一样:class IFoo: public IBar。然后,当你实现接口IFoo

    1) 从接口继承:class Foo: public IFoo

    2) 按Alt + Enter 并选择Insert virtual methods of base classes

    3) 然后,对于每个方法,使用 Alt + Enter 并选择 Implement (methodname) in class Foo.cpp

    4) 利润!

    【讨论】:

      猜你喜欢
      • 2010-10-17
      • 2011-01-17
      • 2021-04-10
      • 2011-01-01
      • 2011-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多