c++ - 动态改变图标[QT/c++]

标签 c++ qt dynamic icons

我想在Qt中实现一个动态变化的性能托盘图标。 但是我似乎在谷歌上找不到任何相关链接,所以你知道如何做到这一点吗?

如果您不知道我在要求什么,我创建了一个 gif 文件,您可以在其中了解我的想法。

因此,任何链接、代码、示例都值得赞赏。 http://gifninja.com/animatedgifs/715636/icon.gif

编辑

所以我想出了一些代码,但它不起作用,你能看一下吗?

ma​​inwindow.h

QPixmap test;
QSystemTrayIcon *speedPerformance;

ma​​inwindow.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/

相关文章:

javascript - JSON 与动态编码

c++ - 在来自多个线程的阻塞套接字上使用 send()

c++ - SIGSEGV 使用 gdb 时无法获取堆栈跟踪

c++ - main() 函数中的默认初始化

python - python 中使用 qml 中的元素显示的数组

c# - 如何将对象转换为不同程序集中的匿名类型?

c++ - Boost asio 中的 TCP 客户端

qt - 为什么 QWebSocketServer 在客户端连接尝试时发送 TCP [FIN] 消息

python - 如何从不能继承 QObject 的类发出信号?

c# - 如何将 List<ExpandoObject> 中的每个对象转换为自己的类型?