2011-03-11 48 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