C++ 如何验证 Qt 中的复选框是否已选中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27196428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I verify if the checkbox is checked or not in Qt?
提问by user3396248
The following code is supposed to set the text of nameLine
form to this box is unchecked
when the QCheckBox
instance checkbox
has state Unchecked
.
下面的代码应该将nameLine
表单的文本设置为this box is unchecked
当QCheckBox
实例checkbox
具有 state 时Unchecked
。
Here is the my checkbox instance declaration:
这是我的复选框实例声明:
QCheckBox *checkbox = new QCheckBox("paid with cash!", this);
checkbox->setCheckState(Qt::Unchecked);
and here is the logic so far:
这是到目前为止的逻辑:
if(checkbox->checkState(Qt::Unchecked))
{
nameLine->setText("the box is unchecked");
}
This code does not compile. The resulting error is the following:
此代码无法编译。结果错误如下:
C:\Qt.1.1\mingw48_32\examples\widgets\tutorials\addressbook\part1\voruskra.cpp:144: error: no matching function for call to 'QCheckBox::checkState(Qt::CheckState)'
if(checkbox->checkState(Qt::Unchecked))
^
Can you tell me what I am doing wrong?
你能告诉我我做错了什么吗?
回答by dtech
Unless you are using a tristate checkbox, you can simply if (checkbox->isChecked())
除非您使用三态复选框,否则您可以简单地 if (checkbox->isChecked())
This property is inherited way back in QAbstractButton
. If it is a tristate checkbox, you will have to use checkState()
as suggested in the other answer.
此属性在QAbstractButton
. 如果它是三态复选框,则必须checkState()
按照其他答案中的建议使用。
回答by styvane
I think checkState
doesn't take any argument. Try if(checkbox->checkState() == Qt::Unchecked)
我认为checkState
不需要任何争论。尝试if(checkbox->checkState() == Qt::Unchecked)
回答by procatmer
maybe you can try like this?
也许你可以这样试试?
QCheckBox *checkbox = new QCheckBox("paid with cash!", this);
checkbox->setChecked(false);
then for if command..
那么对于 if 命令..
if(!checkbox->isChecked)
{
nameLine->setText("the box is unchecked");
}