Reputation: 1193
so I wrote some Qt5 application and wanted to add a system tray icon with QSystemTrayIcon. After being done with coding I compiled and ran it and a system tray icon did not appear, so I tested around, added a line so it would display a info bubble, which it did, but in the top left corner, instead of in the system tray.
Anyway I tried Qt4.8, compiled the same code and there it worked just fine.
I'm using Arch linux with latest updates, XFCE4 as DE and Qt 5.3.0-3. I made the application with Qt Creator.
I wrote an example application which has the same behaviour. So here's the code:
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();
}
Thanks in advance!
Upvotes: 4
Views: 3270
Reputation: 4411
it's a regression (bug) on Qt 5.x versions. Here is the bug record for it https://bugreports.qt.io/browse/QTBUG-31762 it's still open :(
You can try gtk code in your Qt app to create a working system tray icon/menu.
Upvotes: 3
Reputation: 173
The code looks solid to me, but it looks like there are problems with QSystemTrayIcon in certain Linux distros. Check out this link for more info: http://www.qtcentre.org/threads/56459-QSystemTrayIcon-and-linux
Upvotes: 0