c++ - Qt QLine类扩展

标签 c++ qt inheritance

我正在尝试扩展 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/

相关文章:

c++ - 使用 upper_bound 严格返回较小的元素

c++ - 需要从数据库中加载图像 C++

qt - 关闭主窗口并打开一个新窗口 - PyQt

c++ - Boost序列化多个对象

c++ - 静态/动态链接的静态初始化顺序问题

c++ - 在 C++ 中,当表达式涉及该对象时,将表达式分配给对象时是否有定义的操作顺序?

c++ - 两次设置 QApplication::style 后程序崩溃

java - 我可以在父类(super class)的数组上调用子类吗?

c# - 继承与收藏

ruby - 从 User 类继承的多个基于角色的类 - Rails 4