虽然可以使用定时器和 Qt 的信号槽机制从场景中自动删除 QGraphicsTextItem,例如
QTimer::singleShot(1000, QGraphicsTextItem*, SLOT(deleteLater()));
其他图形对象(QGraphicsItem、QGraphicsEllipseItem)似乎没有继承QObject,因此在编译时会导致错误:
error: C2664: 'QTimer::singleShot': Konvertierung des Parameters 2 von 'QGraphicsEllipseItem *' in 'QObject *' nicht m”glich
(conversion / cast of parameter 2 ... not possible)
由于我希望在有限的时间内同时显示文本和一些图形,所以我的问题是: 如何实现自动定时清除上述“其他”对象?
最佳答案
QGraphicsItem
通常不会继承 QObject
。您需要像这样子类化 QGraphicsEllipseItem
:
class AutoHidingItem : public QObject, public QGraphicsEllipseItem
{
Q_OBJECT
// ...
}
或者您只需让场景跟踪要隐藏的项目,然后在需要时将其隐藏。 (在隐藏或删除项目的子类场景或 View 中创建一个插槽。)
编辑:@thuga 指出 QGraphicsEllipseItem
不继承 QObject
,而 QGraphicsTextItem
已经继承。编辑答案以显示这一点。
希望对您有所帮助。
关于c++ - Qt 使用 singleShot Timer 从 QGraphicsScene 中删除 QGraphicsItem?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16697368/