Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Updating QGraphicsScene
Qt 6.12 LTS is out! See what's new in the release blog

Updating QGraphicsScene

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 3.8k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • bleriot13B Offline
    bleriot13B Offline
    bleriot13
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Asperamanca
      wrote on last edited by
      #4

      How about calling update() of the affected QGraphicsItems?

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        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 ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • bleriot13B Offline
          bleriot13B Offline
          bleriot13
          wrote on last edited by
          #3

          @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!

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Asperamanca
            wrote on last edited by
            #4

            How about calling update() of the affected QGraphicsItems?

            1 Reply Last reply
            0
            • bleriot13B Offline
              bleriot13B Offline
              bleriot13
              wrote on last edited by
              #5

              @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!

              A 1 Reply Last reply
              0
              • bleriot13B bleriot13

                @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!

                A Offline
                A Offline
                Asperamanca
                wrote on last edited by
                #6

                @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...

                1 Reply Last reply
                0
                • bleriot13B Offline
                  bleriot13B Offline
                  bleriot13
                  wrote on last edited by
                  #7

                  @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.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @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.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt
                    • Unsolved