Qt: QList QButtonGroup
Hey! I am trying to do the following
QList<QButtonGroup*> groups;
for (int i=0; i<nGroup; i++)
{
QButtonGroup *objects = new QButtonGroup(this);
objects->setExclusive(false);
for (int j=0; j<nObject; j++)
{
Led *tempLed = new Led();
tempLed->setAutoExclusive(false);
layout->addWidget(tempLed,j,i,Qt::AlignLeft);
objects->addButton(tempLed);
}
groups.append(objects);
}
And then try doing something like this:
groups.at(1)->button(2)->setChecked(true);
Compiles your code, but throws an unhandled exception at runtime. What am I doing wrong? Any better way to create a QButtonGroup?
a source to share
The QButtonGroup :: button function returns the button for a specific ID, but you didn't use the ID when you added the button to the buttongroup. QButtonGroup :: button returns 0 in your example, which results in a null pointer access exception.
... objects->addButton(tempLed); ...
If you change your code to
... objects->addButton(tempLed, j ); ...
your original code will work.
I prefer QList :: at over QList :: operator [] because you don't want to change the value (== pointer) in the list.
a source to share
I think the problem is with the at function . It returns a const element and you call a non-const function on it.
Use operator [] instead .
a source to share