Solved Easy question about QPushButton
-
Hello,
I have a QPushButton.
I want to make it checked.
So I write:ui.Encoded_File->setChecked(Qt::Checked);
I've got a warning C4305 about "truncation from 'Qt::CheckState' to 'bool'"
Is there any other constant that doesn't give a warning ?
I found the solution to write:bool(Qt::Checked)
Many thanks !
-
hi
http://doc.qt.io/qt-5/qabstractbutton.html#checkable-prop
setCheckable(true) <<< Say it can be checked
and
setCheckedui.Encoded_File->setChecked(true); << that "checks" it.
-
Sorry it's not the question.
I know that we can use true or false.
But to make the source more readable, it's better to use Qt constants.
Should we use bool(Qt::Checked)) ? -
But why would a constant make it more readable?
bool(Qt::Checked) is very confusing for the rest of us.
where as
setCheckable(true);
its 100% clearNormally you do not use enums when true/false is expected.
-
You can remove this warning with " btn->setCheckable(static_cast<bool>(Qt::Checked))" to make typecasting from int to bool, but this is ugly and not recommended.
-
Thanks for reply
-
Qt::Checked is an enum used for QCheckBox (http://doc.qt.io/qt-5/qt.html#CheckState-enum). It values are Checked, Unchecked and PartiallyChecked. QPushButton can only be checked or unchecked so it would not make sense to use this Enum. For example, how should the button react if it receives Qt::PartiallyChecked?
So just use the boolean as documented. It will not lead to less readable code.