0

The following code is not working when the checkbox is checked. Whether the checkbox is checked or not, it jumps to the "else" statement.

   void MainWindow::runButtonClicked()

        {
            if (MainWindow::betAllRed->isChecked()==true){
                red.didBet=true;
                qDebug()<<"bet Red true";
            } else{
                qDebug()<<"red not checked";
            }
        }

is there anything noticeably wrong here or is the issue elsewhere? Do I need to connect a slot to get the check state?

hyde
  • 60,639
  • 21
  • 115
  • 176
chuckieDub
  • 1,767
  • 9
  • 27
  • 46
  • 1
    What _looks_ wrong is the way you access the `betAllRed` member. Using simply `betAllRed->` (without qualifier) or `this->betAllRed` if you need disambiguation would be more usual. – Mat Dec 30 '12 at 08:47
  • @Mat it's not wrong, it's just redundant. That syntax can be used to access an identifier in a specific namespace/scope, MainWindow:: in this case, which would be used anyway. (Edit: But I guess you know this, now that I re-read what you wrote in your comment, but leaving this as clarification anyway.) – hyde Dec 30 '12 at 10:16
  • Did you use Designer to create the GUI? – hyde Dec 30 '12 at 10:20

2 Answers2

1

the problem is the way you access the betAllRed checkbox. if you are using the designer you can access it using Ui;

if(ui->betAllRed->isChecked())

if you are using your own code:

QComboBox *betAllRed = new QComboBox(this);

just access it using:

if(this->betAllRed->isChecked())
Ahmed Kato
  • 1,697
  • 4
  • 29
  • 53
1

What I suspect you are doing wrong is, you actually have two betAllRed fields: You have created and initialized QCheckBox* MainWindow::betAllRed, and then you also have a checkbox in MainWindow::ui (possibly with same name, if you renamed it in Designer, otherwise with the default name created by Designer).

If this is the case, remove your own betAllRed, then fix the code to use ui->betAllRed to access the checkbox (and possibly rename the checkbox in Designer if it has default name now).

And then a coding style note about this line of yours:

if (MainWindow::betAllRed->isChecked()==true){

That line is equal to just this, which would be much shorter and clearer:

if (betAllRed->isChecked()) {
hyde
  • 60,639
  • 21
  • 115
  • 176