0
votes

I have an Qgraphicsscene implemented as a class, then i use QGraphicsScene::mousePressEvent to add a QGraphicsRectItem, this item have also an implementation for QGraphicsRectItem::mousePressEvent, the problem is that the event in the rect item is propagated to the scene and when i click it is added a new rect item, but i want is that the events inside this item do not propagate to the scene, i try event->accept but the event is propagated, how i cant do that? thanks for any help.

here is my qgraphicsscene code:

#include "imageview.h"

ImageView::ImageView(QWidget *parent){
    scene = new ImageScene(this);
    setScene(scene);
    //this->setMouseTracking(true);
    this->setInteractive(true);
}


ImageScene::ImageScene(QWidget *parent){
    current = NULL;
    selection = new QRubberBand(QRubberBand::Rectangle,parent);
    selection->setGeometry(QRect(10,10,20,20));
    setSceneRect(0,0,500,500);
}

void ImageScene::mousePressEvent(QGraphicsSceneMouseEvent *event){
    QGraphicsScene::mousePressEvent(event);
    /*IGNORING THIS EVENT FROM QGRAPHICSRECTITEM*/
    cout<<"image view"<<endl;
    if(this->selectedItems().length() == 0){ /*WORKS BUT IN SOME IMPLEMENTATION IS A PROBLEM (WHEN I DELETE THE ITEM WITH A DELETE BUTTON THE EVENT IS FIRED AND ADD A NEW ITEM .)*/
        origin = event->scenePos();
        selection->show();
    }
}
void ImageScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
    if(selection->isVisible() && selection->rect().width() >= 20 && selection->rect().height() >= 20){
        QGraphicsScene::mouseReleaseEvent(event);

        ResizableRect * rselection = new ResizableRect();
            //selection->origin = event->scenePos();
            //selection->grabMouse();
        cout<<"add"<<endl;
        this->addItem(rselection);
        rselection->setPos(selection->pos());
        rselection->setRect(0,0,selection->rect().width(),selection->rect().height());
    }
    selection->hide();

}
void ImageScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event){
    QGraphicsScene::mouseMoveEvent(event);
    if(selection->isVisible()){
        QPoint rorigin(origin.x(),origin.y());
        int xdes = event->scenePos().x();
        int ydes = event->scenePos().y();
        xdes = xdes > 0? xdes:0;
        ydes = ydes > 0? ydes:0;
        xdes = xdes < this->width()?xdes:this->width();
        ydes = ydes < this->height()?ydes:this->height();

        QPoint rdest(xdes,ydes);
        selection->setGeometry(QRect(rorigin,rdest).normalized());
    }

}
1
Where are the calls to event->accept() in your code?jmk
In the function of mousePressEvent in the ResizableRect class, this class extends QGraphicsRectItem.Diego Fernando Murillo Valenci
See here and here. Hope it helps.sashoalm
thaks for your help, i read the documentation as satuon says and i solve the problem using mouseReleaseEvent (not as i want but works) but the original mishap persists, leave this thread open to see who can determine why event-> accept in qGraphicsitem not stop the propagation to qGraphicsScene, maybe a bug in Qt?Diego Fernando Murillo Valenci

1 Answers

1
votes

Opposite to QWidgets, QGraphicsScene catches events before child items. It is described on Qt documentation.

For correctly work with that, use reimplementation of QGraphicsView instead of QGraphcisScene. Reimplement mousePressEvent there.

At that moment you can determine item under mouse pointer. it it is there - you can just call QGraphicsView::mousePressEvent(); It it is not - use your implementation for add new item.

It also allows you to separate behavior of different views.