Polish the imageviewer example.

- Remove unneeded member variables.
- Use member initialization in the constructor.
- Use new connection syntax in createActions()
  to assemble the menu there, removing the createMenus()
  function.
- Fix coding style issue (braces)
- Avoid empty label showing up by setting the scroll
  area invisible until an image is loaded.
- Set a new image only if image read succeeds.
- Add status bar with information message.

Task-number: QTBUG-46848
Change-Id: I32d5af70d8eb71ec16dd58a0b98c32eb2bd988d7
Reviewed-by: Topi Reiniö <topi.reinio@digia.com>
This commit is contained in:
Friedemann Kleint 2015-07-09 13:25:16 +02:00
parent b349289fa0
commit 5549ba4fa5
3 changed files with 37 additions and 74 deletions

View File

@ -287,7 +287,8 @@
\snippet widgets/imageviewer/imageviewer.cpp 18 \snippet widgets/imageviewer/imageviewer.cpp 18
In the private \c createAction() function, we create the In the private \c createAction() function, we create the
actions providing the application features. actions providing the application features and populate
a menu with them.
We assign a short-cut key to each action and connect them to the We assign a short-cut key to each action and connect them to the
appropriate slots. We only enable the \c openAct and \c exitAct at appropriate slots. We only enable the \c openAct and \c exitAct at
@ -295,16 +296,10 @@
been loaded into the application. In addition we make the \c been loaded into the application. In addition we make the \c
fitToWindowAct \l {QAction::checkable}{checkable}. fitToWindowAct \l {QAction::checkable}{checkable}.
\snippet widgets/imageviewer/imageviewer.cpp 19
\snippet widgets/imageviewer/imageviewer.cpp 20
In the private \c createMenu() function, we add the previously
created actions to the \uicontrol File, \uicontrol View and \uicontrol Help menus.
The QMenu class provides a menu widget for use in menu bars, The QMenu class provides a menu widget for use in menu bars,
context menus, and other popup menus. The QMenuBar class provides context menus, and other popup menus. The QMenuBar class provides
a horizontal menu bar that consists of a list of pull-down menu a horizontal menu bar that consists of a list of pull-down menu
items. So at the end we put the menus in the \c {ImageViewer}'s items. So we put the menus in the \c {ImageViewer}'s
menu bar which we retrieve with the QMainWindow::menuBar() menu bar which we retrieve with the QMainWindow::menuBar()
function. function.

View File

