這是我第一週做Qt,所以如果我不瞭解基本知識,請原諒我。在下面代碼的註釋部分,我想編寫處理QInputDialog
上取消按鈕的代碼。如何處理用戶按下QInputDialog中的取消按鈕?
#include <QtWidgets>
int main (int argc, char* argv[]) {
QApplication app(argc, argv);
QTextStream cout(stdout);
int answer;
do {
int celciusArg = 0;
int farenheit;
celciusArg = QInputDialog::getInt(0, "Celcius Calculator",
"Convert this number to Farenheit:", 1);
// I'd like to say here:
// if (user clicked cancel)
// then (close the widget)
cout << "User entered: " << celciusArg
<< endl;
farenheit = celciusArg * 1.8 + 32;
QString response = QString("%1 degrees celcius is %2 degrees farenheit .\n%3")
.arg(celciusArg).arg(farenheit) /* Each %n is replaced with an arg() value. */
.arg("Convert another temperature?"); /* Long statements can continue on multiple lines, as long as they are broken on token boundaries. */
answer = QMessageBox::question(0, "Play again?", response,
QMessageBox::Yes| QMessageBox::No); /* Bitwise or of two values. */
} while (answer == QMessageBox::Yes);
return EXIT_SUCCESS;
}
沒有測試但也許連接'''拒絕'''從對話框到處理函數的信號? – cen
雖然這個問題是相當基礎的,但它確實包含了完整的源代碼 - 所以對此提問者非常讚賞。降價是相當沒有根據的恕我直言。 –