mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-02 23:35:28 +08:00
qt 6.5.1 original
This commit is contained in:
2
examples/widgets/desktop/CMakeLists.txt
Normal file
2
examples/widgets/desktop/CMakeLists.txt
Normal file
@ -0,0 +1,2 @@
|
||||
qt_internal_add_example(screenshot)
|
||||
qt_internal_add_example(systray)
|
10
examples/widgets/desktop/README
Normal file
10
examples/widgets/desktop/README
Normal file
@ -0,0 +1,10 @@
|
||||
Qt provides features to enable applications to integrate with the user's
|
||||
preferred desktop environment.
|
||||
|
||||
Features such as system tray icons, access to the desktop widget, and
|
||||
support for desktop services can be used to improve the appearance of
|
||||
applications and take advantage of underlying desktop facilities.
|
||||
|
||||
|
||||
Documentation for these examples can be found via the Examples
|
||||
link in the main Qt documentation.
|
2
examples/widgets/desktop/desktop.pro
Normal file
2
examples/widgets/desktop/desktop.pro
Normal file
@ -0,0 +1,2 @@
|
||||
TEMPLATE = subdirs
|
||||
SUBDIRS = screenshot systray
|
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
|
51
examples/widgets/desktop/systray/CMakeLists.txt
Normal file
51
examples/widgets/desktop/systray/CMakeLists.txt
Normal file
@ -0,0 +1,51 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(systray LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/desktop/systray")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(systray
|
||||
main.cpp
|
||||
window.cpp window.h
|
||||
)
|
||||
|
||||
set_target_properties(systray PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(systray PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
# Resources:
|
||||
set(systray_resource_files
|
||||
"images/bad.png"
|
||||
"images/heart.png"
|
||||
"images/trash.png"
|
||||
)
|
||||
|
||||
qt_add_resources(systray "systray"
|
||||
PREFIX
|
||||
"/"
|
||||
FILES
|
||||
${systray_resource_files}
|
||||
)
|
||||
|
||||
install(TARGETS systray
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
Binary file not shown.
After Width: | Height: | Size: 25 KiB |
Binary file not shown.
After Width: | Height: | Size: 46 KiB |
163
examples/widgets/desktop/systray/doc/src/systray.qdoc
Normal file
163
examples/widgets/desktop/systray/doc/src/systray.qdoc
Normal file
@ -0,0 +1,163 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
|
||||
|
||||
/*!
|
||||
\example desktop/systray
|
||||
\title System Tray Icon Example
|
||||
\ingroup examples-widgets
|
||||
\brief The System Tray Icon example shows how to add an icon with a menu
|
||||
and popup messages to a desktop environment's system tray.
|
||||
|
||||
\borderedimage systemtray-example.png
|
||||
\caption Screenshot of the System Tray Icon
|
||||
|
||||
Modern operating systems usually provide a special area on the
|
||||
desktop, called the system tray or notification area, where
|
||||
long-running applications can display icons and short messages.
|
||||
|
||||
This example consists of one single class, \c Window, providing
|
||||
the main application window (i.e., an editor for the system tray
|
||||
icon) and the associated icon.
|
||||
|
||||
\borderedimage systemtray-editor.png
|
||||
|
||||
The editor allows the user to choose the preferred icon as well as
|
||||
set the balloon message's type and duration. The user can also
|
||||
edit the message's title and body. Finally, the editor provides a
|
||||
checkbox controlling whether the icon is actually shown in the
|
||||
system tray, or not.
|
||||
|
||||
\section1 Window Class Definition
|
||||
|
||||
The \c Window class inherits QWidget:
|
||||
|
||||
\snippet desktop/systray/window.h 0
|
||||
|
||||
We implement several private slots to respond to user
|
||||
interaction. The other private functions are only convenience
|
||||
functions provided to simplify the constructor.
|
||||
|
||||
The tray icon is an instance of the QSystemTrayIcon class. To
|
||||
check whether a system tray is present on the user's desktop, call
|
||||
the static QSystemTrayIcon::isSystemTrayAvailable()
|
||||
function. Associated with the icon, we provide a menu containing
|
||||
the typical \uicontrol minimize, \uicontrol maximize, \uicontrol restore and
|
||||
\uicontrol quit actions. We reimplement the QWidget::setVisible() function to
|
||||
update the tray icon's menu whenever the editor's appearance
|
||||
changes, e.g., when maximizing or minimizing the main application
|
||||
window.
|
||||
|
||||
Finally, we reimplement QWidget's \l {QWidget::}{closeEvent()}
|
||||
function to be able to inform the user (when closing the editor
|
||||
window) that the program will keep running in the system tray
|
||||
until the user chooses the \uicontrol Quit entry in the icon's context
|
||||
menu.
|
||||
|
||||
\section1 Window Class Implementation
|
||||
|
||||
When constructing the editor widget, we first create the various
|
||||
editor elements before we create the actual system tray icon:
|
||||
|
||||
\snippet desktop/systray/window.cpp 0
|
||||
|
||||
We ensure that the application responds to user input by
|
||||
connecting most of the editor's input widgets (including the
|
||||
system tray icon) to the application's private slots. But note the
|
||||
visibility checkbox; its \l {QCheckBox::}{toggled()} signal is
|
||||
connected to the \e {icon}'s \l {QSystemTrayIcon::}{setVisible()}
|
||||
function instead.
|
||||
|
||||
\snippet desktop/systray/window.cpp 3
|
||||
|
||||
The \c setIcon() slot is triggered whenever the current index in
|
||||
the icon combobox changes, i.e., whenever the user chooses another
|
||||
icon in the editor. Note that it is also called when the user
|
||||
activates the tray icon with the left mouse button, triggering the
|
||||
icon's \l {QSystemTrayIcon::}{activated()} signal. We will come
|
||||
back to this signal shortly.
|
||||
|
||||
The QSystemTrayIcon::setIcon() function sets the \l
|
||||
{QSystemTrayIcon::}{icon} property that holds the actual system
|
||||
tray icon. On Windows, the system tray icon size is 16x16; on X11,
|
||||
the preferred size is 22x22. The icon will be scaled to the
|
||||
appropriate size as necessary.
|
||||
|
||||
Note that on X11, due to a limitation in the system tray
|
||||
specification, mouse clicks on transparent areas in the icon are
|
||||
propagated to the system tray. If this behavior is unacceptable,
|
||||
we suggest using an icon with no transparency.
|
||||
|
||||
\snippet desktop/systray/window.cpp 4
|
||||
|
||||
Whenever the user activates the system tray icon, it emits its \l
|
||||
{QSystemTrayIcon::}{activated()} signal passing the triggering
|
||||
reason as parameter. QSystemTrayIcon provides the \l
|
||||
{QSystemTrayIcon::}{ActivationReason} enum to describe how the
|
||||
icon was activated.
|
||||
|
||||
In the constructor, we connected our icon's \l
|
||||
{QSystemTrayIcon::}{activated()} signal to our custom \c
|
||||
iconActivated() slot: If the user has clicked the icon using the
|
||||
left mouse button, this function changes the icon image by
|
||||
incrementing the icon combobox's current index, triggering the \c
|
||||
setIcon() slot as mentioned above. If the user activates the icon
|
||||
using the middle mouse button, it calls the custom \c
|
||||
showMessage() slot:
|
||||
|
||||
\snippet desktop/systray/window.cpp 5
|
||||
|
||||
When the \e showMessage() slot is triggered, we first retrieve the
|
||||
message icon depending on the currently chosen message type. The
|
||||
QSystemTrayIcon::MessageIcon enum describes the icon that is shown
|
||||
when a balloon message is displayed. Then we call
|
||||
QSystemTrayIcon's \l {QSystemTrayIcon::}{showMessage()} function
|
||||
to show the message with the title, body, and icon for the time
|
||||
specified in milliseconds.
|
||||
|
||||
\macos users note: The Growl notification system must be
|
||||
installed for QSystemTrayIcon::showMessage() to display messages.
|
||||
|
||||
QSystemTrayIcon also has the corresponding, \l {QSystemTrayIcon::}
|
||||
{messageClicked()} signal, which is emitted when the user clicks a
|
||||
message displayed by \l {QSystemTrayIcon::}{showMessage()}.
|
||||
|
||||
\snippet desktop/systray/window.cpp 6
|
||||
|
||||
In the constructor, we connected the \l
|
||||
{QSystemTrayIcon::}{messageClicked()} signal to our custom \c
|
||||
messageClicked() slot that simply displays a message using the
|
||||
QMessageBox class.
|
||||
|
||||
QMessageBox provides a modal dialog with a short message, an icon,
|
||||
and buttons laid out depending on the current style. It supports
|
||||
four severity levels: "Question", "Information", "Warning" and
|
||||
"Critical". The easiest way to pop up a message box in Qt is to
|
||||
call one of the associated static functions, e.g.,
|
||||
QMessageBox::information().
|
||||
|
||||
As we mentioned earlier, we reimplement a couple of QWidget's
|
||||
virtual functions:
|
||||
|
||||
\snippet desktop/systray/window.cpp 1
|
||||
|
||||
Our reimplementation of the QWidget::setVisible() function updates
|
||||
the tray icon's menu whenever the editor's appearance changes,
|
||||
e.g., when maximizing or minimizing the main application window,
|
||||
before calling the base class implementation.
|
||||
|
||||
\snippet desktop/systray/window.cpp 2
|
||||
|
||||
We have reimplemented the QWidget::closeEvent() event handler to
|
||||
receive widget close events, showing the above message to the
|
||||
users when they are closing the editor window. We need to
|
||||
avoid showing the message and accepting the close event when the
|
||||
user really intends to quit the application: that is, when the
|
||||
user has triggered "Quit" in the menu bar, or in the tray icon's
|
||||
context menu, or pressed Command+Q shortcut on \macOS.
|
||||
|
||||
In addition to the functions and slots discussed above, we have
|
||||
also implemented several convenience functions to simplify the
|
||||
constructor: \c createIconGroupBox(), \c createMessageGroupBox(),
|
||||
\c createActions() and \c createTrayIcon(). See the \c
|
||||
{desktop/systray/window.cpp} file for details.
|
||||
*/
|
BIN
examples/widgets/desktop/systray/images/bad.png
Normal file
BIN
examples/widgets/desktop/systray/images/bad.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.4 KiB |
BIN
examples/widgets/desktop/systray/images/heart.png
Normal file
BIN
examples/widgets/desktop/systray/images/heart.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 25 KiB |
BIN
examples/widgets/desktop/systray/images/trash.png
Normal file
BIN
examples/widgets/desktop/systray/images/trash.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 12 KiB |
49
examples/widgets/desktop/systray/main.cpp
Normal file
49
examples/widgets/desktop/systray/main.cpp
Normal file
@ -0,0 +1,49 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
#ifndef QT_NO_SYSTEMTRAYICON
|
||||
|
||||
#include <QMessageBox>
|
||||
#include "window.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
Q_INIT_RESOURCE(systray);
|
||||
|
||||
QApplication app(argc, argv);
|
||||
|
||||
if (!QSystemTrayIcon::isSystemTrayAvailable()) {
|
||||
QMessageBox::critical(nullptr, QObject::tr("Systray"),
|
||||
QObject::tr("I couldn't detect any system tray "
|
||||
"on this system."));
|
||||
return 1;
|
||||
}
|
||||
QApplication::setQuitOnLastWindowClosed(false);
|
||||
|
||||
Window window;
|
||||
window.show();
|
||||
return app.exec();
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include <QLabel>
|
||||
#include <QDebug>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
QString text("QSystemTrayIcon is not supported on this platform");
|
||||
|
||||
QLabel *label = new QLabel(text);
|
||||
label->setWordWrap(true);
|
||||
|
||||
label->show();
|
||||
qDebug() << text;
|
||||
|
||||
app.exec();
|
||||
}
|
||||
|
||||
#endif
|
11
examples/widgets/desktop/systray/systray.pro
Normal file
11
examples/widgets/desktop/systray/systray.pro
Normal file
@ -0,0 +1,11 @@
|
||||
HEADERS = window.h
|
||||
SOURCES = main.cpp \
|
||||
window.cpp
|
||||
RESOURCES = systray.qrc
|
||||
|
||||
QT += widgets
|
||||
requires(qtConfig(combobox))
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/desktop/systray
|
||||
INSTALLS += target
|
7
examples/widgets/desktop/systray/systray.qrc
Normal file
7
examples/widgets/desktop/systray/systray.qrc
Normal file
@ -0,0 +1,7 @@
|
||||
<!DOCTYPE RCC><RCC version="1.0">
|
||||
<qresource prefix="/">
|
||||
<file>images/bad.png</file>
|
||||
<file>images/heart.png</file>
|
||||
<file>images/trash.png</file>
|
||||
</qresource>
|
||||
</RCC>
|
247
examples/widgets/desktop/systray/window.cpp
Normal file
247
examples/widgets/desktop/systray/window.cpp
Normal file
@ -0,0 +1,247 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "window.h"
|
||||
|
||||
#ifndef QT_NO_SYSTEMTRAYICON
|
||||
|
||||
#include <QAction>
|
||||
#include <QCheckBox>
|
||||
#include <QComboBox>
|
||||
#include <QCoreApplication>
|
||||
#include <QCloseEvent>
|
||||
#include <QGroupBox>
|
||||
#include <QLabel>
|
||||
#include <QLineEdit>
|
||||
#include <QMenu>
|
||||
#include <QPushButton>
|
||||
#include <QSpinBox>
|
||||
#include <QTextEdit>
|
||||
#include <QVBoxLayout>
|
||||
#include <QMessageBox>
|
||||
|
||||
//! [0]
|
||||
Window::Window()
|
||||
{
|
||||
createIconGroupBox();
|
||||
createMessageGroupBox();
|
||||
|
||||
iconLabel->setMinimumWidth(durationLabel->sizeHint().width());
|
||||
|
||||
createActions();
|
||||
createTrayIcon();
|
||||
|
||||
connect(showMessageButton, &QAbstractButton::clicked, this, &Window::showMessage);
|
||||
connect(showIconCheckBox, &QAbstractButton::toggled, trayIcon, &QSystemTrayIcon::setVisible);
|
||||
connect(iconComboBox, &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);
|
||||
mainLayout->addWidget(messageGroupBox);
|
||||
setLayout(mainLayout);
|
||||
|
||||
iconComboBox->setCurrentIndex(1);
|
||||
trayIcon->show();
|
||||
|
||||
setWindowTitle(tr("Systray"));
|
||||
resize(400, 300);
|
||||
}
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
void Window::setVisible(bool visible)
|
||||
{
|
||||
minimizeAction->setEnabled(visible);
|
||||
maximizeAction->setEnabled(!isMaximized());
|
||||
restoreAction->setEnabled(isMaximized() || !visible);
|
||||
QDialog::setVisible(visible);
|
||||
}
|
||||
//! [1]
|
||||
|
||||
//! [2]
|
||||
void Window::closeEvent(QCloseEvent *event)
|
||||
{
|
||||
if (!event->spontaneous() || !isVisible())
|
||||
return;
|
||||
if (trayIcon->isVisible()) {
|
||||
QMessageBox::information(this, tr("Systray"),
|
||||
tr("The program will keep running in the "
|
||||
"system tray. To terminate the program, "
|
||||
"choose <b>Quit</b> in the context menu "
|
||||
"of the system tray entry."));
|
||||
hide();
|
||||
event->ignore();
|
||||
}
|
||||
}
|
||||
//! [2]
|
||||
|
||||
//! [3]
|
||||
void Window::setIcon(int index)
|
||||
{
|
||||
QIcon icon = iconComboBox->itemIcon(index);
|
||||
trayIcon->setIcon(icon);
|
||||
setWindowIcon(icon);
|
||||
|
||||
trayIcon->setToolTip(iconComboBox->itemText(index));
|
||||
}
|
||||
//! [3]
|
||||
|
||||
//! [4]
|
||||
void Window::iconActivated(QSystemTrayIcon::ActivationReason reason)
|
||||
{
|
||||
switch (reason) {
|
||||
case QSystemTrayIcon::Trigger:
|
||||
case QSystemTrayIcon::DoubleClick:
|
||||
iconComboBox->setCurrentIndex((iconComboBox->currentIndex() + 1) % iconComboBox->count());
|
||||
break;
|
||||
case QSystemTrayIcon::MiddleClick:
|
||||
showMessage();
|
||||
break;
|
||||
default:
|
||||
;
|
||||
}
|
||||
}
|
||||
//! [4]
|
||||
|
||||
//! [5]
|
||||
void Window::showMessage()
|
||||
{
|
||||
showIconCheckBox->setChecked(true);
|
||||
int selectedIcon = typeComboBox->itemData(typeComboBox->currentIndex()).toInt();
|
||||
QSystemTrayIcon::MessageIcon msgIcon = QSystemTrayIcon::MessageIcon(selectedIcon);
|
||||
|
||||
if (selectedIcon == -1) { // custom icon
|
||||
QIcon icon(iconComboBox->itemIcon(iconComboBox->currentIndex()));
|
||||
trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), icon,
|
||||
durationSpinBox->value() * 1000);
|
||||
} else {
|
||||
trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), msgIcon,
|
||||
durationSpinBox->value() * 1000);
|
||||
}
|
||||
}
|
||||
//! [5]
|
||||
|
||||
//! [6]
|
||||
void Window::messageClicked()
|
||||
{
|
||||
QMessageBox::information(nullptr, tr("Systray"),
|
||||
tr("Sorry, I already gave what help I could.\n"
|
||||
"Maybe you should try asking a human?"));
|
||||
}
|
||||
//! [6]
|
||||
|
||||
void Window::createIconGroupBox()
|
||||
{
|
||||
iconGroupBox = new QGroupBox(tr("Tray Icon"));
|
||||
|
||||
iconLabel = new QLabel("Icon:");
|
||||
|
||||
iconComboBox = new QComboBox;
|
||||
iconComboBox->addItem(QIcon(":/images/bad.png"), tr("Bad"));
|
||||
iconComboBox->addItem(QIcon(":/images/heart.png"), tr("Heart"));
|
||||
iconComboBox->addItem(QIcon(":/images/trash.png"), tr("Trash"));
|
||||
|
||||
showIconCheckBox = new QCheckBox(tr("Show icon"));
|
||||
showIconCheckBox->setChecked(true);
|
||||
|
||||
QHBoxLayout *iconLayout = new QHBoxLayout;
|
||||
iconLayout->addWidget(iconLabel);
|
||||
iconLayout->addWidget(iconComboBox);
|
||||
iconLayout->addStretch();
|
||||
iconLayout->addWidget(showIconCheckBox);
|
||||
iconGroupBox->setLayout(iconLayout);
|
||||
}
|
||||
|
||||
void Window::createMessageGroupBox()
|
||||
{
|
||||
messageGroupBox = new QGroupBox(tr("Balloon Message"));
|
||||
|
||||
typeLabel = new QLabel(tr("Type:"));
|
||||
|
||||
typeComboBox = new QComboBox;
|
||||
typeComboBox->addItem(tr("None"), QSystemTrayIcon::NoIcon);
|
||||
typeComboBox->addItem(style()->standardIcon(
|
||||
QStyle::SP_MessageBoxInformation), tr("Information"),
|
||||
QSystemTrayIcon::Information);
|
||||
typeComboBox->addItem(style()->standardIcon(
|
||||
QStyle::SP_MessageBoxWarning), tr("Warning"),
|
||||
QSystemTrayIcon::Warning);
|
||||
typeComboBox->addItem(style()->standardIcon(
|
||||
QStyle::SP_MessageBoxCritical), tr("Critical"),
|
||||
QSystemTrayIcon::Critical);
|
||||
typeComboBox->addItem(QIcon(), tr("Custom icon"),
|
||||
-1);
|
||||
typeComboBox->setCurrentIndex(1);
|
||||
|
||||
durationLabel = new QLabel(tr("Duration:"));
|
||||
|
||||
durationSpinBox = new QSpinBox;
|
||||
durationSpinBox->setRange(5, 60);
|
||||
durationSpinBox->setSuffix(" s");
|
||||
durationSpinBox->setValue(15);
|
||||
|
||||
durationWarningLabel = new QLabel(tr("(some systems might ignore this "
|
||||
"hint)"));
|
||||
durationWarningLabel->setIndent(10);
|
||||
|
||||
titleLabel = new QLabel(tr("Title:"));
|
||||
|
||||
titleEdit = new QLineEdit(tr("Cannot connect to network"));
|
||||
|
||||
bodyLabel = new QLabel(tr("Body:"));
|
||||
|
||||
bodyEdit = new QTextEdit;
|
||||
bodyEdit->setPlainText(tr("Don't believe me. Honestly, I don't have a "
|
||||
"clue.\nClick this balloon for details."));
|
||||
|
||||
showMessageButton = new QPushButton(tr("Show Message"));
|
||||
showMessageButton->setDefault(true);
|
||||
|
||||
QGridLayout *messageLayout = new QGridLayout;
|
||||
messageLayout->addWidget(typeLabel, 0, 0);
|
||||
messageLayout->addWidget(typeComboBox, 0, 1, 1, 2);
|
||||
messageLayout->addWidget(durationLabel, 1, 0);
|
||||
messageLayout->addWidget(durationSpinBox, 1, 1);
|
||||
messageLayout->addWidget(durationWarningLabel, 1, 2, 1, 3);
|
||||
messageLayout->addWidget(titleLabel, 2, 0);
|
||||
messageLayout->addWidget(titleEdit, 2, 1, 1, 4);
|
||||
messageLayout->addWidget(bodyLabel, 3, 0);
|
||||
messageLayout->addWidget(bodyEdit, 3, 1, 2, 4);
|
||||
messageLayout->addWidget(showMessageButton, 5, 4);
|
||||
messageLayout->setColumnStretch(3, 1);
|
||||
messageLayout->setRowStretch(4, 1);
|
||||
messageGroupBox->setLayout(messageLayout);
|
||||
}
|
||||
|
||||
void Window::createActions()
|
||||
{
|
||||
minimizeAction = new QAction(tr("Mi&nimize"), this);
|
||||
connect(minimizeAction, &QAction::triggered, this, &QWidget::hide);
|
||||
|
||||
maximizeAction = new QAction(tr("Ma&ximize"), this);
|
||||
connect(maximizeAction, &QAction::triggered, this, &QWidget::showMaximized);
|
||||
|
||||
restoreAction = new QAction(tr("&Restore"), this);
|
||||
connect(restoreAction, &QAction::triggered, this, &QWidget::showNormal);
|
||||
|
||||
quitAction = new QAction(tr("&Quit"), this);
|
||||
connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
|
||||
}
|
||||
|
||||
void Window::createTrayIcon()
|
||||
{
|
||||
trayIconMenu = new QMenu(this);
|
||||
trayIconMenu->addAction(minimizeAction);
|
||||
trayIconMenu->addAction(maximizeAction);
|
||||
trayIconMenu->addAction(restoreAction);
|
||||
trayIconMenu->addSeparator();
|
||||
trayIconMenu->addAction(quitAction);
|
||||
|
||||
trayIcon = new QSystemTrayIcon(this);
|
||||
trayIcon->setContextMenu(trayIconMenu);
|
||||
}
|
||||
|
||||
#endif
|
80
examples/widgets/desktop/systray/window.h
Normal file
80
examples/widgets/desktop/systray/window.h
Normal file
@ -0,0 +1,80 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef WINDOW_H
|
||||
#define WINDOW_H
|
||||
|
||||
#include <QSystemTrayIcon>
|
||||
|
||||
#ifndef QT_NO_SYSTEMTRAYICON
|
||||
|
||||
#include <QDialog>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QAction;
|
||||
class QCheckBox;
|
||||
class QComboBox;
|
||||
class QGroupBox;
|
||||
class QLabel;
|
||||
class QLineEdit;
|
||||
class QMenu;
|
||||
class QPushButton;
|
||||
class QSpinBox;
|
||||
class QTextEdit;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
//! [0]
|
||||
class Window : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Window();
|
||||
|
||||
void setVisible(bool visible) override;
|
||||
|
||||
protected:
|
||||
void closeEvent(QCloseEvent *event) override;
|
||||
|
||||
private slots:
|
||||
void setIcon(int index);
|
||||
void iconActivated(QSystemTrayIcon::ActivationReason reason);
|
||||
void showMessage();
|
||||
void messageClicked();
|
||||
|
||||
private:
|
||||
void createIconGroupBox();
|
||||
void createMessageGroupBox();
|
||||
void createActions();
|
||||
void createTrayIcon();
|
||||
|
||||
QGroupBox *iconGroupBox;
|
||||
QLabel *iconLabel;
|
||||
QComboBox *iconComboBox;
|
||||
QCheckBox *showIconCheckBox;
|
||||
|
||||
QGroupBox *messageGroupBox;
|
||||
QLabel *typeLabel;
|
||||
QLabel *durationLabel;
|
||||
QLabel *durationWarningLabel;
|
||||
QLabel *titleLabel;
|
||||
QLabel *bodyLabel;
|
||||
QComboBox *typeComboBox;
|
||||
QSpinBox *durationSpinBox;
|
||||
QLineEdit *titleEdit;
|
||||
QTextEdit *bodyEdit;
|
||||
QPushButton *showMessageButton;
|
||||
|
||||
QAction *minimizeAction;
|
||||
QAction *maximizeAction;
|
||||
QAction *restoreAction;
|
||||
QAction *quitAction;
|
||||
|
||||
QSystemTrayIcon *trayIcon;
|
||||
QMenu *trayIconMenu;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
#endif // QT_NO_SYSTEMTRAYICON
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user