PDA

View Full Version : How to keep System tray icons



mkkguru
23rd February 2010, 07:20
Hi All,
I have to keep System tray icon when i executed my application..below is the code i written its working but the icon is not visible.



QIcon icon;
icon.addPixmap(QPixmap(QString::fromUtf8(":/images/home.png")),
QIcon::Normal, QIcon::Off);

tray.setIcon(QIcon( icon));//":/images/home.png"));
tray.setContextMenu(&cmenu);
connect(&tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason )),
this, SLOT(trayActivated(QSystemTrayIcon::ActivationReas on)));


void abc::trayActivated(QSystemTrayIcon::ActivationReas on reason)
{
switch(reason)
{
case QSystemTrayIcon::Trigger:
{
if(isHidden()) show();
else hide();
break;
}
default: break;
}
}



plz can anyone give idea about this.........

regards,
mkkguru

Lykurg
23rd February 2010, 07:34
Seems you have to create your QSystemTrayIcon on the heap!

Corinzio
23rd February 2010, 09:36
For what i see you show the icon only on response of a QSystemTrayIcon::Trigger event but that reason is sended only when "The system tray entry was clicked" but that's impossible if you haven't already showed it up with show....

mkkguru
23rd February 2010, 11:30
Seems you have to create your QSystemTrayIcon on the heap!

How to create QSystemTrayIcon on the heap??plz give me some idea.

mkkguru
23rd February 2010, 11:31
Is there any possiblility to show my icon at systemtray.........?plz give me idea regarding this..........

Lykurg
23rd February 2010, 13:18
How to create QSystemTrayIcon on the heap??plz give me some idea.

show us all your relevant code! The part you posted is confusing and "wrong". On the heap I mean:
class Foo {
//...
private:
QSystemTrayIcon *tray;
}

Foo:Foo() {
//...
tray = new QSystemTrayIcon(this);
}

Corinzio
23rd February 2010, 13:32
just after


connect(&tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason )),
this, SLOT(trayActivated(QSystemTrayIcon::ActivationReas on)));

put


tray.show()

mkkguru
24th February 2010, 10:05
just after


connect(&tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason )),
this, SLOT(trayActivated(QSystemTrayIcon::ActivationReas on)));

put


tray.show()

i did but it no use still the error is comming as below




................................
QSystemTrayIcon::setVisible: No Icon set



so how to set icon to systemtray

mkkguru
24th February 2010, 10:21
I updated the code as follows still the error coming as QSystemTrayIcon::setVisible: No Icon set


void foo ::createTrayIcon()
{
QIcon icon;
icon = QIcon(QPixmap( ":/images/new.png" ) );
tray = new QSystemTrayIcon(icon,this);
tray->setContextMenu(cmenu);
tray->setIcon(icon);
tray->setVisible(true);
setWindowIcon(icon);
tray->show();
connect(tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason )),
this, SLOT(iconActivated(QSystemTrayIcon::ActivationReas on)));
}

void foo::iconActivated(QSystemTrayIcon::ActivationReas on reason)
{

switch (reason) {
case QSystemTrayIcon::Trigger:
tray->show();
case QSystemTrayIcon::DoubleClick:
break;
case QSystemTrayIcon::MiddleClick:
// showMessage();
break;
default:
;
}


Plz give some idea regarding this.................thanks in advance.....

Corinzio
24th February 2010, 15:28
I think you should create the QIcon on the heap with new.
In your code the QIcon icon goes out of scope when the foo::createTrayIcon() functions ends...

i'm not sure....
sorry

Lykurg
24th February 2010, 16:19
I think you should create the QIcon on the heap with new.
In your code the QIcon icon goes out of scope when the foo::createTrayIcon() functions ends...

i'm not sure....
sorry
No better create it on the stack. It is copied to the system tray icon instance. Try that code in your main window:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

// BEGIN HERE
QIcon icon = QIcon("/data/misc/nuvola/32x32/apps/kcmsystem.png");
QSystemTrayIcon *tray = new QSystemTrayIcon(icon, this);
tray->show();
// END HERE
} and see if it works. Works fine on my machine. And make sure your resource system works fine and your png is actually found.