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 thatouter.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)
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.
-
@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...
-
@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
-
@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
Cheers