Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Updating QGraphicsScene



  • Hi,

    I have created an application based on a QFrame. This frame contains a QGraphicsScene which, in turn, includes a QGraphicsScene.

    I add "points" (QGraphicsItems) to this QGraphicsSCene. Until now, I always painted this points using the same color. But now I need to change the color these points are painted according to different color tables and the value of some attributes I attach to the points.

    I have added two buttons to my frame to switch between two color tables. When I click on any of these, I select the corresponding color table so the paint method of the QGraphicsItems (the points) selects the appropriate color to be used to draw the points on the screen.

    All this seem to work but for the fact that I'm not able to refresh properly the scene / view / frame once I have changed the color table. I have, for instance, to change the size of the main window to get the scene refreshed.

    This shows me that I'm properly changing color tables and that the points select the appropriate colors from it, but, I insist, I haven't found a way to make the changes visible without having to resize / hide & show / cover & uncover my application.

    I tried several methods to force the update:

    • Calling the update() method of the QFrame.

    • Calling the update() method of the QGraphicsView (no parametres).

    • Obtaining the viewport (scene coordinates) of the QGraphicsView and calling its invalidateScene(QRectF) method.

    • Obtaining the viewport (view coordinates) of the QGraphicsView and calling its invalidateSCene(QRectF) method.

    • Obtaining the center of the viewport (scene coordinates) of the QGraphicsView and calling its centerOn(x, y) method.

    None of these attempts worked at all.

    Can you suggest a way to do this?

    Thank you!



  • How about calling update() of the affected QGraphicsItems?


  • Lifetime Qt Champion

    Hi,

    Can you provide a minimal compilable example that shows that behaviour ?

    By the way;

    • What version of Qt are you using ?
    • On what platform ?


  • @SGaist ,

    thank you very much for your answer.

    The version of Qt I'm using is 5.9.2. The platform Windows 10 1909 with all patches applied.

    I'm working on a minimal working, compilable example. However, and since the application is a rather complex one, this will imply that at least 18 files should be published here. And yes, I insist, this would be the minimalistic thing I can produce if I don't want to reduce my app so much than the possible mistakes I'm making are removed, so your help would be... of no help, since it would be impossible to find what I'm doing wrong.

    For me posting so many files as code is not a problem, but maybe the resulting post will be just huge. Is there a way to attach a .zip file to the post? I'm not able to find such an option in the interface. Of course, I could upload the file to some server out there and then provide a link, but then it is not possible to guarantee the survival of the file... so people reading this post in the future wouldn't be able to get the whole picture.

    What's your advice?

    Thanks!



  • How about calling update() of the affected QGraphicsItems?



  • @Asperamanca ,

    oh, God! It works!

    This is the code I added to the QGraphicsView:

    void
    GraphicsView::
    repaint_all_points
    (void)
    {
      {
        QList<QGraphicsItem*> all = scene()->items();
    
        for (int i = 0; i < all.size(); i++)
        {
          if (all.at(i)->type() == VEPointItemType)
          {
            VEPointItem *gi = static_cast<VEPointItem*>(all[i]);
            gi->update();
          }
        }
      }
    }
    

    Then I called the method above each time the user changed the color table.

    THANK YOU!!!!

    This post should be marked as solved, but... I don't know how to do it! Can anyone help me?

    Thanks again!



  • @bleriot13
    Do the GraphicItems hold the color as a property? If so, it would make sense to call update() there, whenever you change the property.
    If they get the color from a global table, the you have to do it like you wrote...



  • @Asperamanca ,

    thanks one more for your interest in this issue.

    The color is retrieved from a global color table; the problem I had is that when the user changed the color table (there are several of these), the "regular" update() methods didn't trigger a refresh of the scene. Your suggestion did the trick.

    Thanks once more.


  • Lifetime Qt Champion

    @bleriot13 said in Updating QGraphicsScene:

    This post should be marked as solved, but... I don't know how to do it! Can anyone help me?

    Either the "Topic Tools" button or the three doted menu beside the answer you deem correct.


Log in to reply