KDE任务栏进度

4
我正在尝试使用KDE框架在Plasma桌面的任务栏中展示进度。简单来说,我想要做的就像Dolphin一样,当它复制文件时:

enter image description here

我有些困惑,因为我甚至不知道该如何入手。我找到的唯一有用的东西是KStatusBarJobTracker,但我不知道该如何使用它。我没有找到任何关于如何实现这个目标的教程或例子。

2个回答

10
所以,在深入研究并得到@leinir的帮助后,我发现以下内容:
  • 自从Plasma 5.6 KDE支持Unitiy DBus Launcher-API后,可以使用它来显示进度等
  • 我在AskUbuntu上找到了一篇文章,介绍了如何在Qt中使用该API

真正的问题是:这仅在您拥有标准位置之一的有效桌面文件时才起作用!您需要将文件作为DBus消息的参数传递才能使其起作用。

基于这些信息,我找出了如何使用它,并创建了一个GitHub存储库,支持跨平台任务栏进度,并在linux实现中使用了此API。

无论如何,以下是如何操作。它应该适用于KDE Plasma和Unity桌面,可能还有其他桌面(我没有尝试过):

  1. Create a .desktop file for your application. For test purpose, this can be a "dummy" file, that could look like this:

    [Desktop Entry]
    Type=Application
    Version=1.1
    Name=MyApp
    Exec=<path_to>/MyApp
    
  2. Copy that file to ~/.local/share/applications/ (or wherever user specific desktop files go on your system)

  3. In your code, all you need to do is execute the following code, to update the taskbar state:

    auto message = QDBusMessage::createSignal(QStringLiteral("/com/example/MyApp"),
                                              QStringLiteral("com.canonical.Unity.LauncherEntry"),
                                              QStringLiteral("Update"));
    
    //you don't always have to specify all parameters, just the ones you want to update
    QVariantMap properties;
    properties.insert(QStringLiteral("progress-visible"), true);// enable the progress
    properties.insert(QStringLiteral("progress"), 0.5);// set the progress value (from 0.0 to 1.0)
    properties.insert(QStringLiteral("count-visible"), true);// display the "counter badge"
    properties.insert(QStringLiteral("count"), 42);// set the counter value
    
    message << QStringLiteral("application://myapp.desktop") //assuming you named the desktop file "myapp.desktop"
            << properties;
    QDBusConnection::sessionBus().send(message);
    
  4. Compile and run your application. You don't have to start it via the desktop file, at least I did not need to. If you want to be sure your application is "connected" to that desktop file, just set a custom icon for the file. Your application should show that icon in the taskbar.

基本上就是这样。 注意: 当重新启动应用程序时,系统会记住上次的状态。因此,在启动应用程序时,您应该重置所有这些参数。


你知道如何从 bash 脚本中实现这个吗?我对 Qt 毫不熟悉。 - MoralCode

2

没错,事实证明你是正确的,目前没有这方面的教程。然而,这个Reviewboard请求展示了它是如何在KDevelop中实现的,你应该可以通过那个找到解决方法 :) https://git.reviewboard.kde.org/r/127050/

ps: 目前没有教程可能是一个很好的机会,让你参与进来并提供一些小型、自包含的教程... 我相信这将非常受欢迎 :)


我可能会为此创建一个 Github 存储库。 - Felix
但是,我认为这仅适用于Ubuntu Unity桌面?我使用的是KDE Plasma桌面。 - Felix
我已经解决了!明天我会发布我的答案,展示“完整”的解决方案。 - Felix
非常出色的呼叫,我想转达 Eike Hein 的感谢(您可能知道他是那个多年来一直维护旧堆栈并在 Plasma 5 中重写整个任务栏子系统的人)! :) - leinir

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接