2009-09-12 17 views
2
#include <QtGui> 

int main (int argc, char* argv[]) 
{ 
    QApplication app(argc, argv); 
    QTextStream cout(stdout, QIODevice::WriteOnly);  

    // Declarations of variables 
    int answer = 0; 

    do { 
     // local variables to the loop: 
     int factArg = 0; 
     int fact(1); 
     factArg = QInputDialog::getInteger(0, "Factorial Calculator", 
      "Factorial of:"); 
     cout << "User entered: " << factArg << endl; 
     int i=2; 
     while (i <= factArg) { 
      fact = fact * i; 
      ++i; 
     } 
     QString response = QString("The factorial of %1 is %2.\n%3") 
      .arg(factArg).arg(fact) 
      .arg("Do you want to compute another factorial?");  
     answer = QMessageBox::question(0, "Play again?", response, 
      QMessageBox::Yes | QMessageBox::No ,QMessageBox::Yes); 
    } while (answer == QMessageBox::Yes); 

    return EXIT_SUCCESS; 
} 

在這個程序中,我沒有輸入窗口(第4行do-while循環)有取消按鈕。我怎麼做? 我剛開始學習QT。所以,如果它是一個非常基本的問題,很抱歉如何使用C++在QT中隱藏QInputDialog中的「取消」按鈕?

而且我該如何利用取消按鈕來停止應用程序.. Bcos,現在取消按鈕什麼也不做。

回答

5

QInputDialog是一個便捷類,它提供了一種快速簡單的方法來請求輸入,因此不允許進行太多的自定義。我沒有在文檔中看到任何內容,表明您可以更改窗口的佈局。我會建議通過擴展QDialog來設計自己的對話框。這將花費更多時間,但可以讓您自定義表單。

如果您確實想要確定是否在QInputDialog中按下了取消按鈕,則必須將指向bool的指針作爲第8個參數傳遞給getInteger()函數。

do{ 
    bool ok; 
    factArg = QInputDialog::getInteger(0, "Factorial Calculator", "Factorial of:", 
             value, minValue, maxValue, step, &ok); 
    if(!ok) 
    { 
     //cancel was pressed, break out of the loop 
     break; 
    } 
    // 
    // Do your processing based on input 
    // 
} while (some_condition); 

如果ok返回false,用戶點擊取消,你可以跳出你的循環。你可以看到什麼樣的價值,minValue(最小值),maxValue(最大值),和步驟文檔中的意思是: QInputDialog documentation

+0

Thanx Jason。我得到了我想要的東西。另外,我清除了這個bool *參數的概念。其實,這只是我第二天的學習。 – 2009-09-12 14:03:26

0

躲在QInputDialog的幫助按鈕的工作原理是通過正確的windowFlags:

QInputDialog inputDialog; 
bool ok; 
inputDialog.setWindowFlags(inputDialog.windowFlags() & (~Qt::WindowContextHelpButtonHint)); 
QString text = inputDialog.getText(this, tr("Factorial Calculator"), 
          tr("Enter some text:"), QLineEdit::Normal, QString(), &ok, 
          inputDialog.windowFlags()); 

// Or for integers 
int number = inputDialog.getInt(this, tr("Fractorial Calculator"), 
         tr("Enter a number:"), 0, -10, 10, 1, &ok, 
         inputDialog.windowFlags()); 
0

在Qt設計師的屬性編輯器,你可以自定義standardButtons屬性 -

enter image description here

這應該允許您控制正在呈現哪個對話框按鈕。

相關問題