mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-02 15:26:00 +08:00
qt 6.5.1 original
This commit is contained in:
37
examples/widgets/desktop/screenshot/CMakeLists.txt
Normal file
37
examples/widgets/desktop/screenshot/CMakeLists.txt
Normal file
@ -0,0 +1,37 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(screenshot LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/desktop/screenshot")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(screenshot
|
||||
main.cpp
|
||||
screenshot.cpp screenshot.h
|
||||
)
|
||||
|
||||
set_target_properties(screenshot PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(screenshot PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
install(TARGETS screenshot
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
18
examples/widgets/desktop/screenshot/main.cpp
Normal file
18
examples/widgets/desktop/screenshot/main.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QApplication>
|
||||
#include <QScreen>
|
||||
|
||||
#include "screenshot.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
|
||||
Screenshot screenshot;
|
||||
screenshot.move(screenshot.screen()->availableGeometry().topLeft() + QPoint(20, 20));
|
||||
screenshot.show();
|
||||
|
||||
return app.exec();
|
||||
}
|
152
examples/widgets/desktop/screenshot/screenshot.cpp
Normal file
152
examples/widgets/desktop/screenshot/screenshot.cpp
Normal file
@ -0,0 +1,152 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QtWidgets>
|
||||
|
||||
#include "screenshot.h"
|
||||
|
||||
//! [0]
|
||||
Screenshot::Screenshot()
|
||||
: screenshotLabel(new QLabel(this))
|
||||
{
|
||||
screenshotLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
||||
screenshotLabel->setAlignment(Qt::AlignCenter);
|
||||
|
||||
const QRect screenGeometry = screen()->geometry();
|
||||
screenshotLabel->setMinimumSize(screenGeometry.width() / 8, screenGeometry.height() / 8);
|
||||
|
||||
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);
|
||||
|
||||
connect(delaySpinBox, &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);
|
||||
|
||||
shootScreen();
|
||||
delaySpinBox->setValue(5);
|
||||
|
||||
setWindowTitle(tr("Screenshot"));
|
||||
resize(300, 200);
|
||||
}
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
void Screenshot::resizeEvent(QResizeEvent * /* event */)
|
||||
{
|
||||
QSize scaledSize = originalPixmap.size();
|
||||
scaledSize.scale(screenshotLabel->size(), Qt::KeepAspectRatio);
|
||||
if (scaledSize != screenshotLabel->pixmap().size())
|
||||
updateScreenshotLabel();
|
||||
}
|
||||
//! [1]
|
||||
|
||||
//! [2]
|
||||
void Screenshot::newScreenshot()
|
||||
{
|
||||
if (hideThisWindowCheckBox->isChecked())
|
||||
hide();
|
||||
newScreenshotButton->setDisabled(true);
|
||||
|
||||
QTimer::singleShot(delaySpinBox->value() * 1000, this, &Screenshot::shootScreen);
|
||||
}
|
||||
//! [2]
|
||||
|
||||
//! [3]
|
||||
void Screenshot::saveScreenshot()
|
||||
{
|
||||
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;
|
||||
const QList<QByteArray> baMimeTypes = QImageWriter::supportedMimeTypes();
|
||||
for (const QByteArray &bf : baMimeTypes)
|
||||
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()
|
||||
{
|
||||
QScreen *screen = QGuiApplication::primaryScreen();
|
||||
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();
|
||||
}
|
||||
//! [4]
|
||||
|
||||
//! [6]
|
||||
void Screenshot::updateCheckBox()
|
||||
{
|
||||
if (delaySpinBox->value() == 0) {
|
||||
hideThisWindowCheckBox->setDisabled(true);
|
||||
hideThisWindowCheckBox->setChecked(false);
|
||||
} else {
|
||||
hideThisWindowCheckBox->setDisabled(false);
|
||||
}
|
||||
}
|
||||
//! [6]
|
||||
|
||||
|
||||
//! [10]
|
||||
void Screenshot::updateScreenshotLabel()
|
||||
{
|
||||
screenshotLabel->setPixmap(originalPixmap.scaled(screenshotLabel->size(),
|
||||
Qt::KeepAspectRatio,
|
||||
Qt::SmoothTransformation));
|
||||
}
|
||||
//! [10]
|
50
examples/widgets/desktop/screenshot/screenshot.h
Normal file
50
examples/widgets/desktop/screenshot/screenshot.h
Normal file
@ -0,0 +1,50 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef SCREENSHOT_H
|
||||
#define SCREENSHOT_H
|
||||
|
||||
#include <QPixmap>
|
||||
#include <QWidget>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QCheckBox;
|
||||
class QGridLayout;
|
||||
class QGroupBox;
|
||||
class QHBoxLayout;
|
||||
class QLabel;
|
||||
class QPushButton;
|
||||
class QSpinBox;
|
||||
class QVBoxLayout;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
//! [0]
|
||||
class Screenshot : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Screenshot();
|
||||
|
||||
protected:
|
||||
void resizeEvent(QResizeEvent *event) override;
|
||||
|
||||
private slots:
|
||||
void newScreenshot();
|
||||
void saveScreenshot();
|
||||
void shootScreen();
|
||||
void updateCheckBox();
|
||||
|
||||
private:
|
||||
void updateScreenshotLabel();
|
||||
|
||||
QPixmap originalPixmap;
|
||||
|
||||
QLabel *screenshotLabel;
|
||||
QSpinBox *delaySpinBox;
|
||||
QCheckBox *hideThisWindowCheckBox;
|
||||
QPushButton *newScreenshotButton;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
#endif // SCREENSHOT_H
|
10
examples/widgets/desktop/screenshot/screenshot.pro
Normal file
10
examples/widgets/desktop/screenshot/screenshot.pro
Normal file
@ -0,0 +1,10 @@
|
||||
QT += widgets
|
||||
requires(qtConfig(filedialog))
|
||||
|
||||
HEADERS = screenshot.h
|
||||
SOURCES = main.cpp \
|
||||
screenshot.cpp
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/desktop/screenshot
|
||||
INSTALLS += target
|
Reference in New Issue
Block a user