diff options
author | Friedemann Kleint <Friedemann.Kleint@theqtcompany.com> | 2015-07-17 10:41:44 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@theqtcompany.com> | 2015-07-17 14:15:11 +0000 |
commit | 2fe56e37ede40dc703a9c3c8eb240b80c9c00757 (patch) | |
tree | aca858f6e085f23b254df4a7a08c0c10e9805f34 /examples/widgets/mainwindows/mainwindow/main.cpp | |
parent | 7f77d4fcd5534ec0973654e37c8ef189dce35778 (diff) |
Polish the examples/widgets/mainwindows example.
- Introduce Qt 5 signals & slot syntax.
- Replace QSignalMapper used for the corner/area
actions by a by a functor.
- Improve command line parsing.
- Reorder class declarations.
- Remove commented-out code.
- Use QDialogButtonBox in dialogs.
- Fix minor issues in code, use multi-argument version
of QString::arg(), QDir::toNativeSeparators() to
present file paths to the user, static method
invocations.
Change-Id: I865c56639c74135b59740797e9a9dfbfca2e72b6
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
Diffstat (limited to 'examples/widgets/mainwindows/mainwindow/main.cpp')
-rw-r--r-- | examples/widgets/mainwindows/mainwindow/main.cpp | 67 |
1 files changed, 44 insertions, 23 deletions
diff --git a/examples/widgets/mainwindows/mainwindow/main.cpp b/examples/widgets/mainwindows/mainwindow/main.cpp index 2192074791..b32b595a2d 100644 --- a/examples/widgets/mainwindows/mainwindow/main.cpp +++ b/examples/widgets/mainwindows/mainwindow/main.cpp @@ -37,9 +37,10 @@ #include <QPainterPath> #include <QPainter> #include <QMap> -#include <qdebug.h> +#include <QDebug> -void render_qt_text(QPainter *painter, int w, int h, const QColor &color) { +void render_qt_text(QPainter *painter, int w, int h, const QColor &color) +{ QPainterPath path; path.moveTo(-0.083695, 0.283849); path.cubicTo(-0.049581, 0.349613, -0.012720, 0.397969, 0.026886, 0.428917); @@ -108,47 +109,67 @@ void render_qt_text(QPainter *painter, int w, int h, const QColor &color) { painter->drawPath(path); } -void usage() +static void usage() { qWarning() << "Usage: mainwindow [-SizeHint<color> <width>x<height>] ..."; exit(1); } -QMap<QString, QSize> parseCustomSizeHints(int argc, char **argv) -{ - QMap<QString, QSize> result; - - for (int i = 1; i < argc; ++i) { - QString arg = QString::fromLocal8Bit(argv[i]); +enum ParseCommandLineArgumentsResult { + CommandLineArgumentsOk, + CommandLineArgumentsError, + HelpRequested +}; +static ParseCommandLineArgumentsResult + parseCustomSizeHints(const QStringList &arguments, MainWindow::CustomSizeHintMap *result) +{ + result->clear(); + const int argumentCount = arguments.size(); + for (int i = 1; i < argumentCount; ++i) { + const QString &arg = arguments.at(i); if (arg.startsWith(QLatin1String("-SizeHint"))) { - QString name = arg.mid(9); + const QString name = arg.mid(9); if (name.isEmpty()) - usage(); - if (++i == argc) - usage(); - QString sizeStr = QString::fromLocal8Bit(argv[i]); - int idx = sizeStr.indexOf(QLatin1Char('x')); + return CommandLineArgumentsError; + if (++i == argumentCount) + return CommandLineArgumentsError; + const QString sizeStr = arguments.at(i); + const int idx = sizeStr.indexOf(QLatin1Char('x')); if (idx == -1) - usage(); + return CommandLineArgumentsError; bool ok; - int w = sizeStr.left(idx).toInt(&ok); + const int w = sizeStr.leftRef(idx).toInt(&ok); if (!ok) - usage(); - int h = sizeStr.mid(idx + 1).toInt(&ok); + return CommandLineArgumentsError; + const int h = sizeStr.midRef(idx + 1).toInt(&ok); if (!ok) - usage(); - result[name] = QSize(w, h); + return CommandLineArgumentsError; + result->insert(name, QSize(w, h)); + } else if (arg == QLatin1String("-h") || arg == QLatin1String("--help")) { + return HelpRequested; + } else { + return CommandLineArgumentsError; } } - return result; + return CommandLineArgumentsOk; } int main(int argc, char **argv) { QApplication app(argc, argv); - QMap<QString, QSize> customSizeHints = parseCustomSizeHints(argc, argv); + MainWindow::CustomSizeHintMap customSizeHints; + switch (parseCustomSizeHints(QCoreApplication::arguments(), &customSizeHints)) { + case CommandLineArgumentsOk: + break; + case CommandLineArgumentsError: + usage(); + return -1; + case HelpRequested: + usage(); + return 0; + } MainWindow mainWin(customSizeHints); mainWin.resize(800, 600); mainWin.show(); |