Important: Please read the Qt Code of Conduct -

Overriding function from QGraphicsItemGroup without inheritance

  • Hello.

    I have made a simple class GraphicsView that inherits QGraphicsView, and that class is the centry widget of MainWindow class.

    The GraphicsView class has one simple assignment: to display QGraphicsView and set QGraphicsScene on QGraphicsView. In the QGraphicsScene I have a QGraphicsItemGroup that has two QGraphicsRectItem.

    I want to when I click on QGraphicsItem that QGraphicsRectItem change one another (one is visible and red, second is not visible and blue)

    So far I have made when I click on QGraphicsView it changes, but I want to make when I click on QGraphicsItemGroup it changes.

    I know that this can be done with inhereting QGraphicsItemGroup, but I am interesting to see is this possible without inheritance.


    [edit: fixed typo in title, Eddy]

  • Hello.
    Possible solution is to reimplement QGraphicsView's mousePressEvent. There you may use something like this:
    QGraphicsItemGroup *clicked_group;
    if (clicked_goup = qgraphicsitem_cast<QGraphicsItemGroup>(scene() ->itemAt(mapToScene (event ->pos())))) {
    // Code for making different items of group (in)visible. I don't know how actualy you prefer to do it.

Log in to reply