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. Graphics Scene Update

Graphics Scene Update

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 5 Posters 3.0k 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.
  • A Offline
    A Offline
    Anurag2904
    wrote on last edited by
    #1

    Hi,

    I have created a QGraphicsScene and added an ellipse to it and made it movable. Then I wrote a loop where I move the ellipse horizontally on the scene using moveby(x, y) method. But the scene does not change and I only see ellipse at initial location even though the loop completes (the print statement inside the loop executes correct number of time but the ellipse does not move).

    Please suggest how to make ellipse move on screen.

    raven-worxR 1 Reply Last reply
    0
    • A Anurag2904

      Hi,

      I have created a QGraphicsScene and added an ellipse to it and made it movable. Then I wrote a loop where I move the ellipse horizontally on the scene using moveby(x, y) method. But the scene does not change and I only see ellipse at initial location even though the loop completes (the print statement inside the loop executes correct number of time but the ellipse does not move).

      Please suggest how to make ellipse move on screen.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Anurag2904
      show some code to prevent guessing.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Anurag2904
        wrote on last edited by Anurag2904
        #3

        #include "secondpage.h"
        #include <iostream>
        #include <sstream>
        #include <stdio.h>
        #include <Windows.h>

        secondPage::secondPage(QWidget *parent)
        : QWidget(parent)
        {
        ui.setupUi(this);

        graphicsView = new QGraphicsView(this);
        scene = new QGraphicsScene(this);
        graphicsView->setMinimumSize(1600, 900);
        graphicsView->setSceneRect(0,0, 1600, 900);
        graphicsView->setScene(scene);
        
        //QGraphicsEllipseItem *ellipse; - done in .h file
        ellipse = scene->addEllipse(158,66,20,20, QPen(Qt::black), QBrush(Qt::black));
        ellipse->setFlag(QGraphicsItem::ItemIsMovable);
        
        
        graphicsView->show();
        //SIGNAL TO go back to stacked widget
        connect(this, SIGNAL(callParent(int)), this->parent(), SLOT(newChangePage(int)));
        

        }
        //newRoutine() SLOT is called from stacked widget in a different class
        //#define NUM_OF_WRITES 9U - done is .h file
        void secondPage::newRoutine()
        {

        int i = 0;
        QFile file(fname);
        
        for (; i < NUM_OF_WRITES; i++)
        {
        	int a = i;
        	Sleep(700);
        	if (file.open(QIODevice::Append | QIODevice::Text)) 
        	{
        		QTextStream stream(&file);
        		stream2 << 'Write # ' << i << endl;
        	}
        	else
        	{
        		OutputDebugStringW(L"FAILED TO WRITE");
        		OutputDebugStringW(L"\n");
        	}
        	if ((i == 3) || (i == 6))
        	{
        		ellipse->moveBy(-1280, 360);
        	}
        	else
        	{
        		ellipse->moveBy(640, 0);
        	}
        }
        file.close();
        
        //Go back to stacked widget from where this method was called (see connect above)
        emit callParent(2);
        

        }


        Above is my code. On main screen i have a stacked widget, from there i call newRoutine(). I want to show a animation where ellipse moves around on the graphics view and then trigger a signal - callParent() after which stacked widget will change index to next page.

        jsulmJ J.HilkJ 2 Replies Last reply
        0
        • A Anurag2904

          #include "secondpage.h"
          #include <iostream>
          #include <sstream>
          #include <stdio.h>
          #include <Windows.h>

          secondPage::secondPage(QWidget *parent)
          : QWidget(parent)
          {
          ui.setupUi(this);

          graphicsView = new QGraphicsView(this);
          scene = new QGraphicsScene(this);
          graphicsView->setMinimumSize(1600, 900);
          graphicsView->setSceneRect(0,0, 1600, 900);
          graphicsView->setScene(scene);
          
          //QGraphicsEllipseItem *ellipse; - done in .h file
          ellipse = scene->addEllipse(158,66,20,20, QPen(Qt::black), QBrush(Qt::black));
          ellipse->setFlag(QGraphicsItem::ItemIsMovable);
          
          
          graphicsView->show();
          //SIGNAL TO go back to stacked widget
          connect(this, SIGNAL(callParent(int)), this->parent(), SLOT(newChangePage(int)));
          

          }
          //newRoutine() SLOT is called from stacked widget in a different class
          //#define NUM_OF_WRITES 9U - done is .h file
          void secondPage::newRoutine()
          {

          int i = 0;
          QFile file(fname);
          
          for (; i < NUM_OF_WRITES; i++)
          {
          	int a = i;
          	Sleep(700);
          	if (file.open(QIODevice::Append | QIODevice::Text)) 
          	{
          		QTextStream stream(&file);
          		stream2 << 'Write # ' << i << endl;
          	}
          	else
          	{
          		OutputDebugStringW(L"FAILED TO WRITE");
          		OutputDebugStringW(L"\n");
          	}
          	if ((i == 3) || (i == 6))
          	{
          		ellipse->moveBy(-1280, 360);
          	}
          	else
          	{
          		ellipse->moveBy(640, 0);
          	}
          }
          file.close();
          
          //Go back to stacked widget from where this method was called (see connect above)
          emit callParent(2);
          

          }


          Above is my code. On main screen i have a stacked widget, from there i call newRoutine(). I want to show a animation where ellipse moves around on the graphics view and then trigger a signal - callParent() after which stacked widget will change index to next page.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #4

          @Anurag2904 Not an answer to your question.

          1. Why do you open the file inside the loop?!
          2. Sleep in a loop in an event driven app (like Qt apps) isn't a good idea - it makes your app unresponsive

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • A Anurag2904

            #include "secondpage.h"
            #include <iostream>
            #include <sstream>
            #include <stdio.h>
            #include <Windows.h>

            secondPage::secondPage(QWidget *parent)
            : QWidget(parent)
            {
            ui.setupUi(this);

            graphicsView = new QGraphicsView(this);
            scene = new QGraphicsScene(this);
            graphicsView->setMinimumSize(1600, 900);
            graphicsView->setSceneRect(0,0, 1600, 900);
            graphicsView->setScene(scene);
            
            //QGraphicsEllipseItem *ellipse; - done in .h file
            ellipse = scene->addEllipse(158,66,20,20, QPen(Qt::black), QBrush(Qt::black));
            ellipse->setFlag(QGraphicsItem::ItemIsMovable);
            
            
            graphicsView->show();
            //SIGNAL TO go back to stacked widget
            connect(this, SIGNAL(callParent(int)), this->parent(), SLOT(newChangePage(int)));
            

            }
            //newRoutine() SLOT is called from stacked widget in a different class
            //#define NUM_OF_WRITES 9U - done is .h file
            void secondPage::newRoutine()
            {

            int i = 0;
            QFile file(fname);
            
            for (; i < NUM_OF_WRITES; i++)
            {
            	int a = i;
            	Sleep(700);
            	if (file.open(QIODevice::Append | QIODevice::Text)) 
            	{
            		QTextStream stream(&file);
            		stream2 << 'Write # ' << i << endl;
            	}
            	else
            	{
            		OutputDebugStringW(L"FAILED TO WRITE");
            		OutputDebugStringW(L"\n");
            	}
            	if ((i == 3) || (i == 6))
            	{
            		ellipse->moveBy(-1280, 360);
            	}
            	else
            	{
            		ellipse->moveBy(640, 0);
            	}
            }
            file.close();
            
            //Go back to stacked widget from where this method was called (see connect above)
            emit callParent(2);
            

            }


            Above is my code. On main screen i have a stacked widget, from there i call newRoutine(). I want to show a animation where ellipse moves around on the graphics view and then trigger a signal - callParent() after which stacked widget will change index to next page.

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @Anurag2904 to expand a tiny bit on what @jsulm said,

            you don't give your UI- a chance to update.
            During a function call the UI is not updated, during sleep-calls the ui is not updated. During loops the ui is not updated.

            There are a couple of ways to fix that problem, from "hacky" but very simple over unorthodox but possible, to the correct way but complex for this usecase.

            Which path to you choose?


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            raven-worxR 1 Reply Last reply
            2
            • J.HilkJ J.Hilk

              @Anurag2904 to expand a tiny bit on what @jsulm said,

              you don't give your UI- a chance to update.
              During a function call the UI is not updated, during sleep-calls the ui is not updated. During loops the ui is not updated.

              There are a couple of ways to fix that problem, from "hacky" but very simple over unorthodox but possible, to the correct way but complex for this usecase.

              Which path to you choose?

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @J.Hilk said in Graphics Scene Update:

              Which path to you choose?

              always teach the clean way ;)

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              2
              • A Offline
                A Offline
                Asperamanca
                wrote on last edited by
                #7

                So one approach would be this:

                • Create a separate class derived from QObject
                • Create a slot which basically has the code inside your file writing loop
                • Create a QTimer with an interval of 700 ms, and connect it to that slot
                • Every time you enter the slot, you can increment a member variable (which replaces your loop counter)
                • Whenever you want the ellipse to move in a certain fashion, have your class emit a signal. There are different ways to design this, depending on what you want. Probably the closest to your current code would be a signal with a QPointF parameter, which contains the offset by which to move.
                • Connect that signal to a new slot within the class that has access to the ellipse, and have the ellipse move whenever the signal arrives
                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