1

I'm a beginner in Qt/C++ so while I was learning from some tutorials I wanted to make something other than the tutorial but didn't know exactly the way how.

Here is the code, I'm using curently http://pastebin.com/x612nAPV

#include "dialog.h"
#include "ui_dialog.h"
#include <QtGui>
#include <QMessageBox>
#include <QString>
 
Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    //ui->checkBox->setChecked(true);
 
}
 
Dialog::~Dialog()
{
    delete ui;
}
 
void Dialog::on_pushButton_clicked()
{
 
    if(ui->checkBox->isChecked())
    {
        QMessageBox::information(this,"Result","Cool another Male !");
    }
 
    if(ui->checkBox_2->isChecked())
    {
        QMessageBox::information(this,"Result", "Yay a female");
    }
}

What I want to do is to make a third if() which contains 2 variables, so that in this case if none of the check boxes is checked to show a message, but I don't know how to use 2 variables in qt inside an if

like

if(2variableshere)
  {
     Do...
  }
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
ibanezz
  • 95
  • 2
  • 5
  • 2
    Please post the code here, not at an external site. – Anders Abel Jul 24 '11 at 11:44
  • You lack of language basics, so first of all go through some kind of C++ language book. I don't know if you noticed something in Qt "How to learn Qt": We assume that you already know C++ and will be using it for Qt development. – Kamil Klimek Jul 25 '11 at 17:42

4 Answers4

11

To create an if statement that requires two values to be true, you combine them with the and operator, &&:

if (a && b) {
// if a and b are true

To find out if a value is false, you use the not operator, !:

if (!a) {
// if a is false

With this logic you can create this statement checking that both values are not checked.

if (!ui->checkBox_1->isChecked() && !ui->checkBox_2->isChecked()) {
// if box 1 isn't checked and box 2 isn't checked...

Related to the and operator is the or operator, ||:

if (a || b) {
// if a or b is true

We can use this to structure this condition in a different but equivalent way:

if (!(ui->checkBox_1->isChecked() || ui->checkBox_2->isChecked())) {
// if it is not the case that box 1 is checked or box 2 is checked
Hasturkun
  • 35,395
  • 6
  • 71
  • 104
Jeremy
  • 1
  • 85
  • 340
  • 366
3

I would do it like this (if I had checkboxes and one had to be chosen -- I would probably use radio buttons or a combobox with the two options instead, depending in the rest of the GUI, thus ensuring that only one of the possibilities was chosen):

void Dialog::on_pushButton_clicked()
{
    if(ui->checkBox->isChecked())
    {
        QMessageBox::information(this, "Result", "Cool another Male !");
    }
    else if(ui->checkBox_2->isChecked())
    {
        QMessageBox::information(this, "Result", "Yay a female");
    }
    else
    {
        QMessageBox::information(this, "Result", "What are you???");
    }
}

You don't need the if(2 variables) check here, since else will take care of that. But if you need something like it elsewhere, you can, of course use && and || to combine the two conditions.

As someone else said, it would be wise if you read a textbook about C or C++.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
1
void Dialog::on_pushButton_clicked()
{
    if(ui->checkBox->isChecked())
    {
        QMessageBox::information(this,"Result","Cool another Male !");
    }

    if(ui->checkBox_2->isChecked())
    {
        QMessageBox::information(this,"Result", "Yay a female");
    }

    if(!(ui->checkBox->isChecked() || ui->checkBox_2->isChecked()))
    {
        QMessageBox::infomration(this,"Result","Nothing checked.");
    }
}
Anders Abel
  • 67,989
  • 17
  • 150
  • 217
  • 2
    @ibanezz: No, && is and. || is or. I think you should read a [book on C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Armen Tsirunyan Jul 24 '11 at 11:55
  • 1
    ibanezz: no, `||` is `OR`. In C++ you could even write `and`, `or` etc. explicitly (instead of the symbols). – rubenvb Jul 24 '11 at 11:57
1

You can use Logical operators - AND ( && ) OR ( || ) to use multiple variables in if statements. Obviously the variables are of Bool type.You can also pass functions in if statements which return bool type values..

  • and in this case If I want to make: If 2 boxes are checked message "Use only one !" I'll use this: if(ui->checkBox->isChecked() && ui->checkBox_2->isChecked()) { QMessageBox::information(this,"Result","Please chose only one !"); } – ibanezz Jul 24 '11 at 11:56
  • This works but then after the message appears, other 2 messages appear to !? – ibanezz Jul 24 '11 at 11:58