@ -47,19 +47,20 @@
//! [0] //! [0]
ImageViewer::ImageViewer() ImageViewer::ImageViewer()
: imageLabel(new QLabel)
, scrollArea(new QScrollArea)
, scaleFactor(1)
{ {
imageLabel = new QLabel;
imageLabel->setBackgroundRole(QPalette::Base); imageLabel->setBackgroundRole(QPalette::Base);
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
imageLabel->setScaledContents(true); imageLabel->setScaledContents(true);
scrollArea = new QScrollArea;
scrollArea->setBackgroundRole(QPalette::Dark); scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setWidget(imageLabel); scrollArea->setWidget(imageLabel);
scrollArea->setVisible(false);
setCentralWidget(scrollArea); setCentralWidget(scrollArea);
createActions(); createActions();
createMenus();
resize(QGuiApplication::primaryScreen()->availableSize() * 3 / 5); resize(QGuiApplication::primaryScreen()->availableSize() * 3 / 5);
} }
@ -74,10 +75,8 @@ bool ImageViewer::loadFile(const QString &fileName)
const QImage image = reader.read(); const QImage image = reader.read();
if (image.isNull()) { if (image.isNull()) {
QMessageBox::information(this, QGuiApplication::applicationDisplayName(), QMessageBox::information(this, QGuiApplication::applicationDisplayName(),
tr("Cannot load %1.").arg(QDir::toNativeSeparators(fileName))); tr("Cannot load %1: %2")
setWindowFilePath(QString()); .arg(QDir::toNativeSeparators(fileName)), reader.errorString());
imageLabel->setPixmap(QPixmap());
imageLabel->adjustSize();
return false; return false;
} }
//! [2] //! [3] //! [2] //! [3]
@ -85,6 +84,7 @@ bool ImageViewer::loadFile(const QString &fileName)
//! [3] //! [4] //! [3] //! [4]
scaleFactor = 1.0; scaleFactor = 1.0;
scrollArea->setVisible(true);
printAct->setEnabled(true); printAct->setEnabled(true);
fitToWindowAct->setEnabled(true); fitToWindowAct->setEnabled(true);
updateActions(); updateActions();
@ -93,6 +93,10 @@ bool ImageViewer::loadFile(const QString &fileName)
imageLabel->adjustSize(); imageLabel->adjustSize();
setWindowFilePath(fileName); setWindowFilePath(fileName);
const QString message = tr("Opened \"%1\", %2x%3, Depth: %4")
.arg(QDir::toNativeSeparators(fileName)).arg(image.width()).arg(image.height()).arg(image.depth());
statusBar()->showMessage(message);
return true; return true;
} }
@ -167,9 +171,8 @@ void ImageViewer::fitToWindow()
{ {
bool fitToWindow = fitToWindowAct->isChecked(); bool fitToWindow = fitToWindowAct->isChecked();
scrollArea->setWidgetResizable(fitToWindow); scrollArea->setWidgetResizable(fitToWindow);
if (!fitToWindow) { if (!fitToWindow)
normalSize(); normalSize();
}
updateActions(); updateActions();
} }
//! [14] //! [14]
@ -199,75 +202,48 @@ void ImageViewer::about()
void ImageViewer::createActions() void ImageViewer::createActions()
//! [17] //! [18] //! [17] //! [18]
{ {
openAct = new QAction(tr("&Open..."), this); QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
openAct->setShortcut(tr("Ctrl+O"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
printAct = new QAction(tr("&Print..."), this); QAction *openAct = fileMenu->addAction(tr("&Open..."), this, &ImageViewer::open);
printAct->setShortcut(tr("Ctrl+P")); openAct->setShortcut(QKeySequence::Open);
printAct = fileMenu->addAction(tr("&Print..."), this, &ImageViewer::print);
printAct->setShortcut(QKeySequence::Print);
printAct->setEnabled(false); printAct->setEnabled(false);
connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
exitAct = new QAction(tr("E&xit"), this); fileMenu->addSeparator();
QAction *exitAct = fileMenu->addAction(tr("E&xit"), this, &QWidget::close);
exitAct->setShortcut(tr("Ctrl+Q")); exitAct->setShortcut(tr("Ctrl+Q"));
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
zoomInAct = new QAction(tr("Zoom &In (25%)"), this); QMenu *viewMenu = menuBar()->addMenu(tr("&View"));
zoomInAct->setShortcut(tr("Ctrl++"));
zoomInAct = viewMenu->addAction(tr("Zoom &In (25%)"), this, &ImageViewer::zoomIn);
zoomInAct->setShortcut(QKeySequence::ZoomIn);
zoomInAct->setEnabled(false); zoomInAct->setEnabled(false);
connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn()));
zoomOutAct = new QAction(tr("Zoom &Out (25%)"), this); zoomOutAct = viewMenu->addAction(tr("Zoom &Out (25%)"), this, &ImageViewer::zoomOut);
zoomOutAct->setShortcut(tr("Ctrl+-")); zoomOutAct->setShortcut(QKeySequence::ZoomOut);
zoomOutAct->setEnabled(false); zoomOutAct->setEnabled(false);
connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut()));
normalSizeAct = new QAction(tr("&Normal Size"), this); normalSizeAct = viewMenu->addAction(tr("&Normal Size"), this, &ImageViewer::normalSize);
normalSizeAct->setShortcut(tr("Ctrl+S")); normalSizeAct->setShortcut(tr("Ctrl+S"));
normalSizeAct->setEnabled(false); normalSizeAct->setEnabled(false);
connect(normalSizeAct, SIGNAL(triggered()), this, SLOT(normalSize()));
fitToWindowAct = new QAction(tr("&Fit to Window"), this); viewMenu->addSeparator();
fitToWindowAct = viewMenu->addAction(tr("&Fit to Window"), this, &ImageViewer::fitToWindow);
fitToWindowAct->setEnabled(false); fitToWindowAct->setEnabled(false);
fitToWindowAct->setCheckable(true); fitToWindowAct->setCheckable(true);
fitToWindowAct->setShortcut(tr("Ctrl+F")); fitToWindowAct->setShortcut(tr("Ctrl+F"));
connect(fitToWindowAct, SIGNAL(triggered()), this, SLOT(fitToWindow()));
aboutAct = new QAction(tr("&About"), this); QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
aboutQtAct = new QAction(tr("About &Qt"), this); helpMenu->addAction(tr("&About"), this, &ImageViewer::about);
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); helpMenu->addAction(tr("About &Qt"), &QApplication::aboutQt);
} }
//! [18] //! [18]
//! [19]
void ImageViewer::createMenus()
//! [19] //! [20]
{
fileMenu = new QMenu(tr("&File"), this);
fileMenu->addAction(openAct);
fileMenu->addAction(printAct);
fileMenu->addSeparator();
fileMenu->addAction(exitAct);
viewMenu = new QMenu(tr("&View"), this);
viewMenu->addAction(zoomInAct);
viewMenu->addAction(zoomOutAct);
viewMenu->addAction(normalSizeAct);
viewMenu->addSeparator();
viewMenu->addAction(fitToWindowAct);
helpMenu = new QMenu(tr("&Help"), this);
helpMenu->addAction(aboutAct);
helpMenu->addAction(aboutQtAct);
menuBar()->addMenu(fileMenu);
menuBar()->addMenu(viewMenu);
menuBar()->addMenu(helpMenu);
}
//! [20]
//! [21] //! [21]
void ImageViewer::updateActions() void ImageViewer::updateActions()
//! [21] //! [22] //! [21] //! [22]

View File

@ -87,19 +87,11 @@ private:
QPrinter printer; QPrinter printer;
#endif #endif
QAction *openAct;
QAction *printAct; QAction *printAct;
QAction *exitAct;
QAction *zoomInAct; QAction *zoomInAct;
QAction *zoomOutAct; QAction *zoomOutAct;
QAction *normalSizeAct; QAction *normalSizeAct;
QAction *fitToWindowAct; QAction *fitToWindowAct;
QAction *aboutAct;
QAction *aboutQtAct;
QMenu *fileMenu;
QMenu *viewMenu;
QMenu *helpMenu;
}; };
//! [0] //! [0]