Polish the mdi example.

Add a command line parser to be able to pass on files.

Task-number: QTBUG-35146
Change-Id: I32cbb9ec1e87667076e40a81c66674cf16836b54
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
Friedemann Kleint 2014-09-24 16:15:14 +02:00
parent 4b03b18548
commit 8f2519660b
3 changed files with 26 additions and 6 deletions

View File

@ -39,6 +39,8 @@
****************************************************************************/ ****************************************************************************/
#include <QApplication> #include <QApplication>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include "mainwindow.h" #include "mainwindow.h"
@ -47,7 +49,17 @@ int main(int argc, char *argv[])
Q_INIT_RESOURCE(mdi); Q_INIT_RESOURCE(mdi);
QApplication app(argc, argv); QApplication app(argc, argv);
QCoreApplication::setApplicationVersion(QT_VERSION_STR);
QCommandLineParser parser;
parser.setApplicationDescription("Qt MDI Example");
parser.addHelpOption();
parser.addVersionOption();
parser.addPositionalArgument("file", "The file to open.");
parser.process(app);
MainWindow mainWin; MainWindow mainWin;
foreach (const QString &fileName, parser.positionalArguments())
mainWin.openFile(fileName);
mainWin.show(); mainWin.show();
return app.exec(); return app.exec();
} }

View File

@ -95,16 +95,22 @@ void MainWindow::open()
return; return;
} }
MdiChild *child = createMdiChild(); if (openFile(fileName))
if (child->loadFile(fileName)) {
statusBar()->showMessage(tr("File loaded"), 2000); statusBar()->showMessage(tr("File loaded"), 2000);
child->show();
} else {
child->close();
}
} }
} }
bool MainWindow::openFile(const QString &fileName)
{
MdiChild *child = createMdiChild();
const bool succeeded = child->loadFile(fileName);
if (succeeded)
child->show();
else
child->close();
return succeeded;
}
void MainWindow::save() void MainWindow::save()
{ {
if (activeMdiChild() && activeMdiChild()->save()) if (activeMdiChild() && activeMdiChild()->save())

View File

@ -59,6 +59,8 @@ class MainWindow : public QMainWindow
public: public:
MainWindow(); MainWindow();
bool openFile(const QString &fileName);
protected: protected:
void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE; void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE;