gpt4 book ai didi

QT Systray 图标出现在 Ubuntu 上的启动器旁边而不是面板上

转载 作者:太空宇宙 更新时间:2023-11-03 16:43:58 24 4
gpt4 key购买 nike

我是 QT 的新手,需要构建一个带有应用指示器的应用。由于 QT 似乎比 GTK+ 更容易,所以我在 QT 中实现。

我会提到我安装了 sni-qt 并且 vlc 和 skype 的应用程序指示器在面板上显示正常。我在 Ubuntu 13.04 64 位上使用 QT5。

我一步步跟着这个教程:http://qt-project.org/doc/qt-4.8/desktop-systray.html

但是当我运行它时,它是这样显示的(十字是我使用的图标):

http://i.stack.imgur.com/4bT33.png

我该如何解决这个问题?

最佳答案

恐怕 sni-qt 目前不支持 Qt5,所以您要么等待支持它的新版本,要么使用 gtk+ 和 libappindicator 使用 this guide 对其进行编码.甚至还有各种语言的例子。 Since Qt5 also distributes GLib events这使得集成更加容易。首先,您需要确定您是否在 Unity 上运行(以支持更多的桌面,而不仅仅是 unity),您可以通过检索 XDG_CURRENT_DESKTOP 环境变量来做到这一点,如果它返回 Unity,则创建 appindicator,否则创建 QSystemTrayIcon。

首先您需要包含所需的 header :

#undefine signals                                                  
extern "C" {
#include <libappindicator/app-indicator.h>
#include <gtk/gtk.h>
}
#define signals public

由于 app-indicator 直接使用“signals”名称,我们需要取消定义默认的 Qt“关键字”sig​​nals,它通常会转换为 public。然后,由于我们正在编写 C++ 代码,而 libappindicator 是用 C 编写的,我们需要使用 extern "C"而不是使用 C++ 名称修改。

接下来根据我们所在的桌面创建 AppIndicator/QSystemTrayIcon:

QString desktop;
bool is_unity;

desktop = getenv("XDG_CURRENT_DESKTOP");
is_unity = (desktop.toLower() == "unity");

if (is_unity) {
AppIndicator *indicator;
GtkWidget *menu, *item;

menu = gtk_menu_new();

item = gtk_menu_item_new_with_label("Quit");
gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
g_signal_connect(item, "activate",
G_CALLBACK(quitIndicator), qApp); // We cannot connect
// gtk signal and qt slot so we need to create proxy
// function later on, we pass qApp pointer as an argument.
// This is useful when we need to call signals on "this"
//object so external function can access current object
gtk_widget_show(item);

indicator = app_indicator_new(
"unique-application-name",
"indicator-messages",
APP_INDICATOR_CATEGORY_APPLICATION_STATUS
);

app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
app_indicator_set_menu(indicator, GTK_MENU(menu));
} else {
QSystemTrayIcon *icon;
QMenu *m = new QMenu();

m->addAction(tr("Quit"), qApp, SLOT(quit()));
}

最后我们创建代理函数来从它调用 Qt 信号,声明我们需要使用 extern "C"的函数,这样就不会有任何未定义的行为。

extern "C" {                                                                    
void quitIndicator(GtkMenu *, gpointer);
}

现在代理函数:

void quitIndicator(GtkMenu *menu, gpointer data) {                                    
Q_UNUSED(menu);
QApplication *self = static_cast<QApplication *>(data);

self->quit();
}

关于QT Systray 图标出现在 Ubuntu 上的启动器旁边而不是面板上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17193307/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com