Solved Why VerticalHeaderItem cause crit.error
-
When i add any action to my VertHItem - in runtime it cause crit error and the program stop working. I've included QHeaderView, certain Item indeed exist, but i simply can't set text for it:
if(tabWidGuide.verticalHeaderItem(0)->text()=="1") tabWidGuide.verticalHeaderItem(0)->setBackground(Qt::red);
That code assures that item at '0' exist and it's label is 1, which is true, it 1 by default in my app.
P.S. here is it in documentation
-
Hi
You should check its not NULL
try
qDebug() << "item = " << tabWidGuide.verticalHeaderItem(0); -
@Engelard said in Why VerticalHeaderItem cause crit.error:
if(tabWidGuide.verticalHeaderItem(0)->text()=="1") tabWidGuide.verticalHeaderItem(0)->setBackground(Qt::red);
To add to @mrjj : you should ALWAYS check pointers before dereferencing them!
if(tabWidGuide.verticalHeaderItem(0) && tabWidGuide.verticalHeaderItem(0)->text()=="1") tabWidGuide.verticalHeaderItem(0)->setBackground(Qt::red);
-
And that's exactly the difference between pointers and references you have to understand.
Pointers have to be checked against nullptr before de-referencing, references can always be used.
-
-
Tnx for all advices for checking item, now it shows that header does'nt exist. BUT. Why it exist in the app, and it returning like it does'nt?
-
Why it exist in the app, and it returning like it does'nt?
- How do you set it?
- Do you set it before your read it back?
-
@aha_1980 i didn't set anything, i assume that it set by default, so why would i set it..
-
@Engelard said in Why VerticalHeaderItem cause crit.error:
i assume that it set by default,
It isn't.