导致不完整类型的原因是什么?(QGraphicsItem:源或目标类型不完整)

时间:2022-03-05 19:37:53

I have a custom QGraphicsItem, that (among other things) changed the cursor to an open hand when clicked, using the standard procedure as described in the Qt documentation. This worked fine for the past two weeks or so. Yesterday, I changed a few things inside the class. Most significantly, I now subclass directly from QGraphicsPixmapItem instead of QGraphicsItem.

我有一个定制的QGraphicsItem,它(以及其他东西)使用Qt文档中描述的标准过程,在单击时将光标变成了一个张开的指针。在过去两周左右的时间里,这个方法很有效。昨天,我在班上换了几件东西。最重要的是,我现在直接从QGraphicsPixmapItem而不是QGraphicsItem子类化。

At some point I started to get the following error (partly my own translation):

在某个时候,我开始出现以下错误(部分是我自己翻译的):

C664: Conversion of parameter 1 from 'Qt::CursorShape' to 'const QCursor &' not possible. Source or target has incomplete type.

C664:参数1从'Qt::CursorShape'转换为'const QCursor &'不可能。源或目标具有不完整的类型。

I now try to figure out why my item has an incomplete type. Unfortunately I can't retrace when exactly this problem started to occur. The changed base class was my only guess, but I could not really think of a way how this could be the cause. After googling the error, I found examples, where members of the class were somewhat ill-defined, but I could not find an error in my declarations. So, here is the Header, in case I missed something:

我现在试着找出为什么我的项目有一个不完整的类型。不幸的是,我无法追溯这个问题是何时开始出现的。改变的基类是我唯一的猜测,但我真的想不出这是为什么。在google了错误之后,我找到了一些示例,其中类的成员有些不明确,但是我在声明中找不到错误。这是标题,以防我漏掉什么:

#include <QGraphicsPixmapItem>
class Collection;
class ItemSource;

class PhotoItem : public QGraphicsPixmapItem
{
public:
    PhotoItem( QString sourceFilePath, Collection *collection =0, QColor color =Qt::white, qreal size = 80.0);

    enum Orientation    { portrait, landscape };

    QPixmap content();
    bool hasContent();
    QColor color();

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    QRectF boundingRect() const;

    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);

private:
    qreal _size;
    Orientation _orientation;
    QPixmap _content;
    QColor _color;
    Collection *_collection;
    ItemSource *_source;
};

If this file is correct, then are there any common problems that lead to undefined types that I can check out? Maybe I am even searching at the wrong place?

如果这个文件是正确的,那么是否存在导致未定义类型的常见问题?也许我找错地方了?

Thanks for your time, Louise

谢谢你,露易丝

2 个解决方案

#1


9  

What leads to incomplete types?

导致不完整类型的原因是什么?

When instead of including the header file which defines a type, you add the line:

当不包含定义类型的头文件时,添加以下行:

class myclass;

It Forward declares the class myclass which means for compiler it is an Incomplete type. With Incomplete types, One cannot create objects of it or do anything which needs the compiler to know the layout of myclass more than the fact that myclass is just an type. i.e: The compiler does not know what are its members and what its memory layout is. But Since pointers to all objects need just the same memory allocation, You can use the forward declaration when just reffering to an Incomplete type as a pointer.

它向前声明了类myclass,这意味着编译器是不完整的类型。对于不完整的类型,不能创建它的对象或做任何需要编译器知道myclass布局的事情,而不能只知道myclass是一个类型。我。e:编译器不知道它的成员是什么,它的内存布局是什么。但是由于指向所有对象的指针只需要相同的内存分配,所以您可以使用forward声明作为指针返回到一个不完整的类型。

So in short, You are forward declaring a class and then performing some operations which need the compiler to know the layout of that class, Since the compiler doesn't know it hence the error.

因此,简而言之,您向前声明一个类,然后执行一些操作,这些操作需要编译器知道这个类的布局,因为编译器不知道它,因此出现了错误。

#2


