2

I'm doing some preparation for an exam using Qt framework and I would like to know how to use QInputDialog and QMessageBox in a basic way (my exams are hand written coding)

The Qt API is really confusing to understand when it comes to using and it was fine for my projects because I could accomplish what I wanted in a really "hacky" way a my set book on the subject is very poorly laid out...

Let me get to the point, what would be a clean way of using QInputDialog and QMessageBox in this scenario:

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

For my QInputDialog I want the user to give their birth date (don't worry about input validation) I want to use the QMessageBox to show the user's age

I just don't understand what parameters need to go into the QInputDialog and QMessageBox in a basic case like because there don't seem to be any examples out there.

How would I accomplish this?

demonplus
  • 5,613
  • 12
  • 49
  • 68
Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143

1 Answers1

6

You can do something like:

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)));
    }
    [..]
vahancho
  • 20,808
  • 3
  • 47
  • 55
  • Does that parameter after QLineEdit::Normal represent the default text in the line? – Barry Michael Doyle Apr 29 '15 at 12:59
  • @BarryDoyle, yes. You can experiment yourself with different values and see how they effect. – vahancho Apr 29 '15 at 13:04
  • Awesome thanks man, I really had no idea where to start when it came to this stuff :) – Barry Michael Doyle Apr 29 '15 at 13:04
  • And for the QMessageBox could I also write the final parameter to be `QString("The age is " + QString::number(age))`? I'm just working on finding simpler easier ways to write this down for understanding... – Barry Michael Doyle Apr 29 '15 at 13:37
  • I suggest you read about the parameters of the static methods in the official documentation. All the arguments are described there – Bowdzone Apr 29 '15 at 13:45
  • @Bowdzone can you send a link because it seems like all the documentation I look up is really confusing and hard to understand... – Barry Michael Doyle Apr 29 '15 at 18:42
  • @BarryDoyle [This is the official documentation](http://doc.qt.io/qt-5/qinputdialog.html#getText). It contains a lot of cross-references if something seems unclear – Bowdzone Apr 29 '15 at 18:53