Deleting Ellipse from QGraphicsScene
-
Hello all,
I would like to make an application which draws Circle randomly. The number of circle should be always specific number. I have created the timer and each timeout I would like to draw a circle. When the number of circle exceed the specigic number, I would like to delete the the circle respectively. The first drawing circle should be deleted firtsly than very timeout I would like to draw new circle. I have used the clear() function but it deleted the graphicsScene.
Here you can see the timeout slot function.void MainWindow::CircleDrawTestSlot(){ CircleCounter++; qreal randomValuePosX = qrand() % (100); qreal randomValuePosY = qrand() % (100); scene->addEllipse(randomValuePosX,randomValuePosY,40,40,QPen(Qt::red)); if(CircleCounter>4){ scene->clear(); CircleCounter=0; }
In the constructor, I have created the timeout mechanism like below.
timerTarget = new QTimer(this); connect(timerTarget,SIGNAL(timeout()),this,SLOT(CircleDrawTestSlot())); timerTarget->start(1000);
How should I make this? Thanks in advanced.
-
@star673 said in Deleting Ellipse from QGraphicsScene:
I did not create any QGraphicsItem, I just use "addEllipse()" fucntion
Function definition is:
QGraphicsEllipseItem *QGraphicsScene::addEllipse()
.
QGraphicsEllipseItem
derives fromQGraphicsItem
, as do any objects you add onto aQGraphicsScene
. -
@star673
QGraphicsScene::clear()
clears allQGraphicsItem
s off the scene, as its name suggests.You need to call void QGraphicsScene::removeItem(QGraphicsItem *item) on the item/ellipse to remove it. Maintain a FIFO list of the items you add to the scene to know which one to delete next.
-
@star673 said in Deleting Ellipse from QGraphicsScene:
I did not create any QGraphicsItem, I just use "addEllipse()" fucntion
Function definition is:
QGraphicsEllipseItem *QGraphicsScene::addEllipse()
.
QGraphicsEllipseItem
derives fromQGraphicsItem
, as do any objects you add onto aQGraphicsScene
. -
@star673 said in Deleting Ellipse from QGraphicsScene:
delete ellipseItem1;
May work, but it's not how I would do it. I would do
delete scene->removeItem(ellipseItem1);
Maybe that is equivalent to
delete ellipseItem1;
, I don't know.You have not said anything about/implemented your original:
When the number of circle exceed the specigic number, I would like to delete the the circle respectively.
for which as I said you would need to
append()
the ellipses created fromscene->addEllipse()
to a (member variable) FIFOQList<QGraphicsItem *>
, so that later you can do something likedelete scene->removeItem(list.takeFirst())