Instantiate widgets with a list of check boxes and QPair
-
Hello, I would like to create a widget with a list of
QCheckBox, which the user can choose from, and a Start button that instantiates and shows other windows (widgets) according to the checked boxes, knowing that all these widgets are different classes.Yesterday I found out about the existence of
QPairand I had the idea to pair a check box and a null instantiation of a widget, e.g.:QVector<QPair<QCheckBox*, QWidget*>> vectorOptions; vectorOptions << qMakePair(new QCheckBox("Option 1"), (WidgetChild1*) nullptr); vectorOptions << qMakePair(new QCheckBox("Option 2"), (WidgetChild2*) nullptr);The idea is to connect a signal produced when the Start button is pressed, to the slot
handleStartButton(). The problem is that I can't instantiate aQWidgetpointer inQPair::secondand I don't understand why.void Class::handleStartButton() { QVectorIterator<QPair<QCheckBox*, QWidget*>> itPairVector(vectorOptions); QPair<QCheckBox*, QWidget*> current; while(itPairVector.hasNext()) { current = itPairVector.next(); if(current.first->isChecked() && current.second == nullptr) { WidgetChild1 *tmp = new WidgetChild1; current = qMakePair(current.first, tmp); //current = qMakePair(current.first, new WidgetChild1); //line above would be equal to the two previous ones qDebug() << "in if: second initialized?" << current.second; } } QVectorIterator<QPair<QCheckBox*, QWidget*>> itPairVector2(vectorOptions); while(itPair2.hasNext()) { qDebug() << "after: second initialized?" << itPair2.next().second; } }With this code, I am able to instantiate a
WidgetChild1object, show it and the debug prints something likein if: second initialized? WidgetChild1(0x55562182c540)However, I have the warning
Potential leak of memory pointed to by field 'second'at the end of the first while loop, which means that the widgets do not stick toQPair::second.
I can verify this with the second while loop: ifQPair:secondwas correctly initialized, it would show something similar to the first debug, but it printsafter: second initialized? QWidget(0x0) after: second initialized? QWidget(0x0)so it still points to
nullptr.I would appreciate some information as to how I can correctly instantiate the widget.
-
@diego-qt said in Instantiate widgets with a list of check boxes and QPair:
current = itPairVector.next();
This creates a copy of your QPair.
Use a range-based for loop:for (auto ¤t : vectorOptions) { if(current.first->isChecked() && current.second == nullptr) { current.second = new WidgetChild1; } } -
Thank you very much! This worked instantly. I see that I was not enough documented about QPair. I will dig into the range-based loops because I didn't know about them.
However I have a warningunused variable 'current'. It doesn't make sense to me because I use it in the loop. Do you know why this happens? -
Thank you very much! This worked instantly. I see that I was not enough documented about QPair. I will dig into the range-based loops because I didn't know about them.
However I have a warningunused variable 'current'. It doesn't make sense to me because I use it in the loop. Do you know why this happens?@diego-qt said in Instantiate widgets with a list of check boxes and QPair:
Do you know why this happens?
You did not remove your variable
currentwhich you defined (for unknown reasons) before your while loop.