繁体   English   中英

QInputDialog和QMessageBox

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM