C++ 中的接口对于 Qt
我的问题是,我的接口可以从 QObject 继承吗?该怎么做?嗯,我知道 C++ 中的接口只是仅包含虚拟方法的类,并且通常类可以从超类继承。但如果我这样做,由于 QObject 引用不明确,我会收到错误。我需要继承 QObject 才能将信号/槽功能添加到我的插件中。
我的接口
#ifndef LABELINTERFACE_H
#define LABELINTERFACE_H
#include <QLabel>
#include <QObject>
class LabelInterface : public QObject {
public :
virtual ~LabelInterface() {}
virtual QLabel* newLabel() = 0;
public slots:
virtual void setLabelText() = 0;
};
Q_DECLARE_INTERFACE (LabelInterface,"com.stefan.Plugin.LabelInterface/1.0")
#endif // LABELINTERFACE_H
插件头文件
#ifndef LABELPLUGIN_H
#define LABELPLUGIN_H
#include "labelinterface.h"
class LabelPlugin : public LabelInterface
{
Q_OBJECT
Q_INTERFACES(LabelInterface)
public:
QLabel* label;
QLabel* newLabel();
LabelPlugin() {}
~LabelPlugin() {}
public slots:
void setTextForLabel();
};
#endif // LABELPLUGIN_H
实现文件
#include <QtGui>
#include "labelplugin.h"
QLabel* LabelPlugin::newLabel() {
label = new QLabel("This plugin works");
return label;
}
void LabelPlugin::setTextForLabel() {
label->setText("This plugin works fine");
}
// Exporta plugin-ul
Q_EXPORT_PLUGIN2 (labelplugin,LabelPlugin)
我收到错误
labelplugin.cpp:18: error: cannot allocate an object of abstract type ‘LabelPlugin’
My question is , can my interface inherit from QObject and how to do that ? Well , i know that interfaces in C++ are simply classes that contains only virtual methods , and normally a class can inherit from a superclass. But if i do so i get an error due to ambiguous QObject references. I need to inherit QObject to add signals /slot feature to my plugins.
My interface
#ifndef LABELINTERFACE_H
#define LABELINTERFACE_H
#include <QLabel>
#include <QObject>
class LabelInterface : public QObject {
public :
virtual ~LabelInterface() {}
virtual QLabel* newLabel() = 0;
public slots:
virtual void setLabelText() = 0;
};
Q_DECLARE_INTERFACE (LabelInterface,"com.stefan.Plugin.LabelInterface/1.0")
#endif // LABELINTERFACE_H
Plugin header file
#ifndef LABELPLUGIN_H
#define LABELPLUGIN_H
#include "labelinterface.h"
class LabelPlugin : public LabelInterface
{
Q_OBJECT
Q_INTERFACES(LabelInterface)
public:
QLabel* label;
QLabel* newLabel();
LabelPlugin() {}
~LabelPlugin() {}
public slots:
void setTextForLabel();
};
#endif // LABELPLUGIN_H
Implementation file
#include <QtGui>
#include "labelplugin.h"
QLabel* LabelPlugin::newLabel() {
label = new QLabel("This plugin works");
return label;
}
void LabelPlugin::setTextForLabel() {
label->setText("This plugin works fine");
}
// Exporta plugin-ul
Q_EXPORT_PLUGIN2 (labelplugin,LabelPlugin)
I get error
labelplugin.cpp:18: error: cannot allocate an object of abstract type ‘LabelPlugin’
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你忘记实施
你实施了
这是一个错字吗?要实例化一个类,您需要重写并实现基类中的所有纯虚方法。由于您没有这样做,因此您的类仍然是抽象的。
You forgot to implement
You implemented
was that a typo? To instantiate a class you need to override and implement all pure virtual methods in the base class. Since you're not doing that, you class remains abstract.