Nominate our 2022 Qt Champions!

Multiple GraphicsViews for one Scene

  • Hi,

    Windows 10 x64, Qt 4.8.1, Qt 5.6.2, MinGW 4.9.2, VisualStudio 2015

    I have a problem with a QGraphicsScene. I am using multiple graphicsview for one scene. I am changing the color of the items when item selection changes.
    When I am selecting items in the first view, the items are not painted as selected in the second view. When I am selecting items in the second view, it's the same. The items are always painted as selected in the first view but never in the second view.
    Everything else works fine, like moving or resizing items.

    Has anyone an idea?


  • Moderators

    @stvokr said in Multiple GraphicsViews for one Scene:

    I am changing the color of the items when item selection changes.

    how do you do that exactly?

  • My class MyItem inherits from GraphicsItem and I reimplemented the paint() function.

    void MyItem::paint( QPainter* painter, const QStyleOptionGraphicsItem*, QWidget* )
        QPen pen( Qt::green, 1 );
        if( isSelected() )
            pen.setColor( Qt::red );

  • Ok,

    I think this has something to do with the update call. The second view is not updated just like the first view, when the selection of the items changes.

    Another thing is very weird.
    I found out that all items, which are visible in the scene rect of the first view, are updated correctly (painted as selected). All other items which are not visible in the first view but in the second view are updated there.

    Or is this a Bug?


  • The code of QGraphicsItem::setSelected reveals that update() is called, so a repaint should be scheduled (unless isVisible() returns false, which should not be the case here).

    You could check the 40000 chips examples, which has multiple views, and see if it works there.

  • Hi,

    thanks for the hint.
    The example is working.

    I found the problem. I compared the example to my project and I called

    setCacheMode( DeviceCoordinateCache );

    in the construtor of my class MyItem (and I don't know why). And now it works.

    I think it was a copy and paste problem from an old project...
    And Closed.

Log in to reply