Qt 带有自定义图标的QSystemTrayIcon通知消息

Qt 带有自定义图标的QSystemTrayIcon通知消息,qt,qt5,system-tray,Qt,Qt5,System Tray,QSystemTrayIcon具有以下功能: void showMessage(常量字符串和标题,常量字符串和消息, MessageIcon图标=信息,整数毫秒=10000) 是否有办法将其更改为自定义图标,例如- void showIconMessage(常量字符串和标题、常量字符串和消息、, QIcon图标=QIcon(),整数毫秒=10000) 不修改Qt源 我知道showMessage(d是QSystemTrayIconPrivate的实例,用Q\u d(QSystemTrayIcon)

QSystemTrayIcon具有以下功能:

void showMessage(常量字符串和标题,常量字符串和消息,
MessageIcon图标=信息,整数毫秒=10000)

是否有办法将其更改为自定义图标,例如-

void showIconMessage(常量字符串和标题、常量字符串和消息、,
QIcon图标=QIcon(),整数毫秒=10000)

不修改Qt源

我知道
showMessage
(d是QSystemTrayIconPrivate的实例,用
Q\u d(QSystemTrayIcon)
宏调用)

QSystemTrayIconPrivate
调用
showMessage\u 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);
}
那我还缺什么吗?或者有没有其他方法可以简单地用自定义图标显示通知消息?

您可以尝试(不确定它是否适用于系统托盘)执行与应答中所述相同的操作,并覆盖
SP_MessageBoxWarning
/
SP_MessageBoxCritical
/
SP_MessageBoxInformation
图标,但正如我所说,我不确定系统托盘是否只是使用了缩小版的消息框图标,或者系统托盘图标是否是分开的。在后一种情况下,我想您必须修补QT源代码,可能需要在
QSystemTrayIcon
中添加一个新项目,并修补开关以调用您提供的一些函数来返回所需的图标。比如:

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);
}
void QSystemTrayIconPrivate::showMessage_sys(const QString&message,
常量字符串和标题,
QSystemTrayIcon::MessageIcon图标,
整数(毫秒)
{
如果(!qpa_sys)
返回;
QIcon通知图标;
开关(图标){
案例QSystemTrayIcon::信息:
notificationIcon=QApplication::style()->standardIcon(QStyle::SP_MessageBoxInformation);
打破
案例QSystemTrayIcon::警告:
notificationIcon=QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning);
打破
案例QSystemTrayIcon::关键:
notificationIcon=QApplication::style()->standardIcon(QStyle::SP_MessageBoxCritical);
打破
案例QSystemTrayIcon::自定义:
//调用一个函数,该函数将获取所需的图标并将其分配给notificationIcon
打破
违约:
打破
}
qpa_sys->showMessage(消息、标题、通知图标、,
静态广播(图标),毫秒;
}

谢谢您的回答,但我明白我必须做哪些更改才能使用定制QIcon实现功能。整个问题是-如何在不改变Qt来源的情况下制作它。现在我正在考虑继承QSystemTrayIcon(例如MyTrayIcon)并完全重新实现QSystemTrayIconPrivate(例如使用MyTrayIconPrivate制作它的副本),并尝试在这两个类中使用新的额外函数使它们以某种方式工作。但我正在与Q_D和所有Qt的D指针系统抗争-@Shf你检查了其他问题的链接了吗?如果这样做有效,你就不必修补源代码。唯一的问题是覆盖的图标是否与用于系统托盘的图标相同。谢谢,我想,这正是我需要的。将立即尝试使用
QProxyStyle
方法,并设置为
SP_MessageBoxInformation
,但控件从未接触到
standardIcon
QIcon standardIcon(StandardPixmap standardIcon,const QStyleOption*option=0,const QWidget*widget=0)const{//如果(standardIcon==QStyle::SP_MessageBoxInformation){return QIcon(“myicon”);}
@Shf您是否如答案所示将样式应用于您的小部件?如果是这样,那么我很遗憾地猜测QT不允许自定义该图标。
QSystemTrayIcon::QSystemTrayIcon(QObject *parent)
: QObject(*new QSystemTrayIconPrivate(), parent)
{
}

QSystemTrayIcon::QSystemTrayIcon(const QIcon &icon, QObject *parent)
: QObject(*new QSystemTrayIconPrivate(), parent)
{
    setIcon(icon);
}
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);
}