From 5f0fbc683bce200b17fea82eb711d8ad4c2f3f23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Br=C3=BCning?= Date: Mon, 7 Apr 2014 14:06:26 +0200 Subject: Fix up Fancy Browser example. * Prevent leaking of MainWindow. * Use proper command line argument parsing. * Make the Qt project page the default page. Task-number: QTBUG-38069 Change-Id: I6142ca207e659e47136e47e4d5817dfa617677e8 Reviewed-by: Allan Sandfeld Jensen --- examples/webkitwidgets/fancybrowser/main.cpp | 42 ++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 6 deletions(-) (limited to 'examples') diff --git a/examples/webkitwidgets/fancybrowser/main.cpp b/examples/webkitwidgets/fancybrowser/main.cpp index 451f247..bde31b0 100644 --- a/examples/webkitwidgets/fancybrowser/main.cpp +++ b/examples/webkitwidgets/fancybrowser/main.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the examples of the Qt Toolkit. @@ -41,15 +41,45 @@ #include #include "mainwindow.h" +static void showHelp(QCommandLineParser &parser, const QString errorMessage = QString()) +{ + QString text; + QTextStream str(&text); + str << ""; + if (!errorMessage.isEmpty()) + str << "

" << errorMessage << "

"; + str << "
" << parser.helpText() << "
"; + QMessageBox box(errorMessage.isEmpty() ? QMessageBox::Information : QMessageBox::Warning, + QGuiApplication::applicationDisplayName(), text, QMessageBox::Ok); + box.setTextInteractionFlags(Qt::TextBrowserInteraction); + box.exec(); +} + int main(int argc, char * argv[]) { QApplication app(argc, argv); + + QCommandLineParser commandLineParser; + commandLineParser.addPositionalArgument(QStringLiteral("url"), + QStringLiteral("The url to be loaded in the browser window.")); + commandLineParser.process(app); + QStringList positionalArguments = commandLineParser.positionalArguments(); + QUrl url; - if (argc > 1) - url = QUrl::fromUserInput(argv[1]); + if (positionalArguments.size() > 1) { + showHelp(commandLineParser, QStringLiteral("Too many arguments.")); + return -1; + } else if (positionalArguments.size() == 1) + url = QUrl::fromUserInput(positionalArguments.at(0)); else - url = QUrl("http://www.google.com/ncr"); - MainWindow *browser = new MainWindow(url); - browser->show(); + url = QUrl("http://www.qt-project.org"); + + if (!url.isValid()) { + showHelp(commandLineParser, QString("%1 is not a valid url.").arg(positionalArguments.at(0))); + return -1; + } + + MainWindow browser(url); + browser.show(); return app.exec(); } -- cgit v1.2.3