From bdbd26a3b73b718b54eb9ffb2ebf687924ed6e6d Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 15 Jul 2015 13:12:24 +0200 Subject: Polish the widgets/mainwindows/mdi example. - Introduce Qt 5 signals & slot syntax and remove the QSignalMapper used to map the triggered() signals of the per-MDI-child actions of the window menu to the activation slot by a functor to demonstrate the flexibility of the new connection syntax (the functor can in turn be replaced by a lambda expression once we have C++ 11). - Merge MainWindow::createMenus()/createQToolBars() into MainWindow::createActions(), removing the need to store the actions as member variables. Use QMenu::addAction() for brevity. - Use QIcon::fromTheme() to obtain system icons and use resource icons as fallback. - Rewrite settings code to use QWidget::saveGeometry(), Widget::restoreGeometry() since saving size and position does not work well with multiple screens. Query the available size when determining the initial size instead of using hard-coded values for High-DPI screens. - Fix minor issues in code, use multi-argument version of QString::arg(), QDir::toNativeSeparators() to present file paths to the user and static method invocation. Change-Id: I3d5078ddbe3cb4eba65e188430ba3580cecb2c79 Reviewed-by: Olivier Goffart (Woboq GmbH) --- examples/widgets/mainwindows/mdi/mainwindow.cpp | 215 +++++++++++------------- examples/widgets/mainwindows/mdi/mainwindow.h | 20 +-- examples/widgets/mainwindows/mdi/mdichild.cpp | 39 +++-- 3 files changed, 125 insertions(+), 149 deletions(-) (limited to 'examples/widgets/mainwindows') diff --git a/examples/widgets/mainwindows/mdi/mainwindow.cpp b/examples/widgets/mainwindows/mdi/mainwindow.cpp index 83ffbf6557..4dada5ce70 100644 --- a/examples/widgets/mainwindows/mdi/mainwindow.cpp +++ b/examples/widgets/mainwindows/mdi/mainwindow.cpp @@ -44,20 +44,15 @@ #include "mdichild.h" MainWindow::MainWindow() + : mdiArea(new QMdiArea) { - mdiArea = new QMdiArea; mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); setCentralWidget(mdiArea); - connect(mdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)), - this, SLOT(updateMenus())); - windowMapper = new QSignalMapper(this); - connect(windowMapper, SIGNAL(mapped(QWidget*)), - this, SLOT(setActiveSubWindow(QWidget*))); + connect(mdiArea, &QMdiArea::subWindowActivated, + this, &MainWindow::updateMenus); createActions(); - createMenus(); - createToolBars(); createStatusBar(); updateMenus(); @@ -89,8 +84,7 @@ void MainWindow::open() { QString fileName = QFileDialog::getOpenFileName(this); if (!fileName.isEmpty()) { - QMdiSubWindow *existing = findMdiChild(fileName); - if (existing) { + if (QMdiSubWindow *existing = findMdiChild(fileName)) { mdiArea->setActiveSubWindow(existing); return; } @@ -164,7 +158,7 @@ void MainWindow::updateMenus() cascadeAct->setEnabled(hasMdiChild); nextAct->setEnabled(hasMdiChild); previousAct->setEnabled(hasMdiChild); - separatorAct->setVisible(hasMdiChild); + windowMenuSeparatorAct->setVisible(hasMdiChild); #ifndef QT_NO_CLIPBOARD bool hasSelection = (activeMdiChild() && @@ -174,6 +168,16 @@ void MainWindow::updateMenus() #endif } +class ActiveMdiSubWindowFunctor { +public: + explicit ActiveMdiSubWindowFunctor(QMdiArea *mdiArea, QMdiSubWindow *activeWindow) : m_mdiArea(mdiArea), m_activeWindow(activeWindow) {} + void operator()() const { m_mdiArea->setActiveSubWindow(m_activeWindow); } + +private: + QMdiArea *m_mdiArea; + QMdiSubWindow *m_activeWindow; +}; + void MainWindow::updateWindowMenu() { windowMenu->clear(); @@ -185,13 +189,14 @@ void MainWindow::updateWindowMenu() windowMenu->addSeparator(); windowMenu->addAction(nextAct); windowMenu->addAction(previousAct); - windowMenu->addAction(separatorAct); + windowMenu->addAction(windowMenuSeparatorAct); QList windows = mdiArea->subWindowList(); - separatorAct->setVisible(!windows.isEmpty()); + windowMenuSeparatorAct->setVisible(!windows.isEmpty()); for (int i = 0; i < windows.size(); ++i) { - MdiChild *child = qobject_cast(windows.at(i)->widget()); + QMdiSubWindow *mdiSubWindow = windows.at(i); + MdiChild *child = qobject_cast(mdiSubWindow->widget()); QString text; if (i < 9) { @@ -201,11 +206,9 @@ void MainWindow::updateWindowMenu() text = tr("%1 %2").arg(i + 1) .arg(child->userFriendlyCurrentFile()); } - QAction *action = windowMenu->addAction(text); + QAction *action = windowMenu->addAction(text, mdiSubWindow, ActiveMdiSubWindowFunctor(mdiArea, mdiSubWindow)); action->setCheckable(true); action ->setChecked(child == activeMdiChild()); - connect(action, SIGNAL(triggered()), windowMapper, SLOT(map())); - windowMapper->setMapping(action, windows.at(i)); } } @@ -215,10 +218,8 @@ MdiChild *MainWindow::createMdiChild() mdiArea->addSubWindow(child); #ifndef QT_NO_CLIPBOARD - connect(child, SIGNAL(copyAvailable(bool)), - cutAct, SLOT(setEnabled(bool))); - connect(child, SIGNAL(copyAvailable(bool)), - copyAct, SLOT(setEnabled(bool))); + connect(child, &QTextEdit::copyAvailable, cutAct, &QAction::setEnabled); + connect(child, &QTextEdit::copyAvailable, copyAct, &QAction::setEnabled); #endif return child; @@ -226,139 +227,128 @@ MdiChild *MainWindow::createMdiChild() void MainWindow::createActions() { - newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this); + QMenu *fileMenu = menuBar()->addMenu(tr("&File")); + QToolBar *fileToolBar = addToolBar(tr("File")); + + const QIcon newIcon = QIcon::fromTheme("document-new", QIcon(":/images/new.png")); + newAct = new QAction(newIcon, tr("&New"), this); newAct->setShortcuts(QKeySequence::New); newAct->setStatusTip(tr("Create a new file")); - connect(newAct, SIGNAL(triggered()), this, SLOT(newFile())); + connect(newAct, &QAction::triggered, this, &MainWindow::newFile); + fileMenu->addAction(newAct); + fileToolBar->addAction(newAct); - openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this); + const QIcon openIcon = QIcon::fromTheme("document-open", QIcon(":/images/open.png")); + QAction *openAct = new QAction(openIcon, tr("&Open..."), this); openAct->setShortcuts(QKeySequence::Open); openAct->setStatusTip(tr("Open an existing file")); - connect(openAct, SIGNAL(triggered()), this, SLOT(open())); + connect(openAct, &QAction::triggered, this, &MainWindow::open); + fileMenu->addAction(openAct); + fileToolBar->addAction(openAct); - saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this); + const QIcon saveIcon = QIcon::fromTheme("document-save", QIcon(":/images/save.png")); + saveAct = new QAction(saveIcon, tr("&Save"), this); saveAct->setShortcuts(QKeySequence::Save); saveAct->setStatusTip(tr("Save the document to disk")); - connect(saveAct, SIGNAL(triggered()), this, SLOT(save())); + connect(saveAct, &QAction::triggered, this, &MainWindow::save); + fileToolBar->addAction(saveAct); - saveAsAct = new QAction(tr("Save &As..."), this); + const QIcon saveAsIcon = QIcon::fromTheme("document-save-as"); + saveAsAct = new QAction(saveAsIcon, tr("Save &As..."), this); saveAsAct->setShortcuts(QKeySequence::SaveAs); saveAsAct->setStatusTip(tr("Save the document under a new name")); - connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs())); + connect(saveAsAct, &QAction::triggered, this, &MainWindow::saveAs); + fileMenu->addAction(saveAsAct); + + fileMenu->addSeparator(); + + fileMenu->addAction(tr("Switch layout direction"), this, &MainWindow::switchLayoutDirection); //! [0] - exitAct = new QAction(tr("E&xit"), this); + const QIcon exitIcon = QIcon::fromTheme("application-exit"); + QAction *exitAct = fileMenu->addAction(exitIcon, tr("E&xit"), qApp, &QApplication::closeAllWindows); exitAct->setShortcuts(QKeySequence::Quit); exitAct->setStatusTip(tr("Exit the application")); - connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows())); + fileMenu->addAction(exitAct); //! [0] #ifndef QT_NO_CLIPBOARD - cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this); + QMenu *editMenu = menuBar()->addMenu(tr("&Edit")); + QToolBar *editToolBar = addToolBar(tr("Edit")); + + const QIcon cutIcon = QIcon::fromTheme("edit-cut", QIcon(":/images/cut.png")); + cutAct = new QAction(cutIcon, tr("Cu&t"), this); cutAct->setShortcuts(QKeySequence::Cut); cutAct->setStatusTip(tr("Cut the current selection's contents to the " "clipboard")); - connect(cutAct, SIGNAL(triggered()), this, SLOT(cut())); + connect(cutAct, &QAction::triggered, this, &MainWindow::cut); + editMenu->addAction(cutAct); + editToolBar->addAction(cutAct); - copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this); + const QIcon copyIcon = QIcon::fromTheme("edit-copy", QIcon(":/images/copy.png")); + copyAct = new QAction(copyIcon, tr("&Copy"), this); copyAct->setShortcuts(QKeySequence::Copy); copyAct->setStatusTip(tr("Copy the current selection's contents to the " "clipboard")); - connect(copyAct, SIGNAL(triggered()), this, SLOT(copy())); + connect(copyAct, &QAction::triggered, this, &MainWindow::copy); + editMenu->addAction(copyAct); + editToolBar->addAction(copyAct); - pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this); + const QIcon pasteIcon = QIcon::fromTheme("edit-paste", QIcon(":/images/paste.png")); + pasteAct = new QAction(pasteIcon, tr("&Paste"), this); pasteAct->setShortcuts(QKeySequence::Paste); pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current " "selection")); - connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste())); + connect(pasteAct, &QAction::triggered, this, &MainWindow::paste); + editMenu->addAction(pasteAct); + editToolBar->addAction(pasteAct); #endif + windowMenu = menuBar()->addMenu(tr("&Window")); + connect(windowMenu, &QMenu::aboutToShow, this, &MainWindow::updateWindowMenu); + closeAct = new QAction(tr("Cl&ose"), this); closeAct->setStatusTip(tr("Close the active window")); - connect(closeAct, SIGNAL(triggered()), - mdiArea, SLOT(closeActiveSubWindow())); + connect(closeAct, &QAction::triggered, + mdiArea, &QMdiArea::closeActiveSubWindow); closeAllAct = new QAction(tr("Close &All"), this); closeAllAct->setStatusTip(tr("Close all the windows")); - connect(closeAllAct, SIGNAL(triggered()), - mdiArea, SLOT(closeAllSubWindows())); + connect(closeAllAct, &QAction::triggered, mdiArea, &QMdiArea::closeAllSubWindows); tileAct = new QAction(tr("&Tile"), this); tileAct->setStatusTip(tr("Tile the windows")); - connect(tileAct, SIGNAL(triggered()), mdiArea, SLOT(tileSubWindows())); + connect(tileAct, &QAction::triggered, mdiArea, &QMdiArea::tileSubWindows); cascadeAct = new QAction(tr("&Cascade"), this); cascadeAct->setStatusTip(tr("Cascade the windows")); - connect(cascadeAct, SIGNAL(triggered()), mdiArea, SLOT(cascadeSubWindows())); + connect(cascadeAct, &QAction::triggered, mdiArea, &QMdiArea::cascadeSubWindows); nextAct = new QAction(tr("Ne&xt"), this); nextAct->setShortcuts(QKeySequence::NextChild); nextAct->setStatusTip(tr("Move the focus to the next window")); - connect(nextAct, SIGNAL(triggered()), - mdiArea, SLOT(activateNextSubWindow())); + connect(nextAct, &QAction::triggered, mdiArea, &QMdiArea::activateNextSubWindow); previousAct = new QAction(tr("Pre&vious"), this); previousAct->setShortcuts(QKeySequence::PreviousChild); previousAct->setStatusTip(tr("Move the focus to the previous " "window")); - connect(previousAct, SIGNAL(triggered()), - mdiArea, SLOT(activatePreviousSubWindow())); - - separatorAct = new QAction(this); - separatorAct->setSeparator(true); - - aboutAct = new QAction(tr("&About"), this); - aboutAct->setStatusTip(tr("Show the application's About box")); - connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); - - aboutQtAct = new QAction(tr("About &Qt"), this); - aboutQtAct->setStatusTip(tr("Show the Qt library's About box")); - connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); -} - -void MainWindow::createMenus() -{ - fileMenu = menuBar()->addMenu(tr("&File")); - fileMenu->addAction(newAct); - fileMenu->addAction(openAct); - fileMenu->addAction(saveAct); - fileMenu->addAction(saveAsAct); - fileMenu->addSeparator(); - QAction *action = fileMenu->addAction(tr("Switch layout direction")); - connect(action, SIGNAL(triggered()), this, SLOT(switchLayoutDirection())); - fileMenu->addAction(exitAct); + connect(previousAct, &QAction::triggered, mdiArea, &QMdiArea::activatePreviousSubWindow); - editMenu = menuBar()->addMenu(tr("&Edit")); -#ifndef QT_NO_CLIPBOARD - editMenu->addAction(cutAct); - editMenu->addAction(copyAct); - editMenu->addAction(pasteAct); -#endif + windowMenuSeparatorAct = new QAction(this); + windowMenuSeparatorAct->setSeparator(true); - windowMenu = menuBar()->addMenu(tr("&Window")); updateWindowMenu(); - connect(windowMenu, SIGNAL(aboutToShow()), this, SLOT(updateWindowMenu())); menuBar()->addSeparator(); - helpMenu = menuBar()->addMenu(tr("&Help")); - helpMenu->addAction(aboutAct); - helpMenu->addAction(aboutQtAct); -} + QMenu *helpMenu = menuBar()->addMenu(tr("&Help")); -void MainWindow::createToolBars() -{ - fileToolBar = addToolBar(tr("File")); - fileToolBar->addAction(newAct); - fileToolBar->addAction(openAct); - fileToolBar->addAction(saveAct); + QAction *aboutAct = helpMenu->addAction(tr("&About"), this, &MainWindow::about); + aboutAct->setStatusTip(tr("Show the application's About box")); -#ifndef QT_NO_CLIPBOARD - editToolBar = addToolBar(tr("Edit")); - editToolBar->addAction(cutAct); - editToolBar->addAction(copyAct); - editToolBar->addAction(pasteAct); -#endif + QAction *aboutQtAct = helpMenu->addAction(tr("About &Qt"), qApp, &QApplication::aboutQt); + aboutQtAct->setStatusTip(tr("Show the Qt library's About box")); } void MainWindow::createStatusBar() @@ -368,28 +358,32 @@ void MainWindow::createStatusBar() void MainWindow::readSettings() { - QSettings settings("QtProject", "MDI Example"); - QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint(); - QSize size = settings.value("size", QSize(400, 400)).toSize(); - move(pos); - resize(size); + QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName()); + const QByteArray geometry = settings.value("geometry", QByteArray()).toByteArray(); + if (geometry.isEmpty()) { + const QRect availableGeometry = QApplication::desktop()->availableGeometry(this); + resize(availableGeometry.width() / 3, availableGeometry.height() / 2); + move((availableGeometry.width() - width()) / 2, + (availableGeometry.height() - height()) / 2); + } else { + restoreGeometry(geometry); + } } void MainWindow::writeSettings() { - QSettings settings("QtProject", "MDI Example"); - settings.setValue("pos", pos()); - settings.setValue("size", size()); + QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName()); + settings.setValue("geometry", saveGeometry()); } -MdiChild *MainWindow::activeMdiChild() +MdiChild *MainWindow::activeMdiChild() const { if (QMdiSubWindow *activeSubWindow = mdiArea->activeSubWindow()) return qobject_cast(activeSubWindow->widget()); return 0; } -QMdiSubWindow *MainWindow::findMdiChild(const QString &fileName) +QMdiSubWindow *MainWindow::findMdiChild(const QString &fileName) const { QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath(); @@ -404,14 +398,7 @@ QMdiSubWindow *MainWindow::findMdiChild(const QString &fileName) void MainWindow::switchLayoutDirection() { if (layoutDirection() == Qt::LeftToRight) - qApp->setLayoutDirection(Qt::RightToLeft); + QGuiApplication::setLayoutDirection(Qt::RightToLeft); else - qApp->setLayoutDirection(Qt::LeftToRight); -} - -void MainWindow::setActiveSubWindow(QWidget *window) -{ - if (!window) - return; - mdiArea->setActiveSubWindow(qobject_cast(window)); + QGuiApplication::setLayoutDirection(Qt::LeftToRight); } diff --git a/examples/widgets/mainwindows/mdi/mainwindow.h b/examples/widgets/mainwindows/mdi/mainwindow.h index fa8e0131e9..bdc2a6a09d 100644 --- a/examples/widgets/mainwindows/mdi/mainwindow.h +++ b/examples/widgets/mainwindows/mdi/mainwindow.h @@ -49,7 +49,6 @@ class QAction; class QMenu; class QMdiArea; class QMdiSubWindow; -class QSignalMapper; QT_END_NAMESPACE class MainWindow : public QMainWindow @@ -79,32 +78,21 @@ private slots: void updateWindowMenu(); MdiChild *createMdiChild(); void switchLayoutDirection(); - void setActiveSubWindow(QWidget *window); private: void createActions(); - void createMenus(); - void createToolBars(); void createStatusBar(); void readSettings(); void writeSettings(); - MdiChild *activeMdiChild(); - QMdiSubWindow *findMdiChild(const QString &fileName); + MdiChild *activeMdiChild() const; + QMdiSubWindow *findMdiChild(const QString &fileName) const; QMdiArea *mdiArea; - QSignalMapper *windowMapper; - QMenu *fileMenu; - QMenu *editMenu; QMenu *windowMenu; - QMenu *helpMenu; - QToolBar *fileToolBar; - QToolBar *editToolBar; QAction *newAct; - QAction *openAct; QAction *saveAct; QAction *saveAsAct; - QAction *exitAct; #ifndef QT_NO_CLIPBOARD QAction *cutAct; QAction *copyAct; @@ -116,9 +104,7 @@ private: QAction *cascadeAct; QAction *nextAct; QAction *previousAct; - QAction *separatorAct; - QAction *aboutAct; - QAction *aboutQtAct; + QAction *windowMenuSeparatorAct; }; #endif diff --git a/examples/widgets/mainwindows/mdi/mdichild.cpp b/examples/widgets/mainwindows/mdi/mdichild.cpp index 242e8248a2..73364eb3ee 100644 --- a/examples/widgets/mainwindows/mdi/mdichild.cpp +++ b/examples/widgets/mainwindows/mdi/mdichild.cpp @@ -56,8 +56,8 @@ void MdiChild::newFile() curFile = tr("document%1.txt").arg(sequenceNumber++); setWindowTitle(curFile + "[*]"); - connect(document(), SIGNAL(contentsChanged()), - this, SLOT(documentWasModified())); + connect(document(), &QTextDocument::contentsChanged, + this, &MdiChild::documentWasModified); } bool MdiChild::loadFile(const QString &fileName) @@ -78,8 +78,8 @@ bool MdiChild::loadFile(const QString &fileName) setCurrentFile(fileName); - connect(document(), SIGNAL(contentsChanged()), - this, SLOT(documentWasModified())); + connect(document(), &QTextDocument::contentsChanged, + this, &MdiChild::documentWasModified); return true; } @@ -109,8 +109,7 @@ bool MdiChild::saveFile(const QString &fileName) if (!file.open(QFile::WriteOnly | QFile::Text)) { QMessageBox::warning(this, tr("MDI"), tr("Cannot write file %1:\n%2.") - .arg(fileName) - .arg(file.errorString())); + .arg(QDir::toNativeSeparators(fileName), file.errorString())); return false; } @@ -144,18 +143,22 @@ void MdiChild::documentWasModified() bool MdiChild::maybeSave() { - if (document()->isModified()) { - QMessageBox::StandardButton ret; - ret = QMessageBox::warning(this, tr("MDI"), - tr("'%1' has been modified.\n" - "Do you want to save your changes?") - .arg(userFriendlyCurrentFile()), - QMessageBox::Save | QMessageBox::Discard - | QMessageBox::Cancel); - if (ret == QMessageBox::Save) - return save(); - else if (ret == QMessageBox::Cancel) - return false; + if (!document()->isModified()) + return true; + const QMessageBox::StandardButton ret + = QMessageBox::warning(this, tr("MDI"), + tr("'%1' has been modified.\n" + "Do you want to save your changes?") + .arg(userFriendlyCurrentFile()), + QMessageBox::Save | QMessageBox::Discard + | QMessageBox::Cancel); + switch (ret) { + case QMessageBox::Save: + return save(); + case QMessageBox::Cancel: + return false; + default: + break; } return true; } -- cgit v1.2.3