Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QCheckBox->;isChecked()问题_C++_Qt_Qcheckbox - Fatal编程技术网

C++ QCheckBox->;isChecked()问题

C++ QCheckBox->;isChecked()问题,c++,qt,qcheckbox,C++,Qt,Qcheckbox,选中该复选框时,以下代码不起作用。无论复选框是否选中,它都会跳转到“else”语句 void MainWindow::runButtonClicked() { if (MainWindow::betAllRed->isChecked()==true){ red.didBet=true; qDebug()<<"bet Red true"; } e

选中该复选框时,以下代码不起作用。无论复选框是否选中,它都会跳转到“else”语句

   void MainWindow::runButtonClicked()

        {
            if (MainWindow::betAllRed->isChecked()==true){
                red.didBet=true;
                qDebug()<<"bet Red true";
            } else{
                qDebug()<<"red not checked";
            }
        }
void主窗口::runButtonClicked()
{
如果(主窗口::betAllRed->isChecked()==true){
red.didBet=true;

qDebug()问题在于访问betAllRed复选框的方式。 如果您正在使用设计器,则可以使用Ui访问它

if(ui->betAllRed->isChecked())
如果您正在使用自己的代码:

QComboBox *betAllRed = new QComboBox(this);
只需通过以下方式访问:

if(this->betAllRed->isChecked())

我怀疑您做错了的是,您实际上有两个
betAllRed
字段:您创建并初始化了
QCheckBox*MainWindow::betAllRed
,然后在MainWindow::ui中也有一个复选框(如果您在Designer中重命名它,则可能使用相同的名称,否则使用Designer创建的默认名称)

如果是这种情况,请删除您自己的
betAllRed
,然后修复代码以使用
ui->betAllRed
访问复选框(如果复选框现在具有默认名称,则可能在Designer中重命名该复选框)

然后是关于这一行的编码风格说明:

if (MainWindow::betAllRed->isChecked()==true){
这条线就等于这条线,这条线要短得多,也要清晰得多:

if (betAllRed->isChecked()) {

错误的是您访问
betAllRed
成员的方式。只需使用
betAllRed->
(不带限定符)或者
this->betAllRed
如果需要消歧,则更为常见。@Mat这没有错,只是多余。该语法可用于访问特定命名空间/作用域中的标识符,MainWindow::在本例中,无论如何都会使用。(编辑:但我想你知道这一点,现在我重新阅读了你在评论中所写的内容,但不管怎么说,这只是澄清。)你使用Designer创建GUI了吗?