Weird “incomplete type” error in a QGraphicsItem [duplicate]

折月煮酒 提交于 2019-12-12 22:08:39

问题


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.



来源:https://stackoverflow.com/questions/10122219/weird-incomplete-type-error-in-a-qgraphicsitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!