demosthenes
demosthenes

Reputation: 1191

Qt Systray icon cannot be implemented

i have a window with a Pushbutton that hides the window to system tray when it is pushed.

The problem is that no system tray icon is shown. Here is the code, what i do wrong?

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSystemTrayIcon>
#include <QString>
#include <QPixmap>
#include <QIcon>
#include <QAction>
#include <QMenu>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //setup tray icon
    QSystemTrayIcon *tray;
    QPixmap icon(":/pencil.png");
    QIcon trayIcon;

    tray= new QSystemTrayIcon(this);
    trayIcon= QIcon(icon);
    tray->setIcon(trayIcon);
    tray->setToolTip("ToolTip");

    //setup restore
    QAction *restoreAction;
    restoreAction = new QAction(QIcon(":/pencil.png"), "Restore", this);
    connect(restoreAction, SIGNAL(triggered()), this, SLOT(show()));

    QMenu *trayIconMenu;
    trayIconMenu = new QMenu(this);
    trayIconMenu->addAction(restoreAction);
    tray->setContextMenu(trayIconMenu);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    this->hide();
}

I press the button the window is being hidden but no systray icon show up. Why?

Upvotes: 0

Views: 117

Answers (1)

demosthenes
demosthenes

Reputation: 1191

well i found it....

i had to set visible to true tray icon

trayIconMenu = new QMenu(this);
trayIconMenu->addAction(restoreAction);
tray->setContextMenu(trayIconMenu);

tray->setVisible(true);

Upvotes: 1

Related Questions