QTableWidget内容不刷新_解决方法

现象

使用 QTableWidget 时会发现,当外部代码去更新表中的数据,却发现界面没有自动更新。
即使在更新数据之后添加了ui->tableWidget->repaint();或者ui->tableWidget->update(),界面依然没有自动更新。
只有当鼠标点击界面时,数据才被刷新。

解决方法

使用如下代码:

ui->tableWidget->viewport()->update();

原因

update()是更新这个控件
viewport()->update()是更新这个控件里面的内容

进一步分析

QTableWidget Class 官网的资料如下
http://doc.qt.io/qt-5/qtablewidget.html
顺着继承关系,一步步往上找,会发现如下说明

Note: If you inherit QAbstractItemView and intend to update the contents of the viewport, you should use viewport->update() instead of update() as all painting operations take place on the viewport.



参考资料

[SOLVED] QTableWidget is not getting refreshed automatically
https://forum.qt.io/topic/10090/solved-qtablewidget-is-not-getting-refreshed-automatically

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。