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. Update scene
Forum Update on Monday, May 27th 2025

Update scene

Scheduled Pinned Locked Moved Solved General and Desktop
updatesceneqgraphicsview
34 Posts 3 Posters 12.1k 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.
  • M Offline
    M Offline
    mandruk1331
    wrote on 24 Jan 2016, 16:21 last edited by
    #1

    Hello. How I can make an Update of the scene? What i mean is... for example I have a scene with drawn rectangles(all red color), and I want them to change color during the execution and compare them if one rectangle(it height number) is lower than the other one I want to swap places. How I can do that?

    Mandruk1331

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 24 Jan 2016, 16:36 last edited by
      #2

      Hi
      The QGraphicsItems in the scene has a setPos function
      you can use to move them around.

      M 1 Reply Last reply 24 Jan 2016, 16:42
      0
      • M mrjj
        24 Jan 2016, 16:36

        Hi
        The QGraphicsItems in the scene has a setPos function
        you can use to move them around.

        M Offline
        M Offline
        mandruk1331
        wrote on 24 Jan 2016, 16:42 last edited by
        #3

        @mrjj and can I change the color of a rectangle during runtime? for example, I have a rectangle which is red and after 5 second it dynamically becomes blue

        Mandruk1331

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 24 Jan 2016, 16:44 last edited by mrjj
          #4

          Yes, it should be possible.
          How do you make it red in first place?

          Maybe via
          item->setBrush(QBrush(Qt::green));

          M 1 Reply Last reply 24 Jan 2016, 18:14
          0
          • M mrjj
            24 Jan 2016, 16:44

            Yes, it should be possible.
            How do you make it red in first place?

            Maybe via
            item->setBrush(QBrush(Qt::green));

            M Offline
            M Offline
            mandruk1331
            wrote on 24 Jan 2016, 18:14 last edited by
            #5

            @mrjj I'm trying to make an animation of the bubble sort algorithm, it starts that the first two rectangles are set as green(start point), and I was wondering how I can change the color of the third rectangle to animate the movement?

            #include <QThread>
            #define RANDOM_MAX 100
            #define QUANTITY_RANDOM_NUM 10
            MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
            {
            ui->setupUi(this);
            error = new QErrorMessage(this);
            random_numbers_.clear();
            user_input_numbers_.clear();

            scene = new QGraphicsScene(this);
            ui->graphicsView_animation->setScene(scene);
            for(int i=0;i<QUANTITY_RANDOM_NUM;i++){
            random_numbers_.push_back(rand()%RANDOM_MAX);
            }
            ui->graphicsView_animation->setAlignment(Qt::AlignBottom| Qt::AlignRight);
            QBrush darkMagentaBrush(Qt::darkMagenta);
            QBrush greenBrush(Qt::green);
            QPen Pen(Qt::black);
            Pen.setWidth(2);
            // painter.drawText(temp);
            for(int i=0;i<random_numbers_.size();i++){
            QGraphicsRectItem temp = new QGraphicsRectItem(20+(i30),0-random_numbers_[i],28,random_numbers_[i]);
            temp->setPen(Pen);
            temp->setBrush(darkMagentaBrush);
            rectan.append(temp);
            }
            rectan[0]->setBrush(greenBrush);
            rectan[1]->setBrush(greenBrush);
            for(int i=0;i<random_numbers_.size();i++){
            scene->addItem(rectan.at(i));
            }
            //Bubble implement

            //for(int i=0;i<random_numbers_.size();i++){
            // scene->addRect(20+(i*30),0-random_numbers_[i],28,random_numbers_[i],Pen,darkMagentaBrush);
            //}

            }

            Mandruk1331

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 24 Jan 2016, 18:49 last edited by
              #6

              Hi
              You must keep a list or use the build in list to get the items.
              http://doc.qt.io/qt-5.5/qgraphicsscene.html#items
              Then you can change color or move them around.

              U seem to have list already ? rectan

              M 1 Reply Last reply 24 Jan 2016, 19:11
              0
              • M mrjj
                24 Jan 2016, 18:49

                Hi
                You must keep a list or use the build in list to get the items.
                http://doc.qt.io/qt-5.5/qgraphicsscene.html#items
                Then you can change color or move them around.

                U seem to have list already ? rectan

                M Offline
                M Offline
                mandruk1331
                wrote on 24 Jan 2016, 19:11 last edited by
                #7

                @mrjj and how I can swap places the rectangle? I found smth like an Update function but I can't implement it

                Mandruk1331

                M 1 Reply Last reply 24 Jan 2016, 19:13
                0
                • M mandruk1331
                  24 Jan 2016, 19:11

                  @mrjj and how I can swap places the rectangle? I found smth like an Update function but I can't implement it

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 24 Jan 2016, 19:13 last edited by mrjj
                  #8

                  @mandruk1331
                  But what u want to swap them?
                  Is it not enough to just swap the colors?

                  QPointF Pos1;
                  Pos1=rectan[0]->pos();
                  QPointF Pos2;
                  Pos2=rectan[1]->pos();
                  rectan[0]->setPos(pos2);
                  rectan[1]->setPos(pos1);

                  M 1 Reply Last reply 25 Jan 2016, 11:48
                  0
                  • M mrjj
                    24 Jan 2016, 19:13

                    @mandruk1331
                    But what u want to swap them?
                    Is it not enough to just swap the colors?

                    QPointF Pos1;
                    Pos1=rectan[0]->pos();
                    QPointF Pos2;
                    Pos2=rectan[1]->pos();
                    rectan[0]->setPos(pos2);
                    rectan[1]->setPos(pos1);

                    M Offline
                    M Offline
                    mandruk1331
                    wrote on 25 Jan 2016, 11:48 last edited by
                    #9

                    @mrjj No luck, it does not change the position

                    Mandruk1331

                    M 1 Reply Last reply 25 Jan 2016, 11:51
                    0
                    • M mandruk1331
                      25 Jan 2016, 11:48

                      @mrjj No luck, it does not change the position

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 25 Jan 2016, 11:51 last edited by
                      #10

                      @mandruk1331

                      Ok.
                      So what happened?
                      if you do
                      rectan[0]->setPos(0,0);

                      Does it move then?

                      M 1 Reply Last reply 25 Jan 2016, 12:04
                      0
                      • M mrjj
                        25 Jan 2016, 11:51

                        @mandruk1331

                        Ok.
                        So what happened?
                        if you do
                        rectan[0]->setPos(0,0);

                        Does it move then?

                        M Offline
                        M Offline
                        mandruk1331
                        wrote on 25 Jan 2016, 12:04 last edited by
                        #11

                        @mrjj No, at the moment I'm trying to make smth like an Update func

                        void MainWindow::Shot(){

                        QGraphicsRectItem *Pos1;
                        

                        Pos1=rectan[0];
                        QGraphicsRectItem *Pos2;
                        Pos2=rectan[1];
                        scene->removeItem(rectan.at(0));
                        scene->removeItem(rectan.at(1));

                        rectan.at(0) = Pos2;
                        rectan.at(1) = Pos1;
                        scene->addItem(rectan.at(0));
                        scene->addItem(rectan.at(1));
                        }

                        Mandruk1331

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 25 Jan 2016, 12:09 last edited by
                          #12

                          Hi
                          at returns const.
                          This looks strange to me
                          rectan.at(0) = Pos2;
                          maybe
                          rectan[0] = Pos2

                          Also this will just swap the position in your list.
                          It will NOT change on screen.

                          You need to use pos() and SetPos
                          Not just swap in you array. You swap pointers.
                          You should swap QPointF using pos and setPos;

                          M 1 Reply Last reply 25 Jan 2016, 12:17
                          0
                          • M mrjj
                            25 Jan 2016, 12:09

                            Hi
                            at returns const.
                            This looks strange to me
                            rectan.at(0) = Pos2;
                            maybe
                            rectan[0] = Pos2

                            Also this will just swap the position in your list.
                            It will NOT change on screen.

                            You need to use pos() and SetPos
                            Not just swap in you array. You swap pointers.
                            You should swap QPointF using pos and setPos;

                            M Offline
                            M Offline
                            mandruk1331
                            wrote on 25 Jan 2016, 12:17 last edited by
                            #13

                            @mrjj I thought it will repaint the rects, because first I delete them and then I want to repaint them with new options

                            Mandruk1331

                            M 1 Reply Last reply 25 Jan 2016, 12:19
                            0
                            • M mandruk1331
                              25 Jan 2016, 12:17

                              @mrjj I thought it will repaint the rects, because first I delete them and then I want to repaint them with new options

                              M Offline
                              M Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 25 Jan 2016, 12:19 last edited by
                              #14

                              @mandruk1331

                              Yes I guess it will repaint them on scene.
                              But it matters not for scene if you swap your own array.
                              The items still have same location as before as you do not setPos
                              (from code shown)

                              M 1 Reply Last reply 25 Jan 2016, 12:25
                              0
                              • M mrjj
                                25 Jan 2016, 12:19

                                @mandruk1331

                                Yes I guess it will repaint them on scene.
                                But it matters not for scene if you swap your own array.
                                The items still have same location as before as you do not setPos
                                (from code shown)

                                M Offline
                                M Offline
                                mandruk1331
                                wrote on 25 Jan 2016, 12:25 last edited by
                                #15

                                @mrjj and another one, I have a loop inside of a loop and when the second one finishes its iteration it won't singleshot on another iteration of the first loop

                                for (int =0;i<10;i++){
                                //and when i is 1 the second loop won't singleshot again
                                for(int j=0;j<9;j++){
                                SingleShot
                                }
                                }

                                Mandruk1331

                                1 Reply Last reply
                                0
                                • M Offline
                                  M Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 25 Jan 2016, 12:28 last edited by
                                  #16

                                  well
                                  Normally it works as many times as u set it up but hard to guess at with
                                  only "SingleShot".

                                  M 1 Reply Last reply 25 Jan 2016, 12:50
                                  0
                                  • M mrjj
                                    25 Jan 2016, 12:28

                                    well
                                    Normally it works as many times as u set it up but hard to guess at with
                                    only "SingleShot".

                                    M Offline
                                    M Offline
                                    mandruk1331
                                    wrote on 25 Jan 2016, 12:50 last edited by
                                    #17

                                    @mrjj Why this code does not add the item, I don't understand, I remove it and then I want to re-add it with new options
                                    void MainWindow::Shot(){
                                    QGraphicsRectItem *t = new QGraphicsRectItem(rectan.at(0));

                                    scene->removeItem(rectan.at(1));

                                    rectan[1] = t;
                                    scene->addItem(rectan[1]);
                                    }

                                    Mandruk1331

                                    1 Reply Last reply
                                    0
                                    • M Offline
                                      M Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on 25 Jan 2016, 12:55 last edited by
                                      #18

                                      @mandruk1331 said:

                                      Why do yo u give rectan.at(0) to constructor ?
                                      new QGraphicsRectItem(rectan.at(0));

                                      Try
                                      scene->addItem( new QGraphicsRectItem );
                                      and tell if that not add a new item?

                                      M 1 Reply Last reply 25 Jan 2016, 17:51
                                      0
                                      • M mrjj
                                        25 Jan 2016, 12:55

                                        @mandruk1331 said:

                                        Why do yo u give rectan.at(0) to constructor ?
                                        new QGraphicsRectItem(rectan.at(0));

                                        Try
                                        scene->addItem( new QGraphicsRectItem );
                                        and tell if that not add a new item?

                                        M Offline
                                        M Offline
                                        mandruk1331
                                        wrote on 25 Jan 2016, 17:51 last edited by mandruk1331
                                        #19

                                        @mrjj Ok, I managed to swap positions of the rectangles, but they swap not always, at first execution they swap on the other they don't, what could be the problem??

                                        void MainWindow::Shot(){
                                        QPointF Pos1;
                                        QPointF Pos2;
                                        Pos1 = rectan.at(5)->pos();
                                        Pos2 = rectan.at(6)->pos();

                                        rectan.at(6)->setBrush(Qt::blue);
                                        rectan.at(5)->setBrush(Qt::green);
                                        // rectan.at(5)->setBrush(Qt::blue);
                                        rectan.at(6)->setPos(Pos1.rx()-30,Pos1.ry());
                                        rectan.at(5)->setPos(Pos2.rx()+30,Pos2.ry());

                                        }
                                        Solved it)

                                        Mandruk1331

                                        1 Reply Last reply
                                        0
                                        • M Offline
                                          M Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on 25 Jan 2016, 18:18 last edited by
                                          #20

                                          hi
                                          put qDebug() << "in shot:"
                                          in the
                                          MainWindow::Shot()
                                          to make sure its actually called mutiple times.

                                          M 1 Reply Last reply 25 Jan 2016, 19:19
                                          0

                                          5/34

                                          24 Jan 2016, 18:14

                                          29 unread
                                          • Login

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