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
QtWS25 Last Chance

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.
  • pauleddP Offline
    pauleddP Offline
    pauledd
    wrote on 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.

    ODБOïO beeckscheB 2 Replies Last reply
    0
    • pauleddP pauledd

      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.

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #2

      @pauledd hi

          for(int i=0;i<5;i++){
                  outer.append(inner);
              }
      
      1 Reply Last reply
      0
      • pauleddP Offline
        pauleddP Offline
        pauledd
        wrote on 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
        • pauleddP pauledd

          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.

          beeckscheB Offline
          beeckscheB Offline
          beecksche
          wrote on last edited by
          #4

          @pauledd

          Why don't you use a QGenericMatrix ?

          pauleddP 1 Reply Last reply
          0
          • beeckscheB beecksche

            @pauledd

            Why don't you use a QGenericMatrix ?

            pauleddP Offline
            pauleddP Offline
            pauledd
            wrote on 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

            mrjjM 1 Reply Last reply
            0
            • pauleddP pauledd

              @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

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on 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

              pauleddP 1 Reply Last reply
              6
              • mrjjM mrjj

                @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

                pauleddP Offline
                pauleddP Offline
                pauledd
                wrote on 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.

                mrjjM 1 Reply Last reply
                0
                • pauleddP pauledd

                  @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.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 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 :)

                  ODБOïO 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @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 :)

                    ODБOïO Offline
                    ODБOïO Offline
                    ODБOï
                    wrote on last edited by ODБOï
                    #9

                    @mrjj good catch ^^

                    1 Reply Last reply
                    1

                    • Login

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