問題描述
我在更新使用 QToolButton
設置的按鈕的圖標時遇到問題.這個想法是將按鈕用于電影播放器??.播放時,按下按鈕,圖標變為暫停.再次按下時,播放暫停,圖標恢復播放.我有一些工作代碼,但問題是圖標沒有持續更新.如果我將 Qt 窗口保持在焦點上,則需要按下一兩個按鈕才能將圖標更改為預期圖像,此時實際圖像不是預期圖像(交換播放/暫停).
I'm having a problem updating the icon of a button set with QToolButton
. The idea is to use the button for a movie player. To play, one presses the button and the icon changes to pause. When pressed again, play is paused and the icon reverts to play. I have some working code, but the problem is that the icon is not updating consistently. If I keep the Qt window in focus, it takes one or two button presses to change the icon to the intended image, by which time the actual image is not the intended image (swapped play/pause).
這是一些最小的示例代碼:
Here is some minimal example code:
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget, QStyle, QToolButton
class Widget(QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent=parent)
self.play_button = QToolButton(clicked=self.update_button)
self.play_button.setIcon(self.style().standardIcon(QStyle.SP_MediaStop))
self.verticalLayout = QVBoxLayout(self)
self.verticalLayout.addWidget(self.play_button)
self.button_pressed = False
def update_button(self):
if self.button_pressed:
self.play_button.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
self.button_pressed = False
print("Button should be set to PLAY. Press is", self.button_pressed)
else:
self.play_button.setIcon(self.style().standardIcon(QStyle.SP_MediaPause))
self.button_pressed = True
print("Button should be set to PAUSE. Press is", self.button_pressed)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
在上面我從一個停止圖標開始只是為了確保觀察到變化(任何點擊都應該總是改變圖標).保持窗口聚焦,我得到以下輸出:
In the above I start with a stop icon just to be sure to observe a change (any click should always change the icon). Keeping the window focused, I get the following output:
- 第一次點擊:按鈕應設置為暫停.按下為真"(圖標沒有變化)
- 第二次點擊:按鈕應設置為播放.按下為假"(圖標變為暫停)
- 第三次點擊:按鈕應設置為暫停.按下為真"(圖標變為播放)(依此類推,繼續按預期交換)
我還注意到,如果每次單擊后,我在 Qt 窗口之外單擊,或者調整 Qt 窗口的大小,按鈕圖標會更新為正確的圖標.我究竟做錯了什么?如何強制更新圖標?
I've also noticed that if after each click, I click outside the Qt window, or resize the Qt window, the button icon updates to the correct one. What am I doing wrong? How do I force the icon to update?
這種行為主要發生在 QToolButton
中,但 QPushButton
也會產生問題(在聚焦時有效,但如果我調整 Qt 窗口大小,則會出現錯誤行為/丟失正確狀態的跟蹤).在 macOS 上使用 PyQt 5.12.3 和 qt 5.12.5.
This behaviour happens mostly with QToolButton
, but QPushButton
also give issues (works when focused, but misbehaves/loses track of correct status if I resize the Qt window). Using PyQt 5.12.3 and qt 5.12.5 on macOS.
推薦答案
似乎這個問題是 macOS 的 Qt 實現中的一個錯誤.我測試過,它發生在 PyQt5 和 PySide2 上,所以它必須來自 Qt.在 .setIcon()
之后調用 .repaint()
強制重繪似乎可以解決問題:
Seems like this issue is a bug in the Qt implementation for macOS. I tested and it happens with both PyQt5 and PySide2, so it must come from Qt. Forcing a redraw with a call to .repaint()
after .setIcon()
seems to make the problem go away:
self.play_button.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
self.play_button.repaint()
這篇關于PyQt QToolButton在焦點時不更新圖標的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!