无法改变光标形状

发布于 2025-01-25 04:03:37 字数 1467 浏览 2 评论 0原文

当将右鼠标按钮按下工具栏上的按钮小部件时,我正在尝试更改光标的形状。这是一个代码段:

def eventFilter(self, obj, event):  # QEvent
    if isinstance(obj, QPushButton):
        et = event.type()
        if et == QEvent.Type.MouseButtonPress:
            if event.button() == Qt.MouseButton.RightButton:
                cur_cursor = self.toolbar_tables.cursor()  # QCursor
                if cur_cursor.shape() == Qt.CursorShape.ArrowCursor:
                    cur_cursor.setShape(Qt.CursorShape.OpenHandCursor)
                else:
                    cur_cursor.setShape(Qt.CursorShape.ArrowCursor)
                return True

达到了第一个set -shape方法,但是即使光标不会更改,即使第一个setShape确实可以在内部正确更改形状,也从未达到第二个set -shape方法。 QT系统似乎有效,因为如果我在右按钮上按下拖​​动操作,光标会更改为黑色敞开的手。

请任何想法。

编辑:

def eventFilter(self, obj, event):  # QEvent
    if isinstance(obj, QPushButton):
        et = event.type()  # int
        if et == QEvent.Type.MouseButtonPress:
            if event.button() == Qt.MouseButton.RightButton:
                cur_cursor = self.toolbar_tables.cursor()  # QCursor
                if cur_cursor.shape() == Qt.CursorShape.ArrowCursor:
                    cur_cursor.setShape(Qt.CursorShape.OpenHandCursor)
                else:
                    cur_cursor.setShape(Qt.CursorShape.ArrowCursor)
                self.toolbar_tables.setCursor(cur_cursor)
                # QGuiApplication.setOverrideCursor(cur_cursor)
                return True

I am trying to change the shape of the cursor when the right mouse button is pressed over a pushbutton widget on a toolbar. Here's a code snippet:

def eventFilter(self, obj, event):  # QEvent
    if isinstance(obj, QPushButton):
        et = event.type()
        if et == QEvent.Type.MouseButtonPress:
            if event.button() == Qt.MouseButton.RightButton:
                cur_cursor = self.toolbar_tables.cursor()  # QCursor
                if cur_cursor.shape() == Qt.CursorShape.ArrowCursor:
                    cur_cursor.setShape(Qt.CursorShape.OpenHandCursor)
                else:
                    cur_cursor.setShape(Qt.CursorShape.ArrowCursor)
                return True

The first setShape method is reached, but the second is never reached because the cursor doesn't change, even though the first setShape does correctly change the shape internally. The Qt system seems to work because if I start a drag operation upon a right button press, the cursor changes to a black open hand.

Any ideas, please.

Edit:

def eventFilter(self, obj, event):  # QEvent
    if isinstance(obj, QPushButton):
        et = event.type()  # int
        if et == QEvent.Type.MouseButtonPress:
            if event.button() == Qt.MouseButton.RightButton:
                cur_cursor = self.toolbar_tables.cursor()  # QCursor
                if cur_cursor.shape() == Qt.CursorShape.ArrowCursor:
                    cur_cursor.setShape(Qt.CursorShape.OpenHandCursor)
                else:
                    cur_cursor.setShape(Qt.CursorShape.ArrowCursor)
                self.toolbar_tables.setCursor(cur_cursor)
                # QGuiApplication.setOverrideCursor(cur_cursor)
                return True

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文