Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Delete a QRect in QGraphicsView



  • Hello,
    Well i am trying to delete some rect from a QGraphicView but it is kind of hard to do it since i need to delete it when i double click it so i have to get the position first and then get the position of the cursor ( where it clicked twice ) and compare these two.
    I am not sure of what i have done ( cause it s not working ).
    So this is what it looks like :
    alt text
    The points ( red and blue ) are created randomly and they move randomly but when i want to delete them of course i stop it.

    • The red and blue point is a sub-class of QGraphicsRectItem
    • The lines in green is a sub-class of QGraphicsPixmapItem
    • All this is in a sub-class of QGraphicsView
      So this is what i have done so far :
      1- i can add as many "ants(blue/red points" as i want with a function, so whenever i create an "ant" i add the random position(X,Y) to an array , X to an array called antPosX and the Y one is antPosY. this is how i add the positions :
        antPosX[posIndex]=xCase;
        antPosY[posIndex]=yCase;
    

    and i increment posIndex of course. after that i have redefined the function for mouse double click (this is in the class1 QGraphicsView) :

    void InterfaceAutomate::mouseDoubleClickEvent(QGraphicsView *ev)
    {
        x = ev->pos().y();
        y = ev->pos().y();
        emit antClicked();
    }
    

    Then in MainWindow :

    connect(vueAutomate,&InterfaceAutomate::antClicked,this,&MainWindow::deleteAnt);
    

    and in deleteAnt, this is what i do: ( i change the window title just to see if the program got into this function )

        setWindowTitle("changed");
        int foundedIndexX=0;
        int foundedIndexY=0;
            for(int i=0;i<=vueAutomate->posIndex;i++){
                if(vueAutomate->x==vueAutomate->antPosX[i]){
                    foundedIndexX = i;
                }
            }
            for(int i=0;i<=vueAutomate->posIndex;i++){
                if(vueAutomate->y==vueAutomate->antPosY[i]){
                    foundedIndexY = i;
                }
            }
            if(foundedIndexY == foundedIndexX){}
            else{
                vueAutomate->SupprimerFourmi(); // deleteAnt
            }
    

  • Lifetime Qt Champion

    Hi,

    Why not re-implement mouseDoubleClickEvent from your custom QGraphicsRectItem ? There you can emit a signal that basically says "delete me" and connect a slot to it that will do the deletion from the scene.



  • Thanks @SGaist again for the reply
    Yes i have tried it but it does not seem to work. The base code is published for us and we have to modify it. That is why i ahve not tried what you said before.
    so basically i solved the problem and now i am displaying the posX and posY and also where the cursor clicked twice.
    this is what i get :

    QPointF(163.861,63.0236)
    16
    6
    

    QPointF is where the cursor clicked and btw i clicked on the first "point" created . and the second line is posX and last one is posY ( posX and posY is where the point is created for real )
    So i guess the cursor is giving me the coordinates while taking the window edges as reference . i can not figure out how i can fix it only on the scene .
    this is what i am using :

    QPointF mousePoint = mapToScene(event->pos());
    

  • Lifetime Qt Champion

    Something is not clear, what exactly did you implement that is working ?



  • The double Clicking was not working when i made this post, now it is working fine.


  • Lifetime Qt Champion

    You can use the QGraphicsScene::items to get the list of items under that point.

    Why not make the items selectable and QGraphicsScene::selectedItems ?



  • Thanks again @SGaist
    Really appreciate it


Log in to reply