Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Deleting Ellipse from QGraphicsScene
Qt 6.11 is out! See what's new in the release blog

Deleting Ellipse from QGraphicsScene

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 848 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    star673
    wrote on last edited by
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • S star673

      @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?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #4

      @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 from QGraphicsItem, as do any objects you add onto a QGraphicsScene.

      S 1 Reply Last reply
      1
      • S star673

        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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @star673
        QGraphicsScene::clear() clears all QGraphicsItems 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.

        S 1 Reply Last reply
        1
        • JonBJ JonB

          @star673
          QGraphicsScene::clear() clears all QGraphicsItems 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.

          S Offline
          S Offline
          star673
          wrote on last edited by
          #3

          @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?

          JonBJ 1 Reply Last reply
          0
          • S star673

            @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?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #4

            @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 from QGraphicsItem, as do any objects you add onto a QGraphicsScene.

            S 1 Reply Last reply
            1
            • JonBJ JonB

              @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 from QGraphicsItem, as do any objects you add onto a QGraphicsScene.

              S Offline
              S Offline
              star673
              wrote on last edited by
              #5

              @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.

              JonBJ 1 Reply Last reply
              0
              • S star673

                @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.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #6

                @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 from scene->addEllipse() to a (member variable) FIFO QList<QGraphicsItem *>, so that later you can do something like

                delete scene->removeItem(list.takeFirst())
                
                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved