Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Easy question about QPushButton
I have a QPushButton.
I want to make it checked.
So I write:
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:
Many thanks !
setCheckable(true) <<< Say it can be checked
ui.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.
its 100% clear
Normally you do not use enums when true/false is expected.
dream_captain last edited by
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
t3685 last edited by t3685
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.