PDA

View Full Version : QSystemTrayIcon doesnt seem to emit activated signal on icon click



mhogomchungu
13th October 2011, 04:19
i have this line in my header file to declare a qsystemtray icon

private:
QSystemTrayIcon *trayIcon ;

The implementation file has these lines to instantiate a tray class and set up a signal/slot

trayIcon = new QSystemTrayIcon(this);
trayIcon->setIcon(QIcon(QString("/usr/share/icons/zuluCrypt.png")));

signal/slot connection is made with this line just below the above two lines in the implementation file:


connect(trayIcon,SIGNAL(activated(QSystemTrayIcon: :ActivationReason)),this,SLOT(trayClicked(QSystemT rayIcon::ActivationReason)));

trayClicked function is prototyped as below in the header file:

private slots :
void trayClicked(QSystemTrayIcon::ActivationReason e) ;

the command that shows the tray is "trayIcon->show();"

Basically, i have
1. created a pointer to class Qsystemtray in the private section of the header file.
2. Instantiated a Qsystemtrayicon class and assign it to the pointer in the implementation file as shown above.
3. connect the signal generated by the tray on click to a slot on my main window class
4. The slot is prototyped in the "private slot" section of the header file.

I think i have done everything i am supposed to but the slot doesnt get called(or the signal doesnt get generated) when i click on the tray icon and i have no idea why this is happened. What am i doing wrong?

mhogomchungu
14th October 2011, 15:56
still struggling with this one.

I am using kde 4.7 on linux.

Anybody know of a simple Qt based application i can look at the code to see how they implemented the tray?

I have seen Qt tutorials on implementing the tray icon and i believe i have done everything it says but it just doesnt work for me.

entee
14th October 2011, 17:08
I made a program with QSystemTrayIcon some months ago.
Here my code. It isn't full, I simplified it.

header:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QSystemTrayIcon>
#include <QNetworkReply>
#include "mythread.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;
void createActions();
void createTrayIcon();

QAction *quitAction;
QAction *settingsAction;

QSystemTrayIcon *trayIcon;
QMenu *trayIconMenu;
public slots:
void ment();

};

code:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QMenu>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
createActions();
createTrayIcon();
ui->setupUi(this);
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(ment()));
trayIcon->show();

}

MainWindow::~MainWindow(){
mt->exit();
mt->wait();
delete ui;

}

void MainWindow::createActions(){
quitAction = new QAction(tr("&Kilepes"), this);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
settingsAction = new QAction(tr("&Beallitasok"), this);
connect(settingsAction, SIGNAL(triggered()), this, SLOT(showNormal()));
}

void MainWindow::createTrayIcon(){
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(settingsAction);
trayIconMenu->addAction(quitAction);

trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
QIcon icon(":/images/ok.svg");
trayIcon->setIcon(icon);
}

mhogomchungu
14th October 2011, 18:46
I think you forgot in your code to include the line that connect the signal generated by the tray when clicked to a slot.

what part of the header file function "createTrayIcon()" is prototyped?

Did your code for you? on what system?

All i want is to click the tray to minimize the main window and to click it again to raise the window.

The central widget of the main window is tablewidget, could it be eating up the click or something?

norobro
14th October 2011, 19:29
Do you get a warning on the command line? Like: "Object::connect: No such slot ..." Maybe it's a typo, but the reason for the question is the space in your connect statement:"trayClicked(QSystemT rayIcon::ActivationReason)"

How are you determining that the slot isn't being called?

mhogomchungu
14th October 2011, 20:27
that space was probably added by the forum when i pasted the code, its not on my code here. I am not getting any warnings on the command line, no error, no warnings.

The implementations of the "trayClicked function is below

void zuluCrypt::trayClicked(QSystemTrayIcon::Activation Reason e)
{
std::cout << "ds" << std::endl ;
if( e == QSystemTrayIcon::Trigger){

if(this->isVisible() == true)
this->hide();
else
this->show();
}
}

I am using Qt Creator and that "ds" string would have showed up in the "application output window" if i run the program from within Qt Creator if the function was called.

norobro
14th October 2011, 20:39
Works fine here:
#include <QtGui>
#include <iostream>

class MainWindow : public QMainWindow {

Q_OBJECT

public:
MainWindow(){
QTableWidget *tw = new QTableWidget(3,3,this); // added re: your question above.
setCentralWidget(tw);
QSystemTrayIcon *trayIcon = new QSystemTrayIcon(this);
trayIcon->setIcon(QIcon("../icons/googleearth-icon.png"));
trayIcon->show();
connect(trayIcon,SIGNAL(activated(QSystemTrayIcon: :ActivationReason)),this,SLOT(trayClicked(QSystemT rayIcon::ActivationReason)));
}

private slots:
void trayClicked(QSystemTrayIcon::ActivationReason e){
std::cout << "ds" << std::endl;
if( e == QSystemTrayIcon::Trigger){
if(this->isVisible() == true) this->hide();
else this->show();
}
}
};


int main(int argc, char *argv[]){
QApplication app(argc, argv);
MainWindow w;
w.show();
app.exec();
}

#include "main.moc"

norobro
14th October 2011, 22:50
Took a look at your app and your problem is that you have your mainwindow (zulucrypt) set to Qt::ApplicationModal (http://doc.qt.nokia.com/4.7-snapshot/qt.html#WindowModality-enum)

mhogomchungu
15th October 2011, 00:41
I took your code, put in qt creator, build and run it and the tray shows on the system tray but without an icon which is understandable because i dont have that icon in that path but the tray does not respond to clicks but atleast now i have an output and it says:

Object::connect: No such slot MainWindow::trayClicked(QSystemTrayIcon::Activatio nReason)

so i guess this is progress.

What tool did you use to write, build and run the code in? This could be a Qt creator 2.3.1 + Qt 4.7.4 specific issue

entee
15th October 2011, 07:38
I am using Qt Creator and that "ds" string would have showed up in the "application output window" if i run the program from within Qt Creator if the function was called.


#include <QDebug>
qDebug() << "ds";

norobro
15th October 2011, 16:17
Did you read post #8? Try changing the zuluCrypt mainwindow from ApplicationModal to WindowModal and check the behavior.


This could be a Qt creator 2.3.1 + Qt 4.7.4 specific issue Don't think so. I'm using QtCreator 2.2.1 & Qt 4.7.3 (64 bit).

Try it from the command line:
mkdir systraytest && cd systraytest
touch main.cpp
open main.cpp with your favorite editor and paste in the code. Save
qmake -project
qmake
make
./systraytest

Again, it works fine on a Debian Sid (KDE) box.

mhogomchungu
15th October 2011, 22:51
Thanks for the tip, i did not know this.



#include <QDebug>
qDebug() << "ds";

thanks for the tip, i did not know this.

Added after 7 minutes:


Did you read post #8? Try changing the zuluCrypt mainwindow from ApplicationModal to WindowModal and check the behavior.


No, i did not see post number 8 the first time.

I changed the modal to windowModal and it now works. This should have been included in the qt docs examples. It was supper frustrating to follow their examples to the latter and then still getting ignored by QsystemTray.

The problem i had didnt exist on the internet as far as mr. google is concerned so, i must be the only or first idiot to hit it :-)

Thanks you very much, i have been struggling with this for days now.

Did i already said thank you, if not, thank you very much :-)

how do i edit the title to add "solved" to it? or you guys dont do that in this forum