获取QMenuBar菜单的QRect

时间:2018-04-26 04:36:30

标签: python pyqt pyqt4 qmenubar

我正在QMenuBar中重新实现mouseMoveEvent,允许我点击拖动无框应用程序(基本上只是在我的应用程序周围创建自己的框架)。但是,当鼠标悬停在QMenu项目上时,我试图禁用此行为,这会产生奇怪的行为(当您单击菜单时,Windows会在您的鼠标后面开始!)。

我认为这就像调用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

1 个答案:

答案 0 :(得分:0)

在eyllanesc的帮助下,这是工作代码。 self.childrenRect()不起作用(bug?)但是手动循环遍历子节点。

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