所以我写了一些 Qt5 应用程序,想用 QSystemTrayIcon 添加一个系统托盘图标。完成编码后,我编译并运行了它,系统托盘图标没有出现,所以我测试了一下,添加了一行,这样它就会显示一个信息气泡,它确实显示了,但在左上角,而不是在系统托盘。
无论如何,我尝试了 Qt4.8,编译了相同的代码并且在那里工作得很好。
我正在使用带有最新更新的 Arch Linux,XFCE4 作为 DE 和 Qt 5.3.0-3。我使用 Qt Creator 制作了应用程序。
我编写了一个具有相同行为的示例应用程序。 所以这是代码:
主窗口.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSystemTrayIcon>
#include <QIcon>
#include <QMenu>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
QSystemTrayIcon *trayIcon;
QMenu *trayMenu;
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
主窗口.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
trayMenu = new QMenu(this);
trayMenu->addAction("Test");
trayIcon = new QSystemTrayIcon(this);
trayIcon->setIcon(QIcon(":/ui_conf.png"));
trayIcon->setContextMenu(trayMenu);
trayIcon->show();
trayIcon->showMessage("Well...", "Here I should be I guess?");
ui->pushButton->setIcon(QIcon(":/ui_conf.png"));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
this->close();
}
主要.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
提前致谢!
最佳答案
这是 Qt 5.x 版本的回归(错误)。这是它的错误记录https://bugreports.qt.io/browse/QTBUG-31762它仍然是开放的:(
您可以在您的 Qt 应用程序中尝试使用 gtk 代码来创建一个可用的系统托盘图标/菜单。
关于c++ - Qt 5.3 QSystemTrayIcon 无法正常工作[Linux],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24373967/