0

我正在 QMenuBar 中重新实现 mouseMoveEvent 以允许我单击并拖动无框架应用程序(基本上只是在我的应用程序周围制作我自己的框架)。但是,每当鼠标悬停在 QMenu 项目上时,我都会尝试禁用此行为,这会产生奇怪的行为(当您单击菜单时,窗口会开始跟随您的鼠标!)。

我认为这就像调用 QMenuBar 一样简单,self.childrenRect().contains(event.pos())但这不起作用。据我所知self.childrenRect(),实际上并没有返回 QMenu 项目的矩形。那么执行此操作的“正确”方法是什么?

这是我的子类 QMenuBar 供参考:

class MoveMenu(QtGui.QMenuBar):
    def __init__(self):
        super(MoveMenu, self).__init__()
        self.mouseStartLoc = QtCore.QPoint(0,0)
        self.set_move = False

    def mousePressEvent(self, event):
        super(MoveMenu, self).mousePressEvent(event)
        self.mouseStartLoc = event.pos()

        # this is always testing False
        if not self.childrenRect().contains(event.pos()):
            self.set_move = True

    def mouseMoveEvent(self, event):
        super(MoveMenu, self).mouseMoveEvent(event)
        if self.set_move:
            globalPos = event.globalPos()
            self.parent().move(globalPos - self.mouseStartLoc)

    def mouseReleaseEvent(self, event):
        super(MoveMenu, self).mouseReleaseEvent(event)
        self.set_move = False
4

1 回答 1

0

在 eyllanesc 的帮助下,这是工作代码。self.childrenRect() 不起作用(错误?),但手动循环遍历子项可以。

class MoveMenu(QtGui.QMenuBar):
    def __init__(self):
        super(MoveMenu, self).__init__()
        self.mouseStartLoc = QtCore.QPoint(0,0)
        self.set_move = False

    def mousePressEvent(self, event):
        super(MoveMenu, self).mousePressEvent(event)
        self.mouseStartLoc = event.pos()

        if not any(child.rect().contains(event.pos()) for child in self.children():
            self.set_move = True

    def mouseMoveEvent(self, event):
        super(MoveMenu, self).mouseMoveEvent(event)
        if self.set_move:
            globalPos = event.globalPos()
            self.parent().move(globalPos - self.mouseStartLoc)

    def mouseReleaseEvent(self, event):
        super(MoveMenu, self).mouseReleaseEvent(event)
        self.set_move = False
于 2018-04-26T05:15:07.453 回答