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 at(0)
Forum Updated to NodeBB v4.3 + New Features

Update at(0)

Scheduled Pinned Locked Moved Unsolved General and Desktop
stlupdate
17 Posts 4 Posters 5.2k 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.
  • M Offline
    M Offline
    mandruk1331
    wrote on 28 Jan 2016, 13:31 last edited by
    #1

    How i can update data at certain position
    rectan.at(0).setX(rectan.at(1).x());
    rectan.at(0).setY(rectan.at(1).y());
    rectan.at(0).setHeight(rectan.at(1).height());
    rectan.at(0).setWidth(rectan.at(1).width());
    cuz this does not really work

    Mandruk1331

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 28 Jan 2016, 13:40 last edited by mrjj
      #2

      Well (if) you are just messing around with a list of QRectF.
      So nothing will change on screen.
      If you kept a list of the QGraphicsRectItem you new, then
      setRect() would work for those.

      M 1 Reply Last reply 28 Jan 2016, 14:13
      1
      • J Offline
        J Offline
        JohanSolo
        wrote on 28 Jan 2016, 13:42 last edited by JohanSolo
        #3

        Well, it does not work at all because at() returns const T&. You need a non const accessor, which is most probably (you never specified the container you're using) operator[]:

        rectan[ 0 ].setX( rectan.at( 1 ).x() ) ;
        rectan[ 0 ].setY( rectan.at( 1 ).y() ) ;
        

        and so on...

        `They did not know it was impossible, so they did it.'
        -- Mark Twain

        1 Reply Last reply
        2
        • M mrjj
          28 Jan 2016, 13:40

          Well (if) you are just messing around with a list of QRectF.
          So nothing will change on screen.
          If you kept a list of the QGraphicsRectItem you new, then
          setRect() would work for those.

          M Offline
          M Offline
          mandruk1331
          wrote on 28 Jan 2016, 14:13 last edited by mandruk1331
          #4

          @mrjj and can you tell me what's wrong with this code, I was trying to swap rectangles places it swaps only at the first time and then it goes crazy it swaps the way it wants

          void MainWindow::Shot(int j){
          QBrush greenBrush(Qt::green);
          QPointF pos1_point;
          QPointF pos2_point;
          QRectF temp;
          temp.setX(rectan.at(j).x());
          temp.setY(rectan.at(j).y());
          temp.setHeight(rectan.at(j).height());
          temp.setWidth(rectan.at(j).width());

          pos1_point.setX(rectan.at(j).x());
          pos2_point.setX(rectan.at(j+1).x());
          draw_rectan.at(j+1)->setBrush(greenBrush);
          draw_rectan.at(j)->setBrush(greenBrush);
          draw_rectan.at(j)->setPos(-(pos2_point.x()-pos1_point.x()),-draw_rectan.at(j)->y());
          draw_rectan.at(j+1)->setPos( pos2_point.x()-pos1_point.x(),-draw_rectan.at(j+1)->y());
          rectan[j].setX(rectan.at(j+1).x());
          rectan[j].setY(rectan.at(j+1).y());
          rectan[j].setHeight(rectan.at(j+1).height());
          rectan[j].setWidth(rectan.at(j+1).width());

          rectan[j+1].setX(temp.x());
          rectan[j+1].setY(temp.y());
          rectan[j+1].setHeight(temp.height());
          rectan[j+1].setWidth(temp.width());

          }

          Mandruk1331

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

            "swaps the way it wants"
            which is ?
            Also is rectan of GraphicsItems ?

            M 1 Reply Last reply 28 Jan 2016, 14:21
            0
            • M mrjj
              28 Jan 2016, 14:18

              "swaps the way it wants"
              which is ?
              Also is rectan of GraphicsItems ?

              M Offline
              M Offline
              mandruk1331
              wrote on 28 Jan 2016, 14:21 last edited by mandruk1331
              #6

              @mrjj randomly. Rectan is QRectF and draw_rectan is QGraphicsRectItem.
              Maybe the rectan update is wrong:

              rectan[j].setX(rectan.at(j+1).x());
              rectan[j].setY(rectan.at(j+1).y());
              rectan[j].setHeight(rectan.at(j+1).height());
              rectan[j].setWidth(rectan.at(j+1).width());

              rectan[j+1].setX(temp.x());
              rectan[j+1].setY(temp.y());
              rectan[j+1].setHeight(temp.height());
              rectan[j+1].setWidth(temp.width());

              I sort them together with the draw_rectan items

              Mandruk1331

              M 1 Reply Last reply 28 Jan 2016, 14:29
              0
              • M mandruk1331
                28 Jan 2016, 14:21

                @mrjj randomly. Rectan is QRectF and draw_rectan is QGraphicsRectItem.
                Maybe the rectan update is wrong:

                rectan[j].setX(rectan.at(j+1).x());
                rectan[j].setY(rectan.at(j+1).y());
                rectan[j].setHeight(rectan.at(j+1).height());
                rectan[j].setWidth(rectan.at(j+1).width());

                rectan[j+1].setX(temp.x());
                rectan[j+1].setY(temp.y());
                rectan[j+1].setHeight(temp.height());
                rectan[j+1].setWidth(temp.width());

                I sort them together with the draw_rectan items

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 28 Jan 2016, 14:29 last edited by
                #7

                It does seems ok.
                You should put the debugger on it and
                check the rectan array after the swap.

                M 1 Reply Last reply 28 Jan 2016, 14:30
                0
                • M mrjj
                  28 Jan 2016, 14:29

                  It does seems ok.
                  You should put the debugger on it and
                  check the rectan array after the swap.

                  M Offline
                  M Offline
                  mandruk1331
                  wrote on 28 Jan 2016, 14:30 last edited by
                  #8

                  @mrjj Ok. I'll try it, thx. Everything works great but the swap process... random(

                  Mandruk1331

                  1 Reply Last reply
                  0
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on 28 Jan 2016, 15:06 last edited by
                    #9

                    Hi!
                    Some general help:

                    1. Create a list of pointers to QGraphicsRectItems: QList<QGraphicsRectItem*> list;

                    2. Create a new QGraphicsRectItem: QGraphicsRectItem *tmp = new QGraphicsRectItem(0, 0, 100, 50);

                    3. Add that to your list: list.append(tmp);

                    4. Repeat steps 2 and 3 for all the rectangles you want to have.

                    5. Add all the rectangles to your scene:

                    for (int i=0; i<list.size(); i++) {
                        QGraphicsRectItem *temp = list.at(i);
                        scene->addItem(temp);
                    }
                    
                    1. If you want to get the position and size of item number 0:
                    QRectF rect = list.at(0)->rect();
                    qreal x = rect.x();
                    qreal y = rect.y();
                    qreal width = rect.width();
                    qreal height = rect.height();
                    
                    1. If you want to set the position and size of item number 0:
                    QRectF rect(23,42,5,666);
                    list.at(0)->setRect(rect);
                    
                    M 1 Reply Last reply 28 Jan 2016, 15:19
                    2
                    • ? A Former User
                      28 Jan 2016, 15:06

                      Hi!
                      Some general help:

                      1. Create a list of pointers to QGraphicsRectItems: QList<QGraphicsRectItem*> list;

                      2. Create a new QGraphicsRectItem: QGraphicsRectItem *tmp = new QGraphicsRectItem(0, 0, 100, 50);

                      3. Add that to your list: list.append(tmp);

                      4. Repeat steps 2 and 3 for all the rectangles you want to have.

                      5. Add all the rectangles to your scene:

                      for (int i=0; i<list.size(); i++) {
                          QGraphicsRectItem *temp = list.at(i);
                          scene->addItem(temp);
                      }
                      
                      1. If you want to get the position and size of item number 0:
                      QRectF rect = list.at(0)->rect();
                      qreal x = rect.x();
                      qreal y = rect.y();
                      qreal width = rect.width();
                      qreal height = rect.height();
                      
                      1. If you want to set the position and size of item number 0:
                      QRectF rect(23,42,5,666);
                      list.at(0)->setRect(rect);
                      
                      M Offline
                      M Offline
                      mandruk1331
                      wrote on 28 Jan 2016, 15:19 last edited by mandruk1331
                      #10

                      @Wieland for example I have a List of QGraphicsRectItem and when I use the setPos
                      draw_rectan.at(j+1)->setPos( pos2_point.x()-pos1_point.x(),-draw_rectan.at(j+1)->y());
                      It will change the position of the rectangle, but will it change the data in the QList? or I have to swap it manually, I mean will the data be updated after I used the setPos func or not?

                      Mandruk1331

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on 28 Jan 2016, 16:19 last edited by A Former User
                        #11

                        The list, QList<QGraphicsRectItem*>, does not contain the QGraphicsRectItems themselves but pointers to these items. So, when you access an object that's in the list, say you do this: list.at(0), then you'll get a pointer. This is why you use the operator -> when you call setRect: list.at(0)->setRect(...). When you do that you don't change the actual content of the list because the actual content is just a handful of pointers. You don't change the pointers. You use the pointers to change the QGraphicsRectItems that these pointers point to. Once the actual objects change they will notify the scene they were added to.

                        tl;dr : Yes, the scene gets updated.

                        M 1 Reply Last reply 28 Jan 2016, 16:24
                        0
                        • ? A Former User
                          28 Jan 2016, 16:19

                          The list, QList<QGraphicsRectItem*>, does not contain the QGraphicsRectItems themselves but pointers to these items. So, when you access an object that's in the list, say you do this: list.at(0), then you'll get a pointer. This is why you use the operator -> when you call setRect: list.at(0)->setRect(...). When you do that you don't change the actual content of the list because the actual content is just a handful of pointers. You don't change the pointers. You use the pointers to change the QGraphicsRectItems that these pointers point to. Once the actual objects change they will notify the scene they were added to.

                          tl;dr : Yes, the scene gets updated.

                          M Offline
                          M Offline
                          mandruk1331
                          wrote on 28 Jan 2016, 16:24 last edited by mandruk1331
                          #12

                          @Wieland Ok, so I tried a lot of stuff and here is what I got, the rectangles are updating on the scene, but It jumps over a rectangle for instance: I'm implementing a bubble sort. It start with first and second elements, after it swapped this elements it swaps the other two elements whics can be swapped ignoring that swap whic was made before.
                          3 2 1 0
                          2 3 1 0
                          and then it does like this 2 3 0 1 but it has to be 2 1 3 0
                          What can cause this problem?

                          int count=0;
                          for( int i=0;i<random_numbers_.size()-1;i++){
                          for(int j=0;j<random_numbers_.size()-i-1;j++){

                                 if(random_numbers_[j]>random_numbers_[j+1]){
                                     count++;
                                  QTimer::singleShot(3000*count, [=]{ MainWindow::Shot(j);});
                                
                                  }
                              }
                          }
                          

                          void MainWindow::Shot(int j){
                          QBrush greenBrush(Qt::green);
                          QPointF pos1_point;
                          QPointF pos2_point;
                          QRectF temp1;
                          QRectF temp2;

                          temp1.setX(rectan.at(j).x());
                          temp1.setY(rectan.at(j).y());
                          temp1.setHeight(rectan.at(j).height());
                          temp1.setWidth(rectan.at(j).width());

                          temp2.setX(rectan.at(j+1).x());
                          temp2.setY(rectan.at(j+1).y());
                          temp2.setHeight(rectan.at(j+1).height());
                          temp2.setWidth(rectan.at(j+1).width());

                          pos1_point.setX(rectan.at(j).x());
                          pos2_point.setX(rectan.at(j+1).x());

                          draw_rectan.at(j+1)->setBrush(greenBrush);
                          draw_rectan.at(j)->setBrush(greenBrush);

                          int shift_R = pos2_point.x()-pos1_point.x();
                          int shift_L = -shift_R;
                          draw_rectan.at(j)->setPos(shift_R,-draw_rectan.at(j)->y());
                          draw_rectan.at(j+1)->setPos(shift_L ,-draw_rectan.at(j+1)->y());

                          draw_rectan[j]->update(temp2.x(),temp2.y(),temp2.width(),temp2.height());
                          draw_rectan[j+1]->update(temp1.x(),temp1.y(),temp1.width(),temp1.height());

                          rectan[j].setX(temp2.x());
                          rectan[j].setY(temp2.y());
                          rectan[j].setHeight(temp2.height());
                          rectan[j].setWidth(temp2.width());

                          rectan[j+1].setX(temp1.x());
                          rectan[j+1].setY(temp1.y());
                          rectan[j+1].setHeight(temp1.height());
                          rectan[j+1].setWidth(temp1.width());

                          }

                          Mandruk1331

                          1 Reply Last reply
                          0
                          • ? Offline
                            ? Offline
                            A Former User
                            wrote on 28 Jan 2016, 16:50 last edited by
                            #13

                            Let's put the visualization aspect aside for a minute. Does your sorting implementation work? I mean, if you have an array of numbers then does your program sort it correctly?

                            M 1 Reply Last reply 28 Jan 2016, 16:52
                            0
                            • ? A Former User
                              28 Jan 2016, 16:50

                              Let's put the visualization aspect aside for a minute. Does your sorting implementation work? I mean, if you have an array of numbers then does your program sort it correctly?

                              M Offline
                              M Offline
                              mandruk1331
                              wrote on 28 Jan 2016, 16:52 last edited by
                              #14

                              @Wieland The bubble algorithm is working I tested it. Now I'm checking is everything swaps correctly, cuz I think maybe I swap the numbers incorrectly and so it uses the old version of the list

                              Mandruk1331

                              1 Reply Last reply
                              0
                              • ? Offline
                                ? Offline
                                A Former User
                                wrote on 28 Jan 2016, 16:56 last edited by
                                #15

                                Just print your whole array of numbers after each iteration and see if it does what it's supposed to do. You can also check wikipedia; it has pseudo code for bubble sort algorithms with different optimizations.

                                M 1 Reply Last reply 28 Jan 2016, 17:05
                                0
                                • ? A Former User
                                  28 Jan 2016, 16:56

                                  Just print your whole array of numbers after each iteration and see if it does what it's supposed to do. You can also check wikipedia; it has pseudo code for bubble sort algorithms with different optimizations.

                                  M Offline
                                  M Offline
                                  mandruk1331
                                  wrote on 28 Jan 2016, 17:05 last edited by mandruk1331
                                  #16

                                  @Wieland how I can update the data in the QGraphicsRectItem list? Got it

                                  Mandruk1331

                                  ? 1 Reply Last reply 28 Jan 2016, 17:14
                                  0
                                  • M mandruk1331
                                    28 Jan 2016, 17:05

                                    @Wieland how I can update the data in the QGraphicsRectItem list? Got it

                                    ? Offline
                                    ? Offline
                                    A Former User
                                    wrote on 28 Jan 2016, 17:14 last edited by
                                    #17

                                    I don't understand what you mean. What data exactly?

                                    1 Reply Last reply
                                    0

                                    1/17

                                    28 Jan 2016, 13:31

                                    • Login

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