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



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似乎与QSystemTray IconPrivate紧密相连。QSystemTrayIconPrivate的实例仅在QSystemTray Icon构造函数中创建(如果我计划创建同时继承QSystemTrayTayIcon和QSystemTrayConPrivate并重新实现showMessage函数的类,则我无法真正更改该构造函数):

QSystemTrayIcon::QSystemTrayIcon(QObject *parent)
: QObject(*new QSystemTrayIconPrivate(), parent)
{
}
QSystemTrayIcon::QSystemTrayIcon(const QIcon &icon, QObject *parent)
: QObject(*new QSystemTrayIconPrivate(), parent)
{
    setIcon(icon);
}

那么我有什么遗漏吗?或者有没有其他方法可以简单地显示带有自定义图标的通知消息?

您可以尝试(不确定它是否适用于系统托盘)按照本答案中的描述执行相同操作,并覆盖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);
}

相关内容

  • 没有找到相关文章

最新更新