刷新 QWidget [英] Refreshing a QWidget

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

问题描述

这个问题我遇到过很多次了.

I've been having this issue a lot of times.

当我在 widget.show() 之后修改 QWidget 的某些属性时,该小部件不会更新.大多数情况下,鼠标单击或鼠标离开或进入小部件时,小部件都会更新.但是,如果我离开鼠标,它不会自行刷新.

When I modify some properties of a QWidget after the widget.show(), the widget won't update. Most of the time, a mouse click or when the mouse leaves or enters the widget, the widget will be updated. However, if I leave the mouse, it won't refresh by itself.

直到现在我设法通过这样做来处理这个问题:

Until now I managed to deal with this by doing :

widget.hide()
widget.show()

但这是一个非常肮脏的修复.有没有更好的方法告诉 python 刷新小部件?

But this is a very dirty fix. Is there a better way to tell python to refresh the widget ?

谢谢.

推荐答案

要更新widget,你应该repaint()它,但是直接调用repaint()是不是很好,所以试试:

To update the widget, you should repaint() it, but calling repaint() directly is not very good, so try:

widget.update()

来自文档:

这个函数不会导致立即重绘;相反它当 Qt 返回主程序时,安排一个绘制事件进行处理事件循环.这允许 Qt 优化以获得更高的速度和更少的比调用 repaint() 闪烁.

This function does not cause an immediate repaint; instead it schedules a paint event for processing when Qt returns to the main event loop. This permits Qt to optimize for more speed and less flicker than a call to repaint() does.

多次调用 update() 通常只会导致一次PaintEvent() 调用.

Calling update() several times normally results in just one paintEvent() call.

Qt 通常在调用paintEvent() 之前擦除小部件的区域.如果设置了 Qt::WA_OpaquePaintEvent 小部件属性,小部件是负责用不透明的颜色绘制所有像素.

Qt normally erases the widget's area before the paintEvent() call. If the Qt::WA_OpaquePaintEvent widget attribute is set, the widget is responsible for painting all its pixels with an opaque color.

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

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