Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Nested QVector problem



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



  • @pauledd hi

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


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



  • @pauledd

    Why don't you use a QGenericMatrix ?



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


  • Lifetime Qt Champion

    @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



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


  • Lifetime Qt Champion

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



  • @mrjj good catch ^^


Log in to reply