高效地更新QTableView [英] Efficiently updating a QTableView at high speed

查看:327
本文介绍了高效地更新QTableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有QItemDelegate子类的QTableView来控制表视图单元格的外观.

I'm using a QTableView with a subclass of QItemDelegate to control the look and feel of the tableview's cells.

每个单元格显示一个外部连接设备的名称和状态,一次最多可以连接100个设备.

Each cell displays the name and status of a of an externally connected device, and as many as 100 devices may be connected at once.

每个设备的名称和类型本质上是静态的,很少更新(也许每小时更新一次),但是每个单元格需要显示设备输入的实时值,我目前每50毫秒轮询一次.此值显示为TableView提供给Delegate :: paint()方法的绘画者绘制的基本条形图.

The name and type of each device is essentially static, updating very rarely (perhaps once an hour), but each cell needs to display a real time value of the device's input, which I currently poll for every 50 milliseconds. This value is displayed as a basic bar graph drawn by the painter provided to the Delegate::paint() method by the TableView.

每秒更新我的模型20次的问题是每次都重新绘制整个表,这是非常低效的.将绘画方法限制为仅绘制条形图,表明大多数CPU时间专用于在每个单元格而不是图形上绘制名称,状态和关联的图像.

The problem with updating my model 20 times per second is that the entire table is redrawn each time, which is highly inefficient. Limiting the paint method to only drawing the bar graph shows that the majority of CPU time is dedicated to drawing the name, status and associated image on each cell, rather than the graph.

我需要找到的是一种定期更新每个单元格的图而不重绘该单元格的方法,但是我不知道该怎么做.

What I need to find is a way to update the graph for each cell regularly without redrawing the cell, but I can't work out how to do it.

最有效的方法是什么?

附加到帮助的图片.

图像代表QTableView中的10个传感器.数字,名称和状态实际上是静态的,几乎从不更新. 传感器值"文本旁边的条形图每50毫秒更新一次.我只想绘制此栏,而不是文本,状态和单元格背景.状态灯和背景是复杂的图像,因此比简单地绘制和填充矩形要花费更多的CPU时间.

Image represents 10 sensors in a QTableView. The Number, Name and Status are virtually static, almost never updating. The bar graph next to the 'Sensor Value' text updates every 50ms. I only want to paint this bar, rather than the text, status and the cell background. The status lights and background are complex images, so take much more CPU time than simply drawing and filling a rect.

推荐答案

由于您的QTableView继承了QWidget,因此可以在其上调用以下内容:

since your QTableView inherits QWidget, you can call the following on it:

setUpdatesEnabled(false);
changeAllYourData();
setUpdatesEnabled(true);

当setUpdatesEnabled为false时,对其进行的任何paint()或update()调用均无效.因此,您可以停止对其进行更新,更改所有数据,然后重新启用它,这可能是通过对其手动调用paint()或update()来实现的,对此我不确定.

When setUpdatesEnabled is false, any paint() or update() call on it has no effect. So, you could stop it from updating, change all your data and then reenable it, possibly by manually calling paint() or update() manually on it, I'm not sure about this part.

这是setUpdatesEnabled方法的文档.

Here is the doc for the setUpdatesEnabled method.

QWidget updatesEnabled

希望这会有所帮助.

在用户发表评论后进行

您可以通过在执行原始paint()或update()之前测试标志来为QItemDelegate子类实现自己的setUpdatesEnabled(bool)(因为它不继承QWidget且没有一个). 之后,您可以为QTableView的每个单元格(或行或列)指定是否必须更新或重新绘制.

You could implement your own setUpdatesEnabled(bool) for your QItemDelegate subclass (since it does not inherit QWidget and does not have one) by testing a flag before executing your original paint() or update(). After that, you could specify for every cell (or row or column) of your QTableView if they must be updated or repainted.

这样做,您可以阻止其他单元格(代理)重新绘制,除非您更改了手动创建的setUpdatesEnabled标志,但是将更新保留在包含图形的单元格上.

By doing this, you could stop your other cells (delegates) from repainting, unless you change the setUpdatesEnabled flag you created manually, but keep the updates on your cells containing a graph.

我必须说我从未测试过或类似的东西,所以我希望它能按照我认为的方式工作.

I must say I never tested this or anything like this, so I hope it works the way I think it does.

祝你好运

在我之前的评论之后,您可以为每个委托设置一个标志以仅绘制图形或整个图像,而不是为每个单元格设置标志(我认为您的图形位于单独的单元格中).

Following my previous comment, instead of setting a flag for every cell (I thought your graph was in a separate cell), you could set a flag for every delegate to paint only your graph or the whole image.

希望这会有所帮助,

我偶然发现了Qt 4.7中的一项新功能(我不知道您是否可以使用它,但是它可以解决您的一些问题.) 该功能是QStaticText.该类使您可以缓存文本(字体和效果)并更快地绘制它们.请参阅链接此处.

I stumbled upon a new feature in Qt 4.7 (I do not know if it's possible for you to use it, but it could solve some of your problems.) The feature is QStaticText. It is a class that allows you to cache text (font and effects) and paint them faster. See the link here.

希望它可以解决您的问题.

Hope it could solve your problem.

这篇关于高效地更新QTableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