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.
-
@JonB
Thanks for reply. I did not create any QGraphicsItem, I just use "addEllipse()" fucntion to QGraphicsScene. Do I have to create an QGraphicsItem object for each circle?@star673 said in Deleting Ellipse from QGraphicsScene:
I did not create any QGraphicsItem, I just use "addEllipse()" fucntion
Function definition is:
QGraphicsEllipseItem *QGraphicsScene::addEllipse().
QGraphicsEllipseItemderives fromQGraphicsItem, as do any objects you add onto aQGraphicsScene. -
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
QGraphicsScene::clear()clears allQGraphicsItems 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
QGraphicsScene::clear()clears allQGraphicsItems 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.
-
@JonB
Thanks for reply. I did not create any QGraphicsItem, I just use "addEllipse()" fucntion to QGraphicsScene. Do I have to create an QGraphicsItem object for each circle?@star673 said in Deleting Ellipse from QGraphicsScene:
I did not create any QGraphicsItem, I just use "addEllipse()" fucntion
Function definition is:
QGraphicsEllipseItem *QGraphicsScene::addEllipse().
QGraphicsEllipseItemderives fromQGraphicsItem, as do any objects you add onto aQGraphicsScene. -
@star673 said in Deleting Ellipse from QGraphicsScene:
I did not create any QGraphicsItem, I just use "addEllipse()" fucntion
Function definition is:
QGraphicsEllipseItem *QGraphicsScene::addEllipse().
QGraphicsEllipseItemderives fromQGraphicsItem, as do any objects you add onto aQGraphicsScene. -
@JonB
Thanks. I added like below.QGraphicsItem *ellipseItem1; ellipseItem1 = scene->addEllipse(iSecretX,iSecretY,40,40,QPen(Qt::red));After the process done, I delete like object.
delete ellipseItem1;It worked.
@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())