我正在尝试扩展 QLine 类以包含颜色属性。我使用 QCreator 为新类 QLineColor 创建代码,我只是在公共(public)数据中添加了属性 char color=0。这是 QCreator 生成的代码。
更新:根据有关 QObject 的响应进行了修改。但现在我遇到了一些其他错误:
/home/james/qtsdk-2010.05/qt/include/QtCore/qobject.h:309: error:
‘QObject::QObject(const QObject&)’ is private
within this context
and it lists several qt/include directories
文件:QLineColor.h
#ifndef QLINECOLOR_H
#define QLINECOLOR_H
#include <QLine>
#include <QObject>
class QLineColor : public QObject, public QLine
{
Q_OBJECT
public:
explicit QLineColor(int x1, int y1, int x2, int y2, char color);
char color;
};
#endif // QLINECOLOR_H
文件:qlinecolor.cpp
#include "qlinecolor.h"
QLineColor::QLineColor(int x1, int y1, int x2, int y2, char color) :
QLine(x1, y1, x2, y2)
{
color = 0;
}
最佳答案
要在类定义中包含 Q_OBJECT
宏,该类必须继承 QObject
:
#include <QLine>
#include <QObject>
class QLineColor : public QObject, public QLine
{
Q_OBJECT
编辑
如果您在类中使用信号和槽机制,则需要包含 Q_OBJECT
宏。如果您不使用信号和槽,则可以省略 Q_OBJECT
。
关于c++ - Qt QLine类扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5270546/