简体   繁体   English

PyQt5 将鼠标悬停在 QPushButton 上并按下鼠标

[英]PyQt5 Hover over QPushButton with mouse pressed down

I am trying to simulate a paint brush with Qt buttons instead of pixels.我正在尝试使用 Qt 按钮而不是像素来模拟画笔。 I have overloaded the event filter on each button and can detect eventHover or eventFilter only when the mouse is not pressed down.我已经在每个按钮上重载了事件过滤器,并且只有在没有按下鼠标时才能检测到 eventHover 或 eventFilter。 I want to hold mouse click down and detect if i collide with a button.我想按住鼠标单击并检测我是否与按钮发生碰撞。 Any suggestions as to what i should do from here?关于我应该从这里做什么的任何建议?

def eventFilter(self, a0, a1):
    if a1.type() == QtCore.QEvent.Enter:
        if(self.mouse.pressed):
            ui_logic.square_press(self,"red")
    return super().eventFilter(a0, a1)

def square_press(button,color):
    style = "QPushButton{background-color:" + color + "}"
    button.setStyleSheet(style)

Thank you谢谢

What i ended up doing is tracking the mouse and getting the widget at a location.我最终做的是跟踪鼠标并在某个位置获取小部件。 Example code:示例代码:

class TrackMouse(QtWidgets.QWidget):
pressed = False
def __init__(self,obj,app):
    self.app = app
    super(QtWidgets.QWidget, self).__init__()
def eventFilter(self, a0, event):
    if event.type() == QtCore.QEvent.MouseMove:
        if(event.buttons() == QtCore.Qt.MouseButton.LeftButton):
            widget = self.app.widgetAt(event.globalPos())
            print(widget.__class__)
            if(widget.__class__ is CustomButtom):
                ui_logic.square_press(widget,"red")
    return super().eventFilter(a0, event)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM