PDA

View Full Version : Grey Out Spin Box



Atomic_Sheep
20th September 2017, 10:39
I can't seem to find any mention in the documentation about spin boxes having a property of being greyed out.

I have tried:


this->ui->mySpinBox->setReadOnly(true);

&


this->ui->mySpinBox->setEnabled(false);

But this didn't work.

high_flyer
20th September 2017, 15:38
setEnabled(false) should grey it out.
But it will also disable it. (takes no user input)
If this is not working probably somewhere else in your code it is being enabled again.
You can override the changeEvent() method to see when it gets enabled again.

Atomic_Sheep
21st September 2017, 11:44
Bingo it works, thanks for that!

My problem was that my loop wasn't getting entered where the setEnabled(false) was being called. Who would have thought that the QCheckBox is 0 when disabled and 2 when endabled? I'm guessing 1 is when it's in its tri'th state. I obviously was checking for 1.

high_flyer
21st September 2017, 14:47
Who would have thought that the QCheckBox is 0 when disabled and 2 when endabled?
Anyone who'd read the documentation:
http://doc.qt.io/qt-5/qt.html#CheckState-enum


I'm guessing 1 is when it's in its tri'th state.
No need to guess.
It's bad practice to guess and use naked enum values for several reasons:
First, (in general) there is no guarantee the naked value will stay the same in future releases which will break your code if you use the naked values.
Second, its less readable, just like you don't know what '1' '2' or any other values means, so will anyone else who reads your code, or you in the future.
On the other hand if you use the named enum values anyone reading the code knows what the value is, and no one needs to care what the naked value behind it is, and the code will continue to work even if the naked value in the enum will change in the future.
And, be aware that enum does not have to be an int, it also could be a bool for example which can bring about really odd behavior if your are working with naked values expecting ints.

Use the named enum values, you will stay safe that way.

Atomic_Sheep
24th September 2017, 04:09
Anyone who'd read the documentation:
http://doc.qt.io/qt-5/qt.html#CheckState-enum

Yep, that's where I learned about it after I realised my loop wasn't being entered as expected.

Only guessing because I don't need to know this at this stage, but I know that the documentation is where to find it in the future if I need it :).

high_flyer
25th September 2017, 14:19
Only guessing because I don't need to know this at this stage, but I know that the documentation is where to find it in the future if I need it
This comment was mostly meant to point out to not use of raw int values for the enums - which is why you where needing to guess.
Using the named enum values will save you and others reading your code the need to guess what these values mean.

Atomic_Sheep
30th September 2017, 04:45
Ah I see where you were going with that now.