Read access violation
-
Hello,
I have a list of checkbox ,
QCheckBox* cblist[6];
I am initializing that list in consturctor after setUI method
ui->setupUi(this); cblist[0] = ui->checkBox; cblist[1] = ui->checkBox_2; cblist[2] = ui->checkBox_3; cblist[3] = ui->checkBox_4; cblist[4] = ui->checkBox_5; cblist[5] = ui->checkBox_6;
I am changing their check state and color style in another slot function
ui->checkBox->setChecked(true); ui->checkBox_2->setChecked(false); ui->checkBox_3->setChecked(true); ui->checkBox_4->setChecked(false); ui->checkBox_5->setChecked(true); ui->checkBox_6->setChecked(false); for(auto cbIndex = 0; cbIndex < 6; ++cbIndex) { if(cblist[cbIndex]->isChecked()) { cblist[cbIndex]->setStyleSheet("QCheckBox { color : #55FF77; }"); } else { cblist[cbIndex]->setStyleSheet("QCheckBox { color : #FF2222; }"); } }
The strange thing is when I am adding another widget from creator it does give me a error (0xC0000005: Access violation reading location). I am certain that I am not deleting or modifying that cblist array and I dont have a logical example or explanation, but I couldnt find anything and it does keep going like that.
Thank you for your time and help.
-
Hello,
I have a list of checkbox ,
QCheckBox* cblist[6];
I am initializing that list in consturctor after setUI method
ui->setupUi(this); cblist[0] = ui->checkBox; cblist[1] = ui->checkBox_2; cblist[2] = ui->checkBox_3; cblist[3] = ui->checkBox_4; cblist[4] = ui->checkBox_5; cblist[5] = ui->checkBox_6;
I am changing their check state and color style in another slot function
ui->checkBox->setChecked(true); ui->checkBox_2->setChecked(false); ui->checkBox_3->setChecked(true); ui->checkBox_4->setChecked(false); ui->checkBox_5->setChecked(true); ui->checkBox_6->setChecked(false); for(auto cbIndex = 0; cbIndex < 6; ++cbIndex) { if(cblist[cbIndex]->isChecked()) { cblist[cbIndex]->setStyleSheet("QCheckBox { color : #55FF77; }"); } else { cblist[cbIndex]->setStyleSheet("QCheckBox { color : #FF2222; }"); } }
The strange thing is when I am adding another widget from creator it does give me a error (0xC0000005: Access violation reading location). I am certain that I am not deleting or modifying that cblist array and I dont have a logical example or explanation, but I couldnt find anything and it does keep going like that.
Thank you for your time and help.
@ikuris said in Read access violation:
I am certain that I am not deleting or modifying that cblist array
Don't be certain - check it by yourself by e.g. printing out the pointer values.
-
@ikuris said in Read access violation:
I am changing their check state and color style in another slot function
It looks ok but the thing that I'd suspect is that you are calling your slot function BEFORE the constructor is executed.
Something else to think about: modern c++ standards.
the more modern paradigm for handling arrays like you are creating is something like:
std::vector<QCheckBox*> clist; // in class definition
clist = { ui->cb1, ui->cb2, ui->cb3, ui-cb4 }; // in constructor
and then your loops are controlled by clist.size();
In this way you've removed the need for a MAX_INDEX value of checkboxes: a potential source of error.
Here's another shortcut
setStyleSheet(isChecked ? "true style" : "false style");
-
@ikuris said in Read access violation:
for(auto cbIndex = 0; cbIndex < 6; cbIndex++)
pre or post increment makes no difference because the incrementing of loop variant is done at the bottom of the loop, not the top.