From c8cd9f1b9a3d338a12bdccce957d74d515eb9c32 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 7 Oct 2015 12:03:05 +0200 Subject: Polish DnD Examples. - Remove class DragLabel in draggabletext and add a static creation function instead since it only has a constructor setting some properties. - Use QRegularExpression instead of QRegExp - Use new connection syntax. - Ensure compilation with DEFINES += QT_NO_CAST_TO_ASCII QT_NO_CAST_FROM_ASCII demonstrating use of QLatin1String vs QStringLiteral. - Streamline code. Change-Id: I2e2ddeb40837dba379990836c77fb72ad7263e2d Reviewed-by: Oliver Wolff --- examples/widgets/draganddrop/puzzle/mainwindow.cpp | 47 ++++++++++------------ 1 file changed, 22 insertions(+), 25 deletions(-) (limited to 'examples/widgets/draganddrop/puzzle/mainwindow.cpp') diff --git a/examples/widgets/draganddrop/puzzle/mainwindow.cpp b/examples/widgets/draganddrop/puzzle/mainwindow.cpp index 0fbdfc3f8d..48e79946e7 100644 --- a/examples/widgets/draganddrop/puzzle/mainwindow.cpp +++ b/examples/widgets/draganddrop/puzzle/mainwindow.cpp @@ -55,26 +55,27 @@ MainWindow::MainWindow(QWidget *parent) setWindowTitle(tr("Puzzle")); } -void MainWindow::openImage(const QString &path) +void MainWindow::openImage() { - QString fileName = path; + const QString fileName = + QFileDialog::getOpenFileName(this, tr("Open Image"), QString(), + tr("Image Files (*.png *.jpg *.bmp)")); - if (fileName.isNull()) { - fileName = QFileDialog::getOpenFileName(this, - tr("Open Image"), "", "Image Files (*.png *.jpg *.bmp)"); - } + if (!fileName.isEmpty()) + loadImage(fileName); +} - if (!fileName.isEmpty()) { - QPixmap newImage; - if (!newImage.load(fileName)) { - QMessageBox::warning(this, tr("Open Image"), - tr("The image file could not be loaded."), - QMessageBox::Cancel); - return; - } - puzzleImage = newImage; - setupPuzzle(); +void MainWindow::loadImage(const QString &fileName) +{ + QPixmap newImage; + if (!newImage.load(fileName)) { + QMessageBox::warning(this, tr("Open Image"), + tr("The image file could not be loaded."), + QMessageBox::Close); + return; } + puzzleImage = newImage; + setupPuzzle(); } void MainWindow::setCompleted() @@ -120,19 +121,15 @@ void MainWindow::setupMenus() { QMenu *fileMenu = menuBar()->addMenu(tr("&File")); - QAction *openAction = fileMenu->addAction(tr("&Open...")); + QAction *openAction = fileMenu->addAction(tr("&Open..."), this, &MainWindow::openImage); openAction->setShortcuts(QKeySequence::Open); - QAction *exitAction = fileMenu->addAction(tr("E&xit")); + QAction *exitAction = fileMenu->addAction(tr("E&xit"), qApp, &QCoreApplication::quit); exitAction->setShortcuts(QKeySequence::Quit); QMenu *gameMenu = menuBar()->addMenu(tr("&Game")); - QAction *restartAction = gameMenu->addAction(tr("&Restart")); - - connect(openAction, SIGNAL(triggered()), this, SLOT(openImage())); - connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit())); - connect(restartAction, SIGNAL(triggered()), this, SLOT(setupPuzzle())); + gameMenu->addAction(tr("&Restart"), this, &MainWindow::setupPuzzle); } void MainWindow::setupWidgets() @@ -144,8 +141,8 @@ void MainWindow::setupWidgets() piecesList = new PiecesList(puzzleWidget->pieceSize(), this); - connect(puzzleWidget, SIGNAL(puzzleCompleted()), - this, SLOT(setCompleted()), Qt::QueuedConnection); + connect(puzzleWidget, &PuzzleWidget::puzzleCompleted, + this, &MainWindow::setCompleted, Qt::QueuedConnection); frameLayout->addWidget(piecesList); frameLayout->addWidget(puzzleWidget); -- cgit v1.2.3