2  

Forward declaration is usually fine in your headers, just make sure you include the class' header in your corresponding code file at the top (e.g. #include "itemsource.h").

前向声明通常在您的头文件中没有问题,只要确保在顶部的相应代码文件中包含类的头文件(例如#include“itemsource.h”)。

In response to the edit: If I am sure I've included my own headers correctly and I'm still getting incomplete type errors, then the next step would be to change #include <QGraphicsPixmapItem> for the whole Qt package. Unless there is a clearly identifiable benefit I always favour including the package completely. It just makes sure the inner dependencies of that package are satisfied.

作为对编辑的响应:如果我确定我已经正确地包含了我自己的头,并且我仍然得到不完整的类型错误,那么下一步将是为整个Qt包更改#include 。除非有明显的可识别的好处,否则我总是赞成把整个包装都包括进去。它只是确保该包的内部依赖关系得到满足。

And finally, in Qt 4.x it appears QGraphicsPixmapItem is in QtGui but has been moved out to QtWidgets in 5.x. So if you're using 4 try replacing #include <QGraphicsPixmapItem> with #include <QtGui> and for 5 try #include <QtWidgets>.

最后,在qt4中。看起来QGraphicsPixmapItem在QtGui中,但是在5.x中已经转移到QtWidgets。所以如果你用4替换#include 和#include 对于5,try #include

#1


9  

What leads to incomplete types?

导致不完整类型的原因是什么?

When instead of including the header file which defines a type, you add the line:

当不包含定义类型的头文件时,添加以下行:

class myclass;

It Forward declares the class myclass which means for compiler it is an Incomplete type. With Incomplete types, One cannot create objects of it or do anything which needs the compiler to know the layout of myclass more than the fact that myclass is just an type. i.e: The compiler does not know what are its members and what its memory layout is. But Since pointers to all objects need just the same memory allocation, You can use the forward declaration when just reffering to an Incomplete type as a pointer.

它向前声明了类myclass,这意味着编译器是不完整的类型。对于不完整的类型,不能创建它的对象或做任何需要编译器知道myclass布局的事情,而不能只知道myclass是一个类型。我。e:编译器不知道它的成员是什么,它的内存布局是什么。但是由于指向所有对象的指针只需要相同的内存分配,所以您可以使用forward声明作为指针返回到一个不完整的类型。

So in short, You are forward declaring a class and then performing some operations which need the compiler to know the layout of that class, Since the compiler doesn't know it hence the error.

因此,简而言之,您向前声明一个类,然后执行一些操作,这些操作需要编译器知道这个类的布局,因为编译器不知道它,因此出现了错误。

#2


2  

Forward declaration is usually fine in your headers, just make sure you include the class' header in your corresponding code file at the top (e.g. #include "itemsource.h").

前向声明通常在您的头文件中没有问题,只要确保在顶部的相应代码文件中包含类的头文件(例如#include“itemsource.h”)。

In response to the edit: If I am sure I've included my own headers correctly and I'm still getting incomplete type errors, then the next step would be to change #include <QGraphicsPixmapItem> for the whole Qt package. Unless there is a clearly identifiable benefit I always favour including the package completely. It just makes sure the inner dependencies of that package are satisfied.

作为对编辑的响应:如果我确定我已经正确地包含了我自己的头,并且我仍然得到不完整的类型错误,那么下一步将是为整个Qt包更改#include 。除非有明显的可识别的好处,否则我总是赞成把整个包装都包括进去。它只是确保该包的内部依赖关系得到满足。

And finally, in Qt 4.x it appears QGraphicsPixmapItem is in QtGui but has been moved out to QtWidgets in 5.x. So if you're using 4 try replacing #include <QGraphicsPixmapItem> with #include <QtGui> and for 5 try #include <QtWidgets>.

最后,在qt4中。看起来QGraphicsPixmapItem在QtGui中,但是在5.x中已经转移到QtWidgets。所以如果你用4替换#include 和#include 对于5,try #include