我正在开发一个用于数据采集和绘图的应用程序,目标刷新速率为60 and。
我使用带有4列的QTreeWidget来保存信号信息。根据每个信号的当前值,相关的QTreeWidgetItem需要更新字体颜色和背景。只有当QTreeWidgetItem可见时,才会执行此更新。
更新发生时有一个明显的冻结(GIF中有32个可见信号),我已经确定冻结是由以下代码引起的:
if new_background_color is None:
if self._background_color != self.background(0).color():
self.setBackground(0, self._background_color)
self.setBackground(1, self._background_color)
self.setBackground(2, self._background_color)
self.setBackground(3, self._background_color)
else:
if new_background_color != self.background(0).color():
self.setBackground(0, new_background_color)
self.setBackground(1, new_background_color)
self.setBackground(2, new_background_color)
self.setBackground(3, new_background_color)
if new_font_color is None:
if self.signal.color != self.foreground(0).color():
self.setForeground(0, self.signal.color)
self.setForeground(1, self.signal.color)
self.setForeground(2, self.signal.color)
self.setForeground(3, self.signal.color)
else:
if new_font_color != self.foreground(0).color():
self.setForeground(0, new_font_color)
self.setForeground(1, new_font_color)
self.setForeground(2, new_font_color)
self.setForeground(3, new_font_color)

作为参考,当我禁用上面的代码片段时,图是不稳定的。

为什么这些功能这么贵?有没有更快的替代方案?
发布于 2022-04-15 19:17:20
实际发生的情况是,QTreeWidget.itemChanged信号连接到一个只应该处理检查状态更改的函数,但是前景和背景更改也被以相同的方式处理,导致浪费CPU周期。
https://stackoverflow.com/questions/71881506
复制相似问题