Weird “incomplete type” error in a QGraphicsItem [

2019-07-17 01:53发布

问题:

Possible Duplicate:
What leads to incomplete types? (QGraphicsItem: Source or target has incomplete type)

I started out with this question: What leads to incomplete types? (QGraphicsItem: Source or target has incomplete type)

As mentioned there, I got 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.

While trying to figure out why the item might be incomplete, I stripped it down to a minimal test case that still shows the error. Weird thing is: it is absolutely minimal...

Header:

#include <QGraphicsPixmapItem>

class PhotoItem : public QGraphicsPixmapItem
{
public:
    PhotoItem();
    void someMethod();

protected:
};

Implementation:

#include "photoitem.h"

PhotoItem::PhotoItem() : QGraphicsPixmapItem()
{
    QPixmap pxm(80, 80);
    pxm.fill(Qt::cyan);
    setPixmap( pxm );
}

void PhotoItem::someMethod()
{
    setCursor(Qt::OpenHandCursor);
}

It does not compile, giving the error as above. However, setting the cursor in the main method with item->setCursor(Qt::OpenHandCursor); works just fine. The error seems to be persistent across other QGraphicsItems (at least I tested QGraphicsRectItem).

I am utterly confused and don't really know, what to check next. Does the code above work on other machines/setups? What else could I test to get more information?

Thanks, Louise

回答1:

On your cpp, include the following line:

#include <QCursor>

The problem is that some class you are using forward declares QCursor (makes a forward declaration, it is... er, is it right to say 'forward declares'?). Qt::OpenHandCursor has this type, but the compiler does not know where the class QCursor is defined. Including on the cpp the file where the definition is made does the trick.

The reason why it works in your main function is probably because there you are including <QtGui>, or some other header that includes QCursor for you without you knowing about it.



回答2:

QGraphicsItems::setCursor expects a reference to an object of type QCursor, but you try to pass Qt::OpenHandCursor which is an enum element, i.e., a constant number which you can use to construct a specific QCursor instance.

I assume

setCursor(QCursor(Qt::OpenHandCursor));

will do what you want.

It would be interesting to know how your "item" is declared which you mentioned as working.