1

While it is possible to automatically remove a QGraphicsTextItem from a scene using a timer and Qt's signal-slot mechanism like

QTimer::singleShot(1000, QGraphicsTextItem*, SLOT(deleteLater()));

other graphical objects (QGraphicsItem, QGraphicsEllipseItem) seem to not inherit QObject and as such cause an error when compiling:

error: C2664: 'QTimer::singleShot': Konvertierung des Parameters 2 von 'QGraphicsEllipseItem *' in 'QObject *' nicht m”glich

(conversion / cast of parameter 2 ... not possible)

Since I would like text and some graphics shown together for a limited time, my question is: How I can achieve automatic, timed removal of the above-mentioned 'other' objects?

4

1 回答 1

2

QGraphicsItems 不能QObject正常继承。您需要QGraphicsEllipseItem像这样子类化:

class AutoHidingItem : public QObject, public QGraphicsEllipseItem 
{
    Q_OBJECT
    // ...
}

或者您只需让场景跟踪要隐藏的项目,然后在需要时将其隐藏。(在您的子类场景或视图中创建一个插槽以隐藏或删除该项目。)

编辑:@thuga 指出QGraphicsEllipseItem不继承QObject,而QGraphicsTextItem已经继承。编辑答案以显示这一点。

希望有帮助。

于 2013-05-22T17:33:29.967 回答