Important: Please read the Qt Code of Conduct -

Item paint() on every re-call to addItem() to QGraphicsScene

  • Hi,

    In my code, I holding references of all items that are added to scene in a QList, on some event I parse the QList and call scene->addItem(). With this call, item's paint() is getting invoked which is perfect. Now, user clicks "redraw" button on selected items to redraw all selected items at other selected position, on which I do removeItem() for selected items and again do addItem() at the user specified position (mouseclick). But in this case, I see paint() of item is not getting called. Paint() is getting invoked for any new item I create but not for items that were removed and added again.

    Note: I'm not unref the item when doing removeItem(). I want that item reference and I just update item co-ordinates as per user mouse click and wil try to redraw the item at specified position. So, I use setPosition() of item and call addItem() again. Item is getting moved to new position but paint() is not getting called. I cant use scene->clear() because it appears to unref the item even.

    My aim is to remove it from scene and re-add it at given position with paint() called. Currently, item is getting added at given position but its paint() is not getting invoked 2nd time.

    Please help me.

  • I don't 100% understand your use case, but normally, you wouldn't have to remove and re-add the item.

    If you just want to move it, call setPos.
    If you want to give it another parent, call setParent and then setPos to position it correctly in respect to it's new parent.
    If you need to check whether the item is already in the scene, use scene()

    One reason why paint might not be called is caching. Check the cacheMode() of the item.

  • Thanks for the reply. My project involves drawing lines and nodes. And condition to be satisified is if line is open-ended, the ends shuld be marked by different color. If line is joined by other line it should be painted in other color. Hence, I need to have paint function called on every line draw so that I see if line is open-ended or not. But thanks anyways, cacheMode() worked!.

Log in to reply