QGraphicsSimpleTextItem “invalid use of incomplete type”

寵の児 提交于 2019-12-31 07:16:05

问题


My code is as follows:

pointerwidget.h

QGraphicsSimpleTextItem *text;

pointerwidget.cpp

void PointerWidget::placeNumbers(float spacing, int currentTickNumber)
{
    float label = spacing/scaleFactor;
    text = scene->addSimpleText(QString::number(label),QFont("Comic Sans MS", 12, QFont::Bold));
    text->setPos(currentTickNumber*spacing,30);
}

Everywhere that i read says to use setPos(x,y) to position the text item. This is a function of QGraphicsItem. I get the error of "invalid use of incomplete type 'class QGraphicsSimpleTextItem'". To further confirm that something is wrong, Qt Creator doesn't "recognize" or attempt to "auto-complete" the function call.

If it comment out the line that is giving me the error then i get a bunch of simple texts located at (0,0). Point being, it is not the addSimpleText() function that is giving me the error.

Any ideas?

Thanks!


回答1:


You need to include the QGraphicsSimpleTextItem include file:

#include <QGraphicsSimpleTextItem>

You must have a forward declaration to the class somewhere so that the pointer can be declared in your include file, but the actual class definition isn't included, so the compiler doesn't know what to do with your text variable.



来源:https://stackoverflow.com/questions/42658551/qgraphicssimpletextitem-invalid-use-of-incomplete-type

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