【问题标题】:Different Behavior of moveMouseEvent on different WidgetsmoveMouseEvent 在不同小部件上的不同行为
【发布时间】:2018-01-07 12:22:16
【问题描述】:

在学习事件处理时,我注意到 mouseMoveEvent 并在 QTextBrowser 和 QPushButton 上实现了它,结果在某种程度上是不同的。当我将鼠标悬停在 QTextBrowser 上时,mouseMoveEvent 被激活。但是在 QPushButton 的情况下,当鼠标保持单击时会发生 mouseMove 事件,然后光标悬停在周围。一旦单击被释放,悬停在 Button 上就没有效果。 为什么会有这样的差异?我应该怎么做才能在 QPushButton 上实现相同的功能,即在没有 mouseKeypressed 的情况下激活事件

class window35(QtWidgets.QMainWindow):
    def __init__(self):
        super(window35, self).__init__()
        myWidget=QtWidgets.QWidget()
        self.Hbox=QtWidgets.QHBoxLayout()
        self.btn=QtWidgets.QPushButton("HAA BHYI\nBUTTON")
        self.textBrowser=QtWidgets.QTextBrowser()
        self.Hbox.addWidget(self.btn)
        self.Hbox.addWidget(self.textBrowser)
        myWidget.setLayout(self.Hbox)
        self.setCentralWidget(myWidget)
        self.btn.mouseMoveEvent=self.mouse_move
        self.textBrowser.mouseMoveEvent=self.text_browser_move
#        print(type(self.btn.mouseMoveEvent))

        self.show()
    def mouse_move(self,event):
        print("hello btn")
        try:
            QtWidgets.QPushButton.mouseMoveEvent(self.btn,event)
        except Exception as E:
            print(E)

    def text_browser_move(self,event):
        print("hello list")
        try:
            QtWidgets.QTextBrowser.mouseMoveEvent(self.textBrowser,event)
        except Exception as E:
            print(E)




app=QtWidgets.QApplication([])
ex=window35()
sys.exit(app.exec_())

我注意到 listWidget 也像 QPushButton 一样工作 谢谢:)

【问题讨论】:

标签: python python-3.x qt pyqt pyqt5


【解决方案1】:

感谢@G.M。 我刚刚添加了

self.btn.setMouseTracking(True)

达到想要的效果

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多