[英]Qt5 Unidentified reference to QInputDialog and QMessageBox.. ERROR
[英]QInputDialog and QMessageBox
我正在為使用Qt框架的考試做一些准備,我想知道如何以基本方式使用QInputDialog和QMessageBox(我的考試是手寫編碼)
Qt API確實使人難以理解使用時間,這對我的項目來說很好,因為我可以以一種真正的“ hacky”方式完成我想要的事情,而我關於該主題的書本布局很差。
讓我指出,在這種情況下使用QInputDialog和QMessageBox的一種干凈方法是:
#include <QApplication>
#include <QInputDialog>
#include <QDate>
#include <QMessageBox>
int computeAge(QDate id) {
int years = QDate::currentDate().year() - id.year();
int days = QDate::currentDate().daysTo(QDate
(QDate::currentDate().year(), id.month(), id.day()));
if(days > 0)
years--;
return years
}
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
/* I want my QInputDialog and MessageBox in here somewhere */
return a.exec();
}
對於我的QInputDialog,我希望用戶提供其生日(不用擔心輸入驗證),我想使用QMessageBox來顯示用戶的年齡
在基本情況下,我只是不了解需要向QInputDialog和QMessageBox中輸入哪些參數,因為似乎沒有任何示例。
我將如何完成?
您可以執行以下操作:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
bool ok;
// Ask for birth date as a string.
QString text = QInputDialog::getText(0, "Input dialog",
"Date of Birth:", QLineEdit::Normal,
"", &ok);
if (ok && !text.isEmpty()) {
QDate date = QDate::fromString(text);
int age = computeAge(date);
// Show the age.
QMessageBox::information (0, "The Age",
QString("The age is %1").arg(QString::number(age)));
}
[..]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.