【发布时间】:2012-03-22 13:29:51
【问题描述】:
我想在Qt 中实现一个动态变化的性能托盘图标。
但是我似乎在谷歌上找不到任何相关链接,所以你知道如何做到这一点吗?
如果你不知道我在问什么,我已经创建了一个 gif 文件,你会在其中得到我的想法。
因此,任何链接、代码、示例都值得赞赏。 http://gifninja.com/animatedgifs/715636/icon.gif
编辑
所以我想出了一些代码,但它不起作用,你能看看吗?
mainwindow.h
QPixmap test;
QSystemTrayIcon *speedPerformance;
mainwindow.cpp
然后在主窗口构造函数中我有:
this->test = QPixmap(16,16);
那我把这段代码叫做:
QTimer *trayIconTimer = new QTimer(this);
connect(trayIconTimer , SIGNAL(timeout()), this, SLOT(updateSpeedIcon()));
trayIconTimer->start(2000); // update it every 2 seconds
然后我创建托盘图标
speedPerformance = new QSystemTrayIcon(this);
QPainter p(&test);
QColor color;
p.fillRect(0,0,16,16,color.black());
p.end();
speedPerformance->setIcon(QIcon(test));
最后,这里是updateSpeeIcon()的代码,每2秒调用一次:
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));
所以,除了当我尝试通过单击已安装的其他托盘图标退出程序时这段代码给我分段错误之外,生成的托盘图标是 16x16 黑色正方形,而且我从来没有那些红色填充的矩形我在画,你知道有什么问题吗?
【问题讨论】: