Question

#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;
}

In this program, i dont to have Input window(4th line of do-while loop) to have cancel button. How do i do that? I have just started learning QT. So, sorry if its a very basic question.

And also how do i make use of cancel button to stop the application.. Bcos, right now CANCEL button does nothing.

Was it helpful?

Solution

QInputDialog is given as a convenience class that provides a quick and easy way to ask for an input and as such, does not allow for much customization. I don't see anything in the documentation to indicate that you can change the layout of the window. I would suggest just designing your own dialog by extending QDialog. This will take more time, but will allow you to customize the form.

If you do want to determine if the cancel button was pressed in the QInputDialog, you must pass a pointer to a bool into the getInteger() function as the 8th argument.

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);

If ok returns as false, the user clicked cancel and you can break out of your loop. You can see what value, minValue, maxValue, and step mean in the documentation: QInputDialog documentation

OTHER TIPS

Hiding the Help Button in a QInputDialog works by passing the correct 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());

In Qt Designer's Property Editor, you can customize the standardButtons property -

enter image description here

This should allow you to control which dialog buttons are being presented.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top