如何在QEvent中捕获NonClientAreaMouseMove事件

时间:2019-07-03 10:16:22

标签: python qt qt5 pyside2

当用户从titleBar移动Qdialog时,我尝试找到一种捕获事件的方法。 我的目标是附加一个拖动事件,以将我的自定义qdialog停靠在mainWindow中。 (在Linux上)

换句话说,要做ockockwidgets的事情(我可以​​使用dockwidget),我必须对Custom Qdialog(或带有Qt.Window标志的小部件)做同样的事情

我在c ++ Qt源代码中看到的要比QDockWidget要多,他们使用这种东西:

bool QDockWidget::event(QEvent *event)
{   [...]
  case QEvent::NonClientAreaMouseMove:
  case QEvent::NonClientAreaMouseButtonPress:
  case QEvent::NonClientAreaMouseButtonRelease:
  case QEvent::NonClientAreaMouseButtonDblClick:
      d->nonClientAreaMouseEvent(static_cast<QMouseEvent*>(event));

但是当我尝试在pyside上捕获此类事件时,我什么也没收到:

    def event(self, e):
      print('event %s' % e.type())
      return super(myDyalig,self).event(e)

event PySide2.QtCore.QEvent.Type.ActivationChange
event PySide2.QtCore.QEvent.Type.UpdateRequest
event PySide2.QtCore.QEvent.Type.Paint
# I recieve only this move event when user stop moving (when he 
#  release  the button)
event PySide2.QtCore.QEvent.Type.Move 
event PySide2.QtCore.QEvent.Type.WindowActivate
event PySide2.QtCore.QEvent.Type.ActivationChange
event PySide2.QtCore.QEvent.Type.UpdateRequest
event PySide2.QtCore.QEvent.Type.Paint

任何想法如何做到这一点? (或另一个使用qdialog实现拖动事件的想法)

编辑:

一个最小的例子:

 from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *

class CustomDialog(QDialog):

    def __init__(self, parent=None):
        super(CustomDialog,self).__init__(parent)
        self.setFixedSize(QSize(200,200))

    def event(self, e):
        print('event %s' % e.type())
        return super(CustomDialog,self).event(e)

def main():
    import sys
    app = QApplication(sys.argv)
    dial = CustomDialog()
    dial.show()

    sys.exit(app.exec_())
if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:0)

您需要安装事件过滤器:

def __init__(self, parent=None):
    super().__init__(parent)

    #...

    self.installEventFilter(self)

def eventFilter(self, obj, event):
    if event.type() in (QEvent.NonClientAreaMouseButtonPress, QEvent.NonClientAreaMouseButtonPress, QEvent.Move):
        print(event)
    return super().eventFilter(obj, event)

另请参阅:https://doc.qt.io/qt-5/eventsandfilters.html