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. Nested QVector problem

Nested QVector problem

Scheduled Pinned Locked Moved Solved General and Desktop
qvectornest
9 Posts 4 Posters 1.2k 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.
  • P Offline
    P Offline
    pauledd
    wrote on 15 Nov 2019, 14:38 last edited by pauledd
    #1

    Hi

    I currently struggle with a nested QVector.
    I need to have a QVector<QCheckbox*> that has 24 Checkboxes
    in it representing 24 columns. From this QVector<QCheckbox*> I want to have 5 in a parent Vector representing 5 rows so that I can access them like that

    outer.at(4).at(23).setChecked(1)
    

    to access the 24th checkbox in the 5th row. (Of course all this is going later inside a QGridLayout)
    Bildschirmfoto_2019-11-15_15-17-55.png
    But I simply dont get it how to create the two vectors.

    I would start by creating them:

    QVector<QCheckBox*> inner;
    QVector<QVector<QCheckBox*>> outer;
    

    Then I would propably fill the first one with the 24 checkboxes:

    for(int i=0;i<24;i++){
    		inner.append(new QCheckBox);
    	}
    

    But how do I create 5 of them inside the outer vector. I am a bit lost here.

    O B 2 Replies Last reply 15 Nov 2019, 14:44
    0
    • P pauledd
      15 Nov 2019, 14:38

      Hi

      I currently struggle with a nested QVector.
      I need to have a QVector<QCheckbox*> that has 24 Checkboxes
      in it representing 24 columns. From this QVector<QCheckbox*> I want to have 5 in a parent Vector representing 5 rows so that I can access them like that

      outer.at(4).at(23).setChecked(1)
      

      to access the 24th checkbox in the 5th row. (Of course all this is going later inside a QGridLayout)
      Bildschirmfoto_2019-11-15_15-17-55.png
      But I simply dont get it how to create the two vectors.

      I would start by creating them:

      QVector<QCheckBox*> inner;
      QVector<QVector<QCheckBox*>> outer;
      

      Then I would propably fill the first one with the 24 checkboxes:

      for(int i=0;i<24;i++){
      		inner.append(new QCheckBox);
      	}
      

      But how do I create 5 of them inside the outer vector. I am a bit lost here.

      O Offline
      O Offline
      ODБOï
      wrote on 15 Nov 2019, 14:44 last edited by
      #2

      @pauledd hi

          for(int i=0;i<5;i++){
                  outer.append(inner);
              }
      
      1 Reply Last reply
      0
      • P Offline
        P Offline
        pauledd
        wrote on 15 Nov 2019, 14:55 last edited by pauledd
        #3

        @LeLev thank you. But if I now try to fill the grid layout with that vector
        like that:

                layGrid = new QGridLayout;
        	for(int i=0;i<24;i++){
        		inner.append(new QCheckBox);
        
        	}
        
        	for(int i=0;i<5;i++){
        		outer.append(inner);
        	}
        
        	for(int i=0;i<outer.size();i++){
        		for(int j=0;j<inner.size();j++){
        			layGrid->addWidget(outer.at(i).at(j),i,j);
        		}
        	}
        

        I get only the first row with checkboxes...
        Bildschirmfoto_2019-11-15_15-54-49.png

        1 Reply Last reply
        0
        • P pauledd
          15 Nov 2019, 14:38

          Hi

          I currently struggle with a nested QVector.
          I need to have a QVector<QCheckbox*> that has 24 Checkboxes
          in it representing 24 columns. From this QVector<QCheckbox*> I want to have 5 in a parent Vector representing 5 rows so that I can access them like that

          outer.at(4).at(23).setChecked(1)
          

          to access the 24th checkbox in the 5th row. (Of course all this is going later inside a QGridLayout)
          Bildschirmfoto_2019-11-15_15-17-55.png
          But I simply dont get it how to create the two vectors.

          I would start by creating them:

          QVector<QCheckBox*> inner;
          QVector<QVector<QCheckBox*>> outer;
          

          Then I would propably fill the first one with the 24 checkboxes:

          for(int i=0;i<24;i++){
          		inner.append(new QCheckBox);
          	}
          

          But how do I create 5 of them inside the outer vector. I am a bit lost here.

          B Offline
          B Offline
          beecksche
          wrote on 15 Nov 2019, 15:23 last edited by
          #4

          @pauledd

          Why don't you use a QGenericMatrix ?

          P 1 Reply Last reply 15 Nov 2019, 15:43
          0
          • B beecksche
            15 Nov 2019, 15:23

            @pauledd

            Why don't you use a QGenericMatrix ?

            P Offline
            P Offline
            pauledd
            wrote on 15 Nov 2019, 15:43 last edited by
            #5

            @beecksche Because the title says "QVector problem" ;) and I really dont understand https://doc.qt.io/qt-5/qgenericmatrix.html and there are nearly zero examples on the internet

            M 1 Reply Last reply 15 Nov 2019, 15:52
            0
            • P pauledd
              15 Nov 2019, 15:43

              @beecksche Because the title says "QVector problem" ;) and I really dont understand https://doc.qt.io/qt-5/qgenericmatrix.html and there are nearly zero examples on the internet

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 15 Nov 2019, 15:52 last edited by mrjj
              #6

              @pauledd
              Hi
              Its a logical bug :)
              I had to play with the code to realize.

              // create one row of checkboxes
              auto layGrid = new QGridLayout;
              for (int i = 0; i < 24; i++) {
              inner.append(new QCheckBox);
              }

              // copy same row of checkbox to outer 5 times
              // BUT since there really is only one row of widgets, we will only see
              // 1 row of checkboxes in the layout.
              // since we just copy the pointers and the other rows
              // shares the listboxes!

              for (int i = 0; i < 5; i++) {
                  outer.append(inner);
              }
              

              so you have to do

               for (int i = 0; i < 5; i++) {
                      inner.clear();
                      for (int i = 0; i < 24; i++) {
                          inner.append(new QCheckBox);
                      }
                      outer.append(inner); // now we add a new inner and not reuse the same
                  }
              

              and then it works
              alt text
              Cheers

              P 1 Reply Last reply 15 Nov 2019, 16:06
              6
              • M mrjj
                15 Nov 2019, 15:52

                @pauledd
                Hi
                Its a logical bug :)
                I had to play with the code to realize.

                // create one row of checkboxes
                auto layGrid = new QGridLayout;
                for (int i = 0; i < 24; i++) {
                inner.append(new QCheckBox);
                }

                // copy same row of checkbox to outer 5 times
                // BUT since there really is only one row of widgets, we will only see
                // 1 row of checkboxes in the layout.
                // since we just copy the pointers and the other rows
                // shares the listboxes!

                for (int i = 0; i < 5; i++) {
                    outer.append(inner);
                }
                

                so you have to do

                 for (int i = 0; i < 5; i++) {
                        inner.clear();
                        for (int i = 0; i < 24; i++) {
                            inner.append(new QCheckBox);
                        }
                        outer.append(inner); // now we add a new inner and not reuse the same
                    }
                

                and then it works
                alt text
                Cheers

                P Offline
                P Offline
                pauledd
                wrote on 15 Nov 2019, 16:06 last edited by
                #7

                @mrjj Man that drove me crazy, thanks a lot :). I somehow knew there was something wrong adding it to outer because it reserved somehow space for the non-existing rows.

                M 1 Reply Last reply 15 Nov 2019, 16:10
                0
                • P pauledd
                  15 Nov 2019, 16:06

                  @mrjj Man that drove me crazy, thanks a lot :). I somehow knew there was something wrong adding it to outer because it reserved somehow space for the non-existing rows.

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 15 Nov 2019, 16:10 last edited by
                  #8

                  @pauledd
                  Yeah, it also got me for a moment.
                  Same for me as the checkbox row came in the bottom of the layout and then it hit me hard :)

                  O 1 Reply Last reply 15 Nov 2019, 20:35
                  0
                  • M mrjj
                    15 Nov 2019, 16:10

                    @pauledd
                    Yeah, it also got me for a moment.
                    Same for me as the checkbox row came in the bottom of the layout and then it hit me hard :)

                    O Offline
                    O Offline
                    ODБOï
                    wrote on 15 Nov 2019, 20:35 last edited by ODБOï
                    #9

                    @mrjj good catch ^^

                    1 Reply Last reply
                    1

                    2/9

                    15 Nov 2019, 14:44

                    topic:navigator.unread, 7
                    • Login

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