Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem getting view to update when scene changed
Qt 6.11 is out! See what's new in the release blog

Problem getting view to update when scene changed

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 166 Views 2 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.
  • J Offline
    J Offline
    james b-s
    wrote last edited by james b-s
    #1

    I have a qGraphicsScene with a bunch of QGraphicsObjects on it. I also have a QGraphicsView. For the most part, everything works. The problem I am having is that when a user drags the QGraphicsObject off of the view, the view doesn't update and the scrollbars don't change. The item is there, but it isn't visible because the screen cannot be scrolled to the item.

    I have a method that is called when the item is moved. The method gets called as expected. The current code that I have is as follows:

      const QRectF boundingRect = myScene->itemsBoundingRect();
      myScene->setSceneRect(boundingRect);
      myScene->update(boundingRect);
      myView->viewport()->update();
      myView->setScene(myScene);
      myView->updateSceneRect(myScene->sceneRect());
    

    I have tried all sorts of combinations of the above, most of which I got from AI as nothing else seemed to work. None of this did any good.

    I have verified that the scene does indeed expand when I drag something off the left side of the view. The minimum value for the horizontal scroll bar is 0 before the above code executes, and is still 0 after the code executes. I believe that the scene isn't getting updated.

    I did try calling QApplication::processEvents() as suggested by AI, but that didn't help. Getting the graphicsView rectangle, mapping it to the screen, and checking if the QGraphicsObject's rectangle intersects the view rectangle returns true. Maybe the scene is updated, but the scrollbar doesn't change to reflect it.

    Anyone have any suggestions?
    Thanks

    more code of interest

      setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
      setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
      setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
      setAlignment(Qt::AlignLeft | Qt::AlignTop);
      setViewportUpdateMode(QGraphicsView::MinimalViewportUpdate);
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote last edited by
      #2

      Hi,

      Which version of Qt are you using ?
      On which OS ?
      Can you provide a minimal compilable example that shows this behaviour ?

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

      J 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Which version of Qt are you using ?
        On which OS ?
        Can you provide a minimal compilable example that shows this behaviour ?

        J Offline
        J Offline
        james b-s
        wrote last edited by
        #3

        @SGaist Qt 6 and windows 11

        It looks like what was missing was

        connect(scene(), &QGraphicsScene::sceneRectChanged, this, [this](QRectF rect) {this->setSceneRect(rect);});
        

        Seems to work better, although not perfect.

        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 Extensions
        • Unsolved