自定义QGraphicsItems不编译并给出“对象是私有的”错误 [英] Custom QGraphicsItems not compiling and gives "object is private" error
问题描述
我想要创建一个自定义QGraphicsItem按钮,如Fred 此处所示。他发布的代码可以在此处找到。
I am trying to create a Custom QGraphicsItem button as shown by Fred here. The code which he posted can be found here.
问题是当我尝试编译代码时,我得到以下两个错误:
The problem is when I try and compile the code I get the following two errors:
- /usr/include/qt4/QtGui/qgraphicsitem.h
'QGraphicsItem :: QGraphicsItem(const
QGraphicsItem&)'是私人的 - / usr /include/qt4/QtCore/qobject.h
'QObject :: QObject(const QObject&)'is
private
这里的代码片段基本上与上面的示例相同。错误是类减速。
Here is the code snippet which essentially is the same as that in the sample above. The error is on the class deceleration.
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);
....
}
有趣的是, href =http://fredyduarte.net/blog/?p=261 =nofollow>这里工作正常。您可以在此处找到示例代码。
Interestingly the other sample as shown here works fine. The sample code for that can be found here.
任何想法有什么问题?提前感谢。
Any idea what is wrong? Thanks in advance.
推荐答案
这些错误看起来像是试图复制按钮对象。编译器试图自动生成MyButton复制构造函数并失败,因为QObject的复制构造函数(这是你的按钮基础)是私有的。除了你列出的错误,你还应该看到如下:
Those errors looks like an attempt to copy the button object. Compiler tries to generate MyButton copy constructor automatically and fails, because copy constructor of QObject (which is your button base) is private. Besides the errors you listed you should also see stuff like:
note: synthesized method 'MyButton::MyButton(const MyButton&)' first required here
这个消息后的源文件名和行号。如果没有看到此消息,请尝试添加:
with source file name and line number following this message. If you don't see this message, try adding:
private:
Q_DISABLE_COPY(MyButton)
添加到MyButton类定义。然后你应该看到这个:
to the MyButton class definition. Then you should see this:
error: 'MyButton::MyButton(const MyButton&)' is private within this context
这篇关于自定义QGraphicsItems不编译并给出“对象是私有的”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!