Python 刷新 QWidget
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30728820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refreshing a QWidget
提问by Olivier Giniaux
I've been having this issue a lot of times.
我已经遇到这个问题很多次了。
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.
当我在QWidget
之后修改 a 的某些属性时widget.show()
,小部件不会更新。大多数情况下,鼠标单击或鼠标离开或进入小部件时,小部件都会更新。但是,如果我离开鼠标,它不会自行刷新。
Until now I managed to deal with this by doing :
到目前为止,我设法通过执行以下操作来处理此问题:
widget.hide()
widget.show()
But this is a very dirty fix. Is there a better way to tell python
to refresh the widget ?
但这是一个非常肮脏的修复。有没有更好的方法来告诉python
刷新小部件?
Thank you.
谢谢你。
采纳答案by Kosovan
To update the widget, you should repaint()
it, but calling repaint()
directly is not very good, so try:
要更新widget,你应该repaint()
这样做,但是repaint()
直接调用不是很好,所以尝试:
widget.update()
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.
Calling update() several times normally results in just one paintEvent() call.
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.
此函数不会立即重新绘制;相反,它会在 Qt 返回到主事件循环时安排一个绘制事件进行处理。与调用 repaint() 相比,这允许 Qt 进行优化以获得更高的速度和更少的闪烁。
多次调用 update() 通常只会导致一次 paintEvent() 调用。
Qt 通常在paintEvent() 调用之前擦除小部件的区域。如果设置了 Qt::WA_OpaquePaintEvent 小部件属性,则小部件负责用不透明颜色绘制其所有像素。
回答by Bharadwaj
Did you already try the
QWidget.update()
您是否已经尝试过
QWidget.update()
This function updates only the visible parts keeping the invisible parts untouched.
此功能仅更新可见部分,不影响不可见部分。