QSystemTrayIcon 带有自定义图标的通知消息
QSystemTrayIcon notification message with custom icon
QSystemTrayIcon 有一个功能:
void showMessage(const QString &title, const QString &msg,
MessageIcon icon = Information, int msecs = 10000);
有没有办法将其更改为自定义图标,例如这样 -
void showIconMessage(const QString &title, const QString &msg,
QIcon icon = QIcon(), int msecs = 10000);
不修改 Qt 源代码
我知道 showMessage
(d 是 QSystemTrayIconPrivate 的实例,用 Q_D(QSystemTrayIcon)
宏调用)
void QSystemTrayIcon::showMessage(const QString& title, const QString& msg,
QSystemTrayIcon::MessageIcon icon, int msecs)
{
Q_D(QSystemTrayIcon);
if (d->visible)
d->showMessage_sys(title, msg, icon, msecs);
}
从 QSystemTrayIconPrivate
调用 showMessage_sys
,而所有带有图标的魔法又发生了:
void QSystemTrayIconPrivate::showMessage_sys(const QString &message,
const QString &title,
QSystemTrayIcon::MessageIcon icon,
int msecs)
{
if (!qpa_sys)
return;
QIcon notificationIcon;
switch (icon) {
case QSystemTrayIcon::Information:
notificationIcon = QApplication::style()- >standardIcon(QStyle::SP_MessageBoxInformation);
break;
case QSystemTrayIcon::Warning:
notificationIcon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning);
break;
case QSystemTrayIcon::Critical:
notificationIcon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxCritical);
break;
default:
break;
}
qpa_sys->showMessage(message, title, notificationIcon,
static_cast<QPlatformSystemTrayIcon::MessageIcon>(icon), msecs);
}
现在看来,我需要在两个 类 中重新实现这两个函数,我准备好了,但是.. QSystemTrayIcon 似乎与 QSystemTrayIconPrivate 紧密相关。 QSystemTrayIconPrivate 的实例仅在 QSystemTrayIcon 构造函数中创建(如果我打算创建继承 QSystemTrayIcon 和 QSystemTrayIconPrivate 的 类 并重新实现 showMessage 函数,我无法真正改变):
QSystemTrayIcon::QSystemTrayIcon(QObject *parent)
: QObject(*new QSystemTrayIconPrivate(), parent)
{
}
QSystemTrayIcon::QSystemTrayIcon(const QIcon &icon, QObject *parent)
: QObject(*new QSystemTrayIconPrivate(), parent)
{
setIcon(icon);
}
所以有什么我遗漏的吗?还是有另一种方法可以简单地显示带有自定义图标的通知消息?
您可以尝试(不确定它是否适用于系统托盘)按照 this 答案中的描述进行操作并覆盖 SP_MessageBoxWarning
/ SP_MessageBoxCritical
/ SP_MessageBoxInformation
图标,但正如我所说,我不确定系统托盘是否只使用缩小版本的消息框图标,或者系统托盘图标是否是单独的。在后者的情况下,我猜你将不得不修补 QT 源,也许添加一个新项目到 QSystemTrayIcon
并修补开关以调用你提供的一些函数到 return 所需的图标。类似于:
void QSystemTrayIconPrivate::showMessage_sys(const QString &message,
const QString &title,
QSystemTrayIcon::MessageIcon icon,
int msecs)
{
if (!qpa_sys)
return;
QIcon notificationIcon;
switch (icon) {
case QSystemTrayIcon::Information:
notificationIcon = QApplication::style()- >standardIcon(QStyle::SP_MessageBoxInformation);
break;
case QSystemTrayIcon::Warning:
notificationIcon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning);
break;
case QSystemTrayIcon::Critical:
notificationIcon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxCritical);
break;
case QSystemTrayIcon::Custom:
// Call a function that will fetch the needed icon and assign it to notificationIcon
break;
default:
break;
}
qpa_sys->showMessage(message, title, notificationIcon,
static_cast<QPlatformSystemTrayIcon::MessageIcon>(icon), msecs);
}
QSystemTrayIcon 有一个功能:
void showMessage(const QString &title, const QString &msg,
MessageIcon icon = Information, int msecs = 10000);
有没有办法将其更改为自定义图标,例如这样 -
void showIconMessage(const QString &title, const QString &msg,
QIcon icon = QIcon(), int msecs = 10000);
不修改 Qt 源代码
我知道 showMessage
(d 是 QSystemTrayIconPrivate 的实例,用 Q_D(QSystemTrayIcon)
宏调用)
void QSystemTrayIcon::showMessage(const QString& title, const QString& msg,
QSystemTrayIcon::MessageIcon icon, int msecs)
{
Q_D(QSystemTrayIcon);
if (d->visible)
d->showMessage_sys(title, msg, icon, msecs);
}
从 QSystemTrayIconPrivate
调用 showMessage_sys
,而所有带有图标的魔法又发生了:
void QSystemTrayIconPrivate::showMessage_sys(const QString &message,
const QString &title,
QSystemTrayIcon::MessageIcon icon,
int msecs)
{
if (!qpa_sys)
return;
QIcon notificationIcon;
switch (icon) {
case QSystemTrayIcon::Information:
notificationIcon = QApplication::style()- >standardIcon(QStyle::SP_MessageBoxInformation);
break;
case QSystemTrayIcon::Warning:
notificationIcon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning);
break;
case QSystemTrayIcon::Critical:
notificationIcon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxCritical);
break;
default:
break;
}
qpa_sys->showMessage(message, title, notificationIcon,
static_cast<QPlatformSystemTrayIcon::MessageIcon>(icon), msecs);
}
现在看来,我需要在两个 类 中重新实现这两个函数,我准备好了,但是.. QSystemTrayIcon 似乎与 QSystemTrayIconPrivate 紧密相关。 QSystemTrayIconPrivate 的实例仅在 QSystemTrayIcon 构造函数中创建(如果我打算创建继承 QSystemTrayIcon 和 QSystemTrayIconPrivate 的 类 并重新实现 showMessage 函数,我无法真正改变):
QSystemTrayIcon::QSystemTrayIcon(QObject *parent)
: QObject(*new QSystemTrayIconPrivate(), parent)
{
}
QSystemTrayIcon::QSystemTrayIcon(const QIcon &icon, QObject *parent)
: QObject(*new QSystemTrayIconPrivate(), parent)
{
setIcon(icon);
}
所以有什么我遗漏的吗?还是有另一种方法可以简单地显示带有自定义图标的通知消息?
您可以尝试(不确定它是否适用于系统托盘)按照 this 答案中的描述进行操作并覆盖 SP_MessageBoxWarning
/ SP_MessageBoxCritical
/ SP_MessageBoxInformation
图标,但正如我所说,我不确定系统托盘是否只使用缩小版本的消息框图标,或者系统托盘图标是否是单独的。在后者的情况下,我猜你将不得不修补 QT 源,也许添加一个新项目到 QSystemTrayIcon
并修补开关以调用你提供的一些函数到 return 所需的图标。类似于:
void QSystemTrayIconPrivate::showMessage_sys(const QString &message,
const QString &title,
QSystemTrayIcon::MessageIcon icon,
int msecs)
{
if (!qpa_sys)
return;
QIcon notificationIcon;
switch (icon) {
case QSystemTrayIcon::Information:
notificationIcon = QApplication::style()- >standardIcon(QStyle::SP_MessageBoxInformation);
break;
case QSystemTrayIcon::Warning:
notificationIcon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning);
break;
case QSystemTrayIcon::Critical:
notificationIcon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxCritical);
break;
case QSystemTrayIcon::Custom:
// Call a function that will fetch the needed icon and assign it to notificationIcon
break;
default:
break;
}
qpa_sys->showMessage(message, title, notificationIcon,
static_cast<QPlatformSystemTrayIcon::MessageIcon>(icon), msecs);
}