我写了一些Qt5应用程序,并想使用QSystemTrayIcon添加系统托盘图标。在编码完成后,我进行了编译和运行,但系统托盘图标没有出现,于是我进行了测试,添加了一行代码以显示信息气泡,它确实显示了,但是出现在左上角,而不是在系统托盘中。
无论如何,我尝试了Qt4.8,编译了相同的代码,那里它可以正常工作。
我正在使用更新的Arch Linux,XFCE4作为桌面环境,Qt 5.3.0-3。我使用Qt Creator制作了该应用程序。
我编写了一个具有相同行为的示例应用程序。以下是代码:
MainWindow.h
无论如何,我尝试了Qt4.8,编译了相同的代码,那里它可以正常工作。
我正在使用更新的Arch Linux,XFCE4作为桌面环境,Qt 5.3.0-3。我使用Qt Creator制作了该应用程序。
我编写了一个具有相同行为的示例应用程序。以下是代码:
MainWindow.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
MainWindow.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();
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
提前感谢您!