如何使用C++在QT中的QInputDialog中隐藏"取消"按钮?

Abh*_*ogi 3 c++ qt4

#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;
}
Run Code Online (Sandbox Code Playgroud)

在这个程序中,我没有输入窗口(do-while循环的第4行)有取消按钮.我怎么做?我刚开始学习QT.所以,抱歉,如果这是一个非常基本的问题.

而且我如何使用取消按钮来停止应用程序.. Bcos,现在CANCEL按钮什么都不做.

Jas*_*n B 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);
Run Code Online (Sandbox Code Playgroud)

如果ok返回false,则用户单击取消,您可以退出循环.您可以在文档中看到value,minValue,maxValue和step mean: QInputDialog文档