diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2016-10-31 11:09:00 +0100 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2017-02-17 05:55:19 +0000 |
commit | 8d71fae08078afbf4aa2de7429302f46d46fb43f (patch) | |
tree | dd67e91f7dc00c38582930a774981865c6d8d850 /examples/xml/dombookmarks/mainwindow.cpp | |
parent | 146f6d261ba743b643825d62cdb2deb1e701d3d2 (diff) |
Polish the XML bookmarks examples
- Use Qt 5 connect syntax.
- Streamline code, remove unused members.
- Add a context menu for copying and opening the URLs.
- Add const to XML code.
- In the XML code, show the use of QStringLiteral in static inline
functions to create strings versus QLatin1String in comparison
overloads to avoid allocating strings from const char * literals.
Change-Id: Ib5e62ca188e271ffe01996dff3c9ea8e0b60739a
Reviewed-by: Topi Reiniƶ <topi.reinio@qt.io>
Diffstat (limited to 'examples/xml/dombookmarks/mainwindow.cpp')
-rw-r--r-- | examples/xml/dombookmarks/mainwindow.cpp | 44 |
1 files changed, 14 insertions, 30 deletions
diff --git a/examples/xml/dombookmarks/mainwindow.cpp b/examples/xml/dombookmarks/mainwindow.cpp index e0f208d336..fade2dfc96 100644 --- a/examples/xml/dombookmarks/mainwindow.cpp +++ b/examples/xml/dombookmarks/mainwindow.cpp @@ -58,13 +58,13 @@ MainWindow::MainWindow() xbelTree = new XbelTree; setCentralWidget(xbelTree); - createActions(); createMenus(); statusBar()->showMessage(tr("Ready")); setWindowTitle(tr("DOM Bookmarks")); - resize(480, 320); + const QSize availableSize = QApplication::desktop()->availableGeometry(this).size(); + resize(availableSize.width() / 2, availableSize.height() / 3); } void MainWindow::open() @@ -80,8 +80,8 @@ void MainWindow::open() if (!file.open(QFile::ReadOnly | QFile::Text)) { QMessageBox::warning(this, tr("SAX Bookmarks"), tr("Cannot read file %1:\n%2.") - .arg(fileName) - .arg(file.errorString())); + .arg(QDir::toNativeSeparators(fileName), + file.errorString())); return; } @@ -102,8 +102,8 @@ void MainWindow::saveAs() if (!file.open(QFile::WriteOnly | QFile::Text)) { QMessageBox::warning(this, tr("SAX Bookmarks"), tr("Cannot write file %1:\n%2.") - .arg(fileName) - .arg(file.errorString())); + .arg(QDir::toNativeSeparators(fileName), + file.errorString())); return; } @@ -119,37 +119,21 @@ void MainWindow::about() "documents.")); } -void MainWindow::createActions() +void MainWindow::createMenus() { - openAct = new QAction(tr("&Open..."), this); + QMenu *fileMenu = menuBar()->addMenu(tr("&File")); + QAction *openAct = fileMenu->addAction(tr("&Open..."), this, &MainWindow::open); openAct->setShortcuts(QKeySequence::Open); - connect(openAct, SIGNAL(triggered()), this, SLOT(open())); - saveAsAct = new QAction(tr("&Save As..."), this); + QAction *saveAsAct = fileMenu->addAction(tr("&Save As..."), this, &MainWindow::saveAs); saveAsAct->setShortcuts(QKeySequence::SaveAs); - connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs())); - exitAct = new QAction(tr("E&xit"), this); + QAction *exitAct = fileMenu->addAction(tr("E&xit"), this, &QWidget::close); exitAct->setShortcuts(QKeySequence::Quit); - connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); - - aboutAct = new QAction(tr("&About"), this); - connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); - - aboutQtAct = new QAction(tr("About &Qt"), this); - connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); -} - -void MainWindow::createMenus() -{ - fileMenu = menuBar()->addMenu(tr("&File")); - fileMenu->addAction(openAct); - fileMenu->addAction(saveAsAct); - fileMenu->addAction(exitAct); menuBar()->addSeparator(); - helpMenu = menuBar()->addMenu(tr("&Help")); - helpMenu->addAction(aboutAct); - helpMenu->addAction(aboutQtAct); + QMenu *helpMenu = menuBar()->addMenu(tr("&Help")); + helpMenu->addAction(tr("&About"), this, &MainWindow::about); + helpMenu->addAction(tr("About &Qt"), qApp, &QCoreApplication::quit); } |