gpt4 book ai didi

c++ - 自定义 QGraphicsItems 未编译并给出 "object is private"错误

转载 作者:太空宇宙 更新时间:2023-11-04 12:25:40 25 4
gpt4 key购买 nike

我正在尝试创建自定义 QGraphicsItem 按钮,如 Fred here 所示.可以找到他发布的代码 here .

问题是当我尝试编译代码时出现以下两个错误:

  • /usr/include/qt4/QtGui/qgraphicsitem.h'QGraphicsItem::QGraphicsItem(常量QGraphicsItem&)’是私有(private)的
  • /usr/include/qt4/QtCore/qobject.h
    ‘QObject::QObject(const QObject&)’是私有(private)

这是与上面示例中的代码片段基本相同的代码片段。错误在类减速上。

class MyButton : public QObject, public QGraphicsItem
{
Q_OBJECT
Q_INTERFACES(QGraphicsItem)

public:
MyButton(QGraphicsItem *parent = 0);
MyButton(const QString normal, const QString pressed = "", QGraphicsItem *parent = 0);
....
}

有趣的是,另一个示例如图所示 here工作正常。可以找到其示例代码 here .

知道哪里出了问题吗?提前致谢。

最佳答案

这些错误看起来像是试图复制按钮对象。编译器尝试自动生成 MyButton 复制构造函数但失败了,因为 QObject(这是您的按钮基础)的复制构造函数是私有(private)的。除了您列出的错误之外,您还应该看到如下内容:

note: synthesized method 'MyButton::MyButton(const MyButton&)' first required here

此消息后有源文件名和行号。如果您没有看到此消息,请尝试添加:

private:
Q_DISABLE_COPY(MyButton)

MyButton 类定义。然后你应该看到这个:

error: 'MyButton::MyButton(const MyButton&)' is private within this context

关于c++ - 自定义 QGraphicsItems 未编译并给出 "object is private"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2678661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com