问题
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.
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.
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:
#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
回答1:
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.
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:
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"
).
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.
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>
.
来源:https://stackoverflow.com/questions/10108720/what-leads-to-incomplete-types-qgraphicsitem-source-or-target-has-incomplete