2011-07-24 50 views
1

我是Qt/C++的初學者,所以在我從一些教程中學習時,我想做一些其他的教程,但不知道如何實現。我該如何處理if(2變量)?

下面是代碼,我使用的是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"); 
    } 
} 

我想要做的是使第三if()其中包含2個變量,所以在這種情況下 如果沒有複選框檢查顯示一條消息,但我不知道如何使用2個變量在QT裏面如果

if(2variableshere) 
    { 
    Do... 
    } 
+2

請在這裏發佈代碼,而不是在外部網站。 –

+0

您缺乏語言基礎知識,因此首先需要閱讀某種C++語言書籍。我不知道你是否注意到了Qt中的一些內容:「如何學習Qt」:我們假設你已經知道C++並將用它來開發Qt。 –

回答

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."); 
    } 
} 
+0

所以||象徵(和)正確? – ibanezz

+2

@ibanezz:不,&&是和。 ||是或。我認爲你應該閱讀[C++書](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)。 –

+1

ibanezz:不,「||」是「OR」。在C++中,你甚至可以明確地寫出'和','或'等(而不是符號)。 – rubenvb

11

要創建一個使用需要兩個值聲明是真實的,你跟運營商,&&將它們結合起來:

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

要找出一個值是假的,你用運營商,!

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

有了這個邏輯,你可以創建這個語句中檢查這兩個值不檢查。

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

相關的和運營商是操作,||

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

我們可以用不同但等效的方式使用它來構建這個條件:

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 
1

您可以使用邏輯運算符 - AND(& &)或(||)使用多個變量i如果陳述。顯然,變量是Bool類型。你也可以在返回布爾類型值的if語句中傳遞函數..

+0

並且在這種情況下如果我想使: 如果2個框被選中消息「Use only one!」我將使用這個: QMessageBox :: information(this,「Result」,「Please choose one one!」if(ui-> checkBox-> isChecked()&& ui-> checkBox_2-> isChecked()) { ); } – ibanezz

+0

這可以工作,但在出現消息後,其他2條消息顯示!! – ibanezz

3

我會這樣做的(如果我有複選框,必須選擇一個 - 我可能會使用單選按鈕或兩個選項,而不是一個組合框,取決於在GUI的其餘部分,從而確保只有一個可能性選擇):

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???"); 
    } 
} 

您不需要這裏的if(2 variables)檢查,因爲else會照顧到這一點。但是如果你需要其他地方的東西,你當然可以使用& &和||結合這兩個條件。

正如別人所說,如果您閱讀關於C或C++的教科書,這將是明智之舉。