简体   繁体   English

如何在C ++程序中创建KDE托盘通知?

[英]How to create KDE tray notification in a C++ program?

I have a C++ program (that uses Qt), and I want to create a tray notification with a progress bar. 我有一个C ++程序(使用Qt),我想用进度条创建一个托盘通知。 I think Qt can show text notifications, but not progress bar - that's KDE-specific. 我认为Qt可以显示文本通知,但不会显示进度条 - 这是特定于KDE的。 I can't find any example of managing KDE tray notifications in C++. 我找不到在C ++中管理KDE托盘通知的任何示例。 Any advice? 有什么建议?

PS Here's what I mean: PS这就是我的意思:

在此输入图像描述

I was wondering the same thing, and I spent some time investigating. 我想知道同样的事情,我花了一些时间调查。 Here are the results: 结果如下:

Introduction 介绍

That progress bar is a special feature of kde that's not linked to libnotify at all. 进度条是kde的一个特殊功能,它根本没有链接到libnotify。 It is provided by a component called kuiserver, and it is meant to display progress of file transfert, though it can be used for other things. 它由一个名为kuiserver的组件提供,它用于显示文件传输的进度,尽管它可以用于其他事情。

There are two ways 有两种方法

KJob KJob

The first way is to talk to kuiserver in the KDE API, and it's by using KUiServerJobTracker with a KJob . 第一种方法是在KDE API中与kuiserver交谈,并使用KUiServerJobTrackerKJob I haven't tried myself, but I guess you should subclass KJob, and pass an instance of your subclass to KUiServerJobTracker. 我没有尝试过自己,但我想你应该继承KJob,并将你的子类实例传递给KUiServerJobTracker。

dbus DBUS

You can also talk directly to the kuiserver through dbus, which is what I tried. 您也可以通过dbus直接与kuiserver对话,这是我尝试过的。 I used qdbusviewer to explore the API provided by kuiserver. 我使用qdbusviewer来探索kuiserver提供的API。 I wanted to show an example of interaction with the command-line tool qdbus , but I couldn't get it to work. 我想展示一个与命令行工具qdbus交互的例子,但我无法让它工作。 If you plan to do this, you probably should use the QDBus library. 如果您打算这样做,您可能应该使用QDBus库。

What is interesting to us is the /JobViewServer path in the org.kde.kuiserver service. 我们感兴趣的是org.kde.kuiserver服务中的/JobViewServer路径。 The method we want to call is org.kde.JobViewServer.requestView , which creates a new dbus path for this job, and returns it. 我们要调用的方法是org.kde.JobViewServer.requestView ,它为此作业创建一个新的dbus路径,并返回它。 For example, I just called requestView with dummy arguments, and got ObjectPath: /JobViewServer/JobView_29 in return. 例如,我刚用伪参数调用requestView ,并获得了ObjectPath: /JobViewServer/JobView_29作为回报。 In the meantime, a progress indicator has just started rotating in my tray: 与此同时,进度指示器刚刚开始在托盘中旋转:

I can now use the content of that path. 我现在可以使用该路径的内容。 Here, qdbus worked, so here is the list of the methods: 这里, qdbus工作,所以这里是方法的列表:

$ qdbus org.kde.kuiserver /JobViewServer/JobView_29
signal void org.kde.JobViewV2.cancelRequested()
method Q_NOREPLY void org.kde.JobViewV2.clearDescriptionField(uint number)
signal void org.kde.JobViewV2.resumeRequested()
method bool org.kde.JobViewV2.setDescriptionField(uint number, QString name, QString value)
method Q_NOREPLY void org.kde.JobViewV2.setDestUrl(QDBusVariant destUrl)
method Q_NOREPLY void org.kde.JobViewV2.setInfoMessage(QString message)
method Q_NOREPLY void org.kde.JobViewV2.setPercent(uint percent)
method Q_NOREPLY void org.kde.JobViewV2.setProcessedAmount(qulonglong amount, QString unit)
method Q_NOREPLY void org.kde.JobViewV2.setSpeed(qulonglong bytesPerSecond)
method Q_NOREPLY void org.kde.JobViewV2.setSuspended(bool suspended)
method Q_NOREPLY void org.kde.JobViewV2.setTotalAmount(qulonglong amount, QString unit)
signal void org.kde.JobViewV2.suspendRequested()
method Q_NOREPLY void org.kde.JobViewV2.terminate(QString errorMessage)
method QDBusVariant org.freedesktop.DBus.Properties.Get(QString interface_name, QString property_name)
method QVariantMap org.freedesktop.DBus.Properties.GetAll(QString interface_name)
method void org.freedesktop.DBus.Properties.Set(QString interface_name, QString property_name, QDBusVariant value)
method QString org.freedesktop.DBus.Introspectable.Introspect()
method QString org.freedesktop.DBus.Peer.GetMachineId()
method void org.freedesktop.DBus.Peer.Ping()

I'll let you guess and experiment to find what those do, but here is a small example: 我会让你猜测并尝试找到那些做的,但这是一个小例子:

# Give it a name, in case we open the tooltip
qdbus org.kde.kuiserver /JobViewServer/JobView_29 setInfoMessage "Example progress"
# Update the progress
qdbus org.kde.kuiserver /JobViewServer/JobView_29 setPercent 50
# End it
qdbus org.kde.kuiserver /JobViewServer/JobView_29 terminate "Some error message"

Conclusion 结论

You should have enough to create a progress bar in the tray with kde. 您应该有足够的能力使用kde在托盘中创建进度条。 I hope this'll be useful to you. 我希望这对你有用。 Anyway, I had fun finding this out (including diving in some kde applications code to find out how the progress notification was done), and I'll probably write something that uses this. 无论如何,我很高兴找到这个(包括潜入一些kde应用程序代码以了解进度通知是如何完成的),我可能会写一些使用它的东西。

You can use QxtToolTip which is a class in Qxt . 您可以使用QxtToolTip这是一类QXT It enabales you to show any arbitrary widget as a tooltip. 它使您可以将任意窗口小部件显示为工具提示。 So i think you can create your custom widget and show it using QxtToolTip in the proper position. 所以我认为你可以创建自定义小部件并使用QxtToolTip在适当的位置显示它。 You can use this static function: 你可以使用这个静态函数:

void QxtToolTip::show ( const QPoint & pos, QWidget * tooltip, QWidget * parent = 0, const QRect & rect = QRect() )   [static]

It can be like: 它可以像:

#include <QxtToolTip>

MyCustomWidget widget;
QPoint myPosition(x,y);
QxtToolTip::show ( &myPosition, &widget, parent);

要进入系统托盘,您需要使用KStatusNotifierItem,并且您需要查看KNotifications框架的通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM