Reputation: 936
I would like to implement a dynamically changing performance tray icon in Qt
.
However I can't seem to find any relevant link on google, so do you have any idea how to do this?
If you don't know what I'm asking for, I've created a gif file, where you'll get my idea.
So any links,codes,examples are appreciated. http://gifninja.com/animatedgifs/715636/icon.gif
EDIT
So I come up with some code, but it's not working, could you please have a look at it?
mainwindow.h
QPixmap test;
QSystemTrayIcon *speedPerformance;
mainwindow.cpp
then in mainwindow constructor I have:
this->test = QPixmap(16,16);
then I call this piece of code:
QTimer *trayIconTimer = new QTimer(this);
connect(trayIconTimer , SIGNAL(timeout()), this, SLOT(updateSpeedIcon()));
trayIconTimer->start(2000); // update it every 2 seconds
then I create the trayicon
speedPerformance = new QSystemTrayIcon(this);
QPainter p(&test);
QColor color;
p.fillRect(0,0,16,16,color.black());
p.end();
speedPerformance->setIcon(QIcon(test));
and finally, here is the code of updateSpeeIcon()
, which is called every 2 seconds:
QPainter p(&test);
QColor color;
p.setPen(color.red());
xPaint+=3;
qDebug() << xPaint;
p.fillRect(xPaint,0,2,16,color.red());
p.end();
speedPerformance->setIcon(QIcon(test));
so, besides the fact that this code gives me segmentation fault when I try to quit the program by clicking on other trayicon which is installed, the resultant tray icon is 16x16 black square, and there are never those red filled rectangle that I'm trying to draw, do you know what can be wrong?
Upvotes: 2
Views: 2914
Reputation: 1202
If i understood your question correctly, these are the steps that you should follow;
inside your application class,
create a icon variable
create a dataSource variable
inside constructor()
icon = DEFAULT_ICON
connect(dataSource, SIGNAL(performanceChanged()),
this, SLOT(updateIcon()));
inside updateIcon()
var = dataSource.getPerformanceLevel();
switch(var)
case LEVEL_1:
icon = getLevel1Icon()
case LEVEL_2:
icon = getLevel2Icon()
....
hope this helps...
Upvotes: 0
Reputation: 12321
A possible solution is to use a QTimer
. You have to connect the timeout
signal with a slot where you will update the icon.
QTimer *trayIconTimer = new QTimer(this);
connect(trayIconTimer , SIGNAL(timeout()), this, SLOT(updateTrayIcon()));
timer->start(2000); // update it every 2 seconds
In your slot you will create the new icon and set it:
voi updateTrayIcon()
{
QIcon newIcon = CreateIcon();
// I assume that tray is a pointer to the `QSystemTrayIcon`
tray->setIcon(newIcon);
}
Upvotes: 4
Reputation: 90736
Since you have a tray icon, you must have used the QSystemTrayIcon
class already. With this class you can change the tray icon at any time. Simply call QSystemTrayIcon::setIcon()
.
Upvotes: 1