Qt QSystemTrayIcon does not send an activated signal

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

Everything seems to work, except that the QSystemTrayIcon object does not send an activation signal.

Here is 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 am using Qt 4.5.2 on Windows XP SP2. Could this be a problem with Windows XP? Or am I doing something wrong? I don't have a QIcon set for trayIcon. This is problem?

Any help would be appreciated.

Thanks! Jieren

+3
source share
1 answer

Well, if anyone is interested, I found a problem. The problem was 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 . .

+4

Source: https://habr.com/ru/post/1711409/


All Articles