pyqt5悬停在qpushbutton上,鼠标压向下

发布于 2025-02-13 15:49:33 字数 452 浏览 0 评论 0原文

我正在尝试使用QT按钮而不是像素模拟油漆刷。我在每个按钮上都超载了事件过滤器,只有在未按下鼠标时,才能检测到事件持续或事件窗口。我想按住鼠标单击并检测是否与按钮相撞。关于我应该从这里做什么的建议?

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)

谢谢

I am trying to simulate a paint brush with Qt buttons instead of pixels. I have overloaded the event filter on each button and can detect eventHover or eventFilter only when the mouse is not pressed down. 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

临走之时 2025-02-20 15:49:33

我最终做的是跟踪鼠标并将小部件放在一个位置。
示例代码:

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文