【发布时间】:2015-08-20 13:57:25
【问题描述】:
这是我使用 Qt 的第一周,如果我不太了解基础知识,请原谅我。在下面代码的注释部分,我想编写处理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;
}
【问题讨论】:
-
没有测试,但也许将对话框中的
rejected信号连接到处理函数? -
虽然这个问题相当基本,但它确实包含完整的源代码 - 所以向提问者致敬。恕我直言,反对票是毫无根据的。