2011-03-11 47 views
0

我想扩展QLine类以包含颜色属性。我使用QCreator为新类QLineColor创建代码,并在公共数据中添加了属性char color = 0。这里是由QCreator生成的代码。Qt QLine类扩展

更新:根据关于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; 
} 

回答

1

QLine不会从QObject派生。因此Q_OBJECT等都是未定义的。

#include <QLine> 
class QLineColor : public QLine 
{ 
    QLineColor(); 
    char color; 
}; 

应该工作。

+0

哈哈thx ... QtCreator有问题... – user623879 2011-03-11 08:53:33

2

要包括Q_OBJECT宏内部类的定义,类必须继承QObject

#include <QLine> 
#include <QObject> 

class QLineColor : public QObject, public QLine 
{ 
    Q_OBJECT 

编辑

您需要包括Q_OBJECT宏,如果你使用的信号和槽机制与你的类。如果您不使用信号和插槽,则可以省略Q_OBJECT