diff options
author | Friedemann Kleint <Friedemann.Kleint@digia.com> | 2015-10-07 12:03:05 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@theqtcompany.com> | 2015-10-09 15:59:21 +0000 |
commit | c8cd9f1b9a3d338a12bdccce957d74d515eb9c32 (patch) | |
tree | fca6b64875083ce22716c1ed3c6633459bfb93c2 /examples/widgets/draganddrop/puzzle/mainwindow.cpp | |
parent | c55a36cb9015cf1eebd49eaa5b1b4f4ec9b28451 (diff) |
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 <oliver.wolff@theqtcompany.com>
Diffstat (limited to 'examples/widgets/draganddrop/puzzle/mainwindow.cpp')
-rw-r--r-- | examples/widgets/draganddrop/puzzle/mainwindow.cpp | 47 |
1 files changed, 22 insertions, 25 deletions
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); |