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. [SOLVED] How do I simply update scene after adding a new element?

[SOLVED] How do I simply update scene after adding a new element?

Scheduled Pinned Locked Moved General and Desktop
qgraphicsviewscenegraphupdate
4 Posts 3 Posters 5.8k Views
  • 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.
  • A Offline
    A Offline
    alogim
    wrote on 11 Sept 2015, 15:55 last edited by alogim 9 Nov 2015, 22:11
    #1

    I'm writing a simple desktop application to simulate the RRT algorithm.
    So, I set up a simple window with a graphicsView from the ui editor. In the window constructor, I added a scene, like this:
    [code]
    ui->setupUi(this);
    scene = new QGraphicsScene(this);
    scene->setSceneRect(0, 0, vWidth, vHeight);
    [/code]
    where vWidth and vHeight have been previously set. I then start adding graph's points and edges. I have a "START" button that starts adding points and edges, but the scene updates only when the whole graph has been computed (this is done in a while loop). To avoid these, since I'd like to see the graph construction "step-by-step", I added
    [code]
    ui->graphicsView->viewport()->repaint();
    [/code]
    after every point/edge insertion on the scene, done via
    [code]
    scene->addEllipse(...)
    scene->addLine(...)
    [/code]
    This, on the other hand, freezes the ui until the whole graph has been drawn, so I can't stop or pause the process, or even simply close the window.

    So, how could I do this? Shall I use threads? Any example? Thanks.

    A 1 Reply Last reply 11 Sept 2015, 21:25
    0
    • A alogim
      11 Sept 2015, 15:55

      I'm writing a simple desktop application to simulate the RRT algorithm.
      So, I set up a simple window with a graphicsView from the ui editor. In the window constructor, I added a scene, like this:
      [code]
      ui->setupUi(this);
      scene = new QGraphicsScene(this);
      scene->setSceneRect(0, 0, vWidth, vHeight);
      [/code]
      where vWidth and vHeight have been previously set. I then start adding graph's points and edges. I have a "START" button that starts adding points and edges, but the scene updates only when the whole graph has been computed (this is done in a while loop). To avoid these, since I'd like to see the graph construction "step-by-step", I added
      [code]
      ui->graphicsView->viewport()->repaint();
      [/code]
      after every point/edge insertion on the scene, done via
      [code]
      scene->addEllipse(...)
      scene->addLine(...)
      [/code]
      This, on the other hand, freezes the ui until the whole graph has been drawn, so I can't stop or pause the process, or even simply close the window.

      So, how could I do this? Shall I use threads? Any example? Thanks.

      A Offline
      A Offline
      alex_malyu
      wrote on 11 Sept 2015, 21:25 last edited by
      #2

      @alogim

      Stopping is not difficult.
      For example:

      If you call QCoreApplication::processEvents ()

      after each line you want to be able to stop, for example:
      scene->addEllipse(...)

      user will be able to interact with gui.
      This should also let the view update. I do not think you need ui->graphicsView->viewport()->repaint();

      As soon you are able to interact with GUI, clicking on your stop button should set the flag to stop.

      For example:

      • for simplicity assume you need to call only one function scene->addEllipse(...) in a loop, until stop button is not pressed or you are done;
      • bool toStop is a flag to stop processing.
      • **addItemsTo **scene is function which adds all items to the scene:
      • onStopClicked() is a slot which is called on stop button click
      • myWindow is the widget owner of ui.

      void myWindow::onStopClicked()
      {
      toStop = true;
      }

      void myWindow::addItemsTo()
      {
      const int itemsToAdd = 10;
      toStop = false;
      for ( int i=0; i< itemsToAdd ; i++ )
      {
      QCoreApplication::processEvents () ;
      if( toStop ) // user requested to stop
      break;

          scene->addEllipse(...)
      

      }

      }

      You might also want to disable all the controls you do not want user be able to interact with at the beginning of the addItemsTo and restore their state at the end.

      Pausing would be more complicating since you need to know how to continue.

      A 1 Reply Last reply 11 Sept 2015, 22:09
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 11 Sept 2015, 21:46 last edited by
        #3

        Hi and welcome to devnet,

        You could also just use a QTimer that would call periodically as slot where you add your points/ellipses etc.
        The stop button would just have to cancel the QTimer.

        Hope it helps

        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
        • A alex_malyu
          11 Sept 2015, 21:25

          @alogim

          Stopping is not difficult.
          For example:

          If you call QCoreApplication::processEvents ()

          after each line you want to be able to stop, for example:
          scene->addEllipse(...)

          user will be able to interact with gui.
          This should also let the view update. I do not think you need ui->graphicsView->viewport()->repaint();

          As soon you are able to interact with GUI, clicking on your stop button should set the flag to stop.

          For example:

          • for simplicity assume you need to call only one function scene->addEllipse(...) in a loop, until stop button is not pressed or you are done;
          • bool toStop is a flag to stop processing.
          • **addItemsTo **scene is function which adds all items to the scene:
          • onStopClicked() is a slot which is called on stop button click
          • myWindow is the widget owner of ui.

          void myWindow::onStopClicked()
          {
          toStop = true;
          }

          void myWindow::addItemsTo()
          {
          const int itemsToAdd = 10;
          toStop = false;
          for ( int i=0; i< itemsToAdd ; i++ )
          {
          QCoreApplication::processEvents () ;
          if( toStop ) // user requested to stop
          break;

              scene->addEllipse(...)
          

          }

          }

          You might also want to disable all the controls you do not want user be able to interact with at the beginning of the addItemsTo and restore their state at the end.

          Pausing would be more complicating since you need to know how to continue.

          A Offline
          A Offline
          alogim
          wrote on 11 Sept 2015, 22:09 last edited by
          #4

          @alex_malyu
          Thank you very much alex. I just tried what you suggested and it works perfectly.

          @SGaist
          I thought about slots and timer too, as suggested in the examples on qt's site, but it requires subclassing a QGraphicsItem for each item in the QGraphicsView. The problem is, I don't know the number of items that will be generated until runtime, and apart from this, creating 10.000 classes (one for each item) would be very resource intensive, I think.
          Anyway, the method above, the one with
          QCoreApplication::processEvents () ;
          works perfectly for my purpose.
          So thanks both for your politeness: it is very nice to see such a great community :)

          1 Reply Last reply
          0

          1/4

          11 Sept 2015, 15:55

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved