3

I'm trying to copy the Qt systray example here: http://doc.qt.io/archives/4.6/desktop-systray.html

Things seem to be working except that the QSystemTrayIcon object is not sending an activate signal.

Here's my mainwindow.cpp code:

#include <QtGui>

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QMessageBox::information(0, tr("Systray"),
                              tr("Loaded."));
    createTrayIcon();

    connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,
            SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));

    trayIcon->show();
}

void MainWindow::createTrayIcon()
{
    trayIcon = new QSystemTrayIcon(this);

}

void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
    QMessageBox::information(0, tr("Systray"),
                              tr("Testing"));

}

void MainWindow::messageClicked()
{
     QMessageBox::information(0, tr("Systray"),
                              tr("Sorry, I already gave what help I could.\n"
                                 "Maybe you should try asking a human?"));
 }

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

I'm using Qt 4.5.2 on Windows XP SP2. Could this be an issue with Windows XP? Or am I doing something wrong? I don't have a QIcon set for the trayIcon. Is that a problem?

Any help would be appreciated.

Thanks! Jieren

Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
Jieren
  • 1,952
  • 4
  • 18
  • 26
  • It's not clear from your description, hence I have to ask: Does the original (from Qt) system tray icon example work? – Ariya Hidayat Jun 28 '09 at 23:27
  • Just tested it. Right click does work, but double click strangely does not. But for some reason the SystemTray object isn't even sending the activated() signal. I even set a QIcon. Still nothing. Grrrrrrr. – Jieren Jun 29 '09 at 12:45

1 Answers1

4

Well if anyone's interested, I found the issue. The problem was actually in the header file.

Here's the one that works:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QWidget>
#include <QSystemTrayIcon>


class MainWindow : public QWidget
{
    Q_OBJECT

public:
    MainWindow();

private slots:
    void iconActivated(QSystemTrayIcon::ActivationReason reason);
private:
    QAction *minimizeAction;
     QAction *maximizeAction;
     QAction *restoreAction;
     QAction *quitAction;

     QSystemTrayIcon *trayIcon;
    void createActions();
    void createTrayIcon();

    void messageClicked();
};

#endif // MAINWINDOW_H

iconActivated needs to be declared as a private slot. I had it declared as a private function.

Jieren
  • 1,952
  • 4
  • 18
  • 26
  • Good to see you've solved the problem! I think you should probably close the question as it's still appearing in the unanswered list. – Troubadour Jun 29 '09 at 18:12
  • Sorry, working link is http://stackoverflow.com/questions/127588/what-is-a-closed-question-in-stackoverflow-how-do-they-work – Troubadour Jun 29 '09 at 18:13
  • 2
    Yeah sorry dude... when I first tried to close it, it told me that I had to wait for a while. Then I forgot since well... I was working on the program that this was for :-) – Jieren Jun 29 '09 at 23:30
  • You should explain what fixed your problem rather than dump a bunch of code. – notlesh Oct 04 '19 at 16:57
  • @Jieren, If I am going to make my own tray icon class, where should I put iconActivated? – Denis Turgenev Jul 08 '21 at 05:00