0
votes

The idea is that the user clicks on a shape and the information of the shape is shown on a table. This works well if the user selects the shape (drag the mouse over the shape). I'm trying to modify this code to do that action, but not lucky. This is what I'm doing for the select mode:

I have a a call in the:

void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

When the mouse is released I will update the data:

//enter the mode for selecting   
if(theMode == SelectObject){

 if (this->items().isDetached()){

//we check if the object is selected
    if (this->selectedItems().isEmpty()){
        qDebug() << "not selected";
        isSelected = false;
        return;
    }

 //we get a list of the shapes
    QList<QGraphicsItem*> stackOfShapes = this->items();

 //we get index of selected shape
    QGraphicsItem *selected = this->selectedItems().first();
    int indexOfShape = stackOfShapes.indexOf(selected);

 //we see which shape is (For a Rectangle)

    switch (selected->type()) {

        case 346:
        {
        updateDataOfRect();
        }
            break;
    }

}

The problem is that:

//we get index of selected shape
QGraphicsItem *selected = this->selectedItems().first();

How to do this when the shape is clicked not selected?

I tried to modify the subclass of the shape in the mousePressEvent :

if (event->button() == Qt::MouseButton::LeftButton) {
        this->setSelected(true);
}

Can any one help to find a solution?

Thanks.

1

1 Answers

0
votes
QList<QGraphicsItem *> QGraphicsView::items(const QPoint &pos) const

Returns a list of all the items at the position pos in the view. The items are listed in descending stacking order (i.e., the first item in the list is the uppermost item, and the last item is the lowermost item). pos is in viewport coordinates.

Example use by overloading QGraphicsView::mousePressEvent():

void CustomView::mousePressEvent(QMouseEvent *event) {
    qDebug() << "There are" << items(event->pos()).size()
             << "items at position" << mapToScene(event->pos());
}