diff options
Diffstat (limited to 'examples/widgets/desktop')
-rw-r--r-- | examples/widgets/desktop/screenshot/main.cpp | 3 | ||||
-rw-r--r-- | examples/widgets/desktop/screenshot/screenshot.cpp | 139 | ||||
-rw-r--r-- | examples/widgets/desktop/screenshot/screenshot.h | 11 | ||||
-rw-r--r-- | examples/widgets/desktop/systray/window.cpp | 25 |
4 files changed, 84 insertions, 94 deletions
diff --git a/examples/widgets/desktop/screenshot/main.cpp b/examples/widgets/desktop/screenshot/main.cpp index 788ebb3532..811562858a 100644 --- a/examples/widgets/desktop/screenshot/main.cpp +++ b/examples/widgets/desktop/screenshot/main.cpp @@ -39,13 +39,16 @@ ****************************************************************************/ #include <QApplication> +#include <QDesktopWidget> #include "screenshot.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); + Screenshot screenshot; + screenshot.move(QApplication::desktop()->availableGeometry(&screenshot).topLeft() + QPoint(20, 20)); screenshot.show(); return app.exec(); } diff --git a/examples/widgets/desktop/screenshot/screenshot.cpp b/examples/widgets/desktop/screenshot/screenshot.cpp index bfca5a45ae..354fe36369 100644 --- a/examples/widgets/desktop/screenshot/screenshot.cpp +++ b/examples/widgets/desktop/screenshot/screenshot.cpp @@ -44,20 +44,48 @@ //! [0] Screenshot::Screenshot() + : screenshotLabel(new QLabel(this)) { - screenshotLabel = new QLabel; screenshotLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); screenshotLabel->setAlignment(Qt::AlignCenter); - screenshotLabel->setMinimumSize(240, 160); - createOptionsGroupBox(); - createButtonsLayout(); + const QRect screenGeometry = QApplication::desktop()->screenGeometry(this); + screenshotLabel->setMinimumSize(screenGeometry.width() / 8, screenGeometry.height() / 8); - mainLayout = new QVBoxLayout; + QVBoxLayout *mainLayout = new QVBoxLayout(this); mainLayout->addWidget(screenshotLabel); + + QGroupBox *optionsGroupBox = new QGroupBox(tr("Options"), this); + delaySpinBox = new QSpinBox(optionsGroupBox); + delaySpinBox->setSuffix(tr(" s")); + delaySpinBox->setMaximum(60); + + typedef void (QSpinBox::*QSpinBoxIntSignal)(int); + connect(delaySpinBox, static_cast<QSpinBoxIntSignal>(&QSpinBox::valueChanged), + this, &Screenshot::updateCheckBox); + + hideThisWindowCheckBox = new QCheckBox(tr("Hide This Window"), optionsGroupBox); + + QGridLayout *optionsGroupBoxLayout = new QGridLayout(optionsGroupBox); + optionsGroupBoxLayout->addWidget(new QLabel(tr("Screenshot Delay:"), this), 0, 0); + optionsGroupBoxLayout->addWidget(delaySpinBox, 0, 1); + optionsGroupBoxLayout->addWidget(hideThisWindowCheckBox, 1, 0, 1, 2); + mainLayout->addWidget(optionsGroupBox); + + QHBoxLayout *buttonsLayout = new QHBoxLayout; + newScreenshotButton = new QPushButton(tr("New Screenshot"), this); + connect(newScreenshotButton, &QPushButton::clicked, this, &Screenshot::newScreenshot); + buttonsLayout->addWidget(newScreenshotButton); + QPushButton *saveScreenshotButton = new QPushButton(tr("Save Screenshot"), this); + connect(saveScreenshotButton, &QPushButton::clicked, this, &Screenshot::saveScreenshot); + buttonsLayout->addWidget(saveScreenshotButton); + QPushButton *quitScreenshotButton = new QPushButton(tr("Quit"), this); + quitScreenshotButton->setShortcut(Qt::CTRL + Qt::Key_Q); + connect(quitScreenshotButton, &QPushButton::clicked, this, &QWidget::close); + buttonsLayout->addWidget(quitScreenshotButton); + buttonsLayout->addStretch(); mainLayout->addLayout(buttonsLayout); - setLayout(mainLayout); shootScreen(); delaySpinBox->setValue(5); @@ -84,44 +112,59 @@ void Screenshot::newScreenshot() hide(); newScreenshotButton->setDisabled(true); - QTimer::singleShot(delaySpinBox->value() * 1000, this, SLOT(shootScreen())); + QTimer::singleShot(delaySpinBox->value() * 1000, this, &Screenshot::shootScreen); } //! [2] //! [3] void Screenshot::saveScreenshot() { - QString format = "png"; - QString initialPath = QDir::currentPath() + tr("/untitled.") + format; - - QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), initialPath, - tr("%1 Files (*.%2);;All Files (*)") - .arg(format.toUpper()) - .arg(format)); - if (!fileName.isEmpty()) - originalPixmap.save(fileName, format.toLatin1().constData()); + const QString format = "png"; + QString initialPath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation); + if (initialPath.isEmpty()) + initialPath = QDir::currentPath(); + initialPath += tr("/untitled.") + format; + + QFileDialog fileDialog(this, tr("Save As"), initialPath); + fileDialog.setAcceptMode(QFileDialog::AcceptSave); + fileDialog.setFileMode(QFileDialog::AnyFile); + fileDialog.setDirectory(initialPath); + QStringList mimeTypes; + foreach (const QByteArray &bf, QImageWriter::supportedMimeTypes()) + mimeTypes.append(QLatin1String(bf)); + fileDialog.setMimeTypeFilters(mimeTypes); + fileDialog.selectMimeTypeFilter("image/" + format); + fileDialog.setDefaultSuffix(format); + if (fileDialog.exec() != QDialog::Accepted) + return; + const QString fileName = fileDialog.selectedFiles().first(); + if (!originalPixmap.save(fileName)) { + QMessageBox::warning(this, tr("Save Error"), tr("The image could not be saved to \"%1\".") + .arg(QDir::toNativeSeparators(fileName))); + } } //! [3] //! [4] void Screenshot::shootScreen() { - if (delaySpinBox->value() != 0) - qApp->beep(); -//! [4] - originalPixmap = QPixmap(); // clear image for low memory situations - // on embedded devices. -//! [5] QScreen *screen = QGuiApplication::primaryScreen(); - if (screen) - originalPixmap = screen->grabWindow(0); + if (const QWindow *window = windowHandle()) + screen = window->screen(); + if (!screen) + return; + + if (delaySpinBox->value() != 0) + QApplication::beep(); + + originalPixmap = screen->grabWindow(0); updateScreenshotLabel(); newScreenshotButton->setDisabled(false); if (hideThisWindowCheckBox->isChecked()) show(); } -//! [5] +//! [4] //! [6] void Screenshot::updateCheckBox() @@ -135,52 +178,6 @@ void Screenshot::updateCheckBox() } //! [6] -//! [7] -void Screenshot::createOptionsGroupBox() -{ - optionsGroupBox = new QGroupBox(tr("Options")); - - delaySpinBox = new QSpinBox; - delaySpinBox->setSuffix(tr(" s")); - delaySpinBox->setMaximum(60); - connect(delaySpinBox, SIGNAL(valueChanged(int)), this, SLOT(updateCheckBox())); - - delaySpinBoxLabel = new QLabel(tr("Screenshot Delay:")); - - hideThisWindowCheckBox = new QCheckBox(tr("Hide This Window")); - - optionsGroupBoxLayout = new QGridLayout; - optionsGroupBoxLayout->addWidget(delaySpinBoxLabel, 0, 0); - optionsGroupBoxLayout->addWidget(delaySpinBox, 0, 1); - optionsGroupBoxLayout->addWidget(hideThisWindowCheckBox, 1, 0, 1, 2); - optionsGroupBox->setLayout(optionsGroupBoxLayout); -} -//! [7] - -//! [8] -void Screenshot::createButtonsLayout() -{ - newScreenshotButton = createButton(tr("New Screenshot"), this, SLOT(newScreenshot())); - saveScreenshotButton = createButton(tr("Save Screenshot"), this, SLOT(saveScreenshot())); - quitScreenshotButton = createButton(tr("Quit"), this, SLOT(close())); - - buttonsLayout = new QHBoxLayout; - buttonsLayout->addStretch(); - buttonsLayout->addWidget(newScreenshotButton); - buttonsLayout->addWidget(saveScreenshotButton); - buttonsLayout->addWidget(quitScreenshotButton); -} -//! [8] - -//! [9] -QPushButton *Screenshot::createButton(const QString &text, QWidget *receiver, - const char *member) -{ - QPushButton *button = new QPushButton(text); - button->connect(button, SIGNAL(clicked()), receiver, member); - return button; -} -//! [9] //! [10] void Screenshot::updateScreenshotLabel() diff --git a/examples/widgets/desktop/screenshot/screenshot.h b/examples/widgets/desktop/screenshot/screenshot.h index 352806cdba..f0e737e4c7 100644 --- a/examples/widgets/desktop/screenshot/screenshot.h +++ b/examples/widgets/desktop/screenshot/screenshot.h @@ -73,25 +73,14 @@ private slots: void updateCheckBox(); private: - void createOptionsGroupBox(); - void createButtonsLayout(); - QPushButton *createButton(const QString &text, QWidget *receiver, const char *member); void updateScreenshotLabel(); QPixmap originalPixmap; QLabel *screenshotLabel; - QGroupBox *optionsGroupBox; QSpinBox *delaySpinBox; - QLabel *delaySpinBoxLabel; QCheckBox *hideThisWindowCheckBox; QPushButton *newScreenshotButton; - QPushButton *saveScreenshotButton; - QPushButton *quitScreenshotButton; - - QVBoxLayout *mainLayout; - QGridLayout *optionsGroupBoxLayout; - QHBoxLayout *buttonsLayout; }; //! [0] diff --git a/examples/widgets/desktop/systray/window.cpp b/examples/widgets/desktop/systray/window.cpp index b212dc440d..931e443de7 100644 --- a/examples/widgets/desktop/systray/window.cpp +++ b/examples/widgets/desktop/systray/window.cpp @@ -42,11 +42,11 @@ #ifndef QT_NO_SYSTEMTRAYICON -#include <QtGui> - #include <QAction> #include <QCheckBox> #include <QComboBox> +#include <QCoreApplication> +#include <QCloseEvent> #include <QGroupBox> #include <QLabel> #include <QLineEdit> @@ -68,12 +68,13 @@ Window::Window() createActions(); createTrayIcon(); - connect(showMessageButton, SIGNAL(clicked()), this, SLOT(showMessage())); - connect(showIconCheckBox, SIGNAL(toggled(bool)), trayIcon, SLOT(setVisible(bool))); - connect(iconComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setIcon(int))); - connect(trayIcon, SIGNAL(messageClicked()), this, SLOT(messageClicked())); - connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), - this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason))); + connect(showMessageButton, &QAbstractButton::clicked, this, &Window::showMessage); + connect(showIconCheckBox, &QAbstractButton::toggled, trayIcon, &QSystemTrayIcon::setVisible); + typedef void (QComboBox::*QComboIntSignal)(int); + connect(iconComboBox, static_cast<QComboIntSignal>(&QComboBox::currentIndexChanged), + this, &Window::setIcon); + connect(trayIcon, &QSystemTrayIcon::messageClicked, this, &Window::messageClicked); + connect(trayIcon, &QSystemTrayIcon::activated, this, &Window::iconActivated); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(iconGroupBox); @@ -245,16 +246,16 @@ void Window::createMessageGroupBox() void Window::createActions() { minimizeAction = new QAction(tr("Mi&nimize"), this); - connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide())); + connect(minimizeAction, &QAction::triggered, this, &QWidget::hide); maximizeAction = new QAction(tr("Ma&ximize"), this); - connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized())); + connect(maximizeAction, &QAction::triggered, this, &QWidget::showMaximized); restoreAction = new QAction(tr("&Restore"), this); - connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal())); + connect(restoreAction, &QAction::triggered, this, &QWidget::showNormal); quitAction = new QAction(tr("&Quit"), this); - connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); + connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit); } void Window::createTrayIcon() |