我想在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 黑色正方形,并且从来没有我认为的那些红色填充矩形我正在尝试画画,你知道可能会出现什么问题吗?
最佳答案
一个可能的解决方案是使用 QTimer
。您必须连接 timeout
用一个插槽发出信号,您将在其中更新图标。
QTimer *trayIconTimer = new QTimer(this);
connect(trayIconTimer , SIGNAL(timeout()), this, SLOT(updateTrayIcon()));
timer->start(2000); // update it every 2 seconds
在您的插槽中,您将创建新图标并设置它:
voi updateTrayIcon()
{
QIcon newIcon = CreateIcon();
// I assume that tray is a pointer to the `QSystemTrayIcon`
tray->setIcon(newIcon);
}
关于c++ - 动态改变图标[QT/c++],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9823276/