Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to forward Qt Mouse event to QQuickView?
QtWS25 Last Chance

How to forward Qt Mouse event to QQuickView?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qt5pyqt5qmlpython3
3 Posts 2 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eugeneoei
    wrote on last edited by
    #1

    In my QMainWindow, I have a QFrame and a QWidget that wraps a QQuickView and displays the ui through a qml file.

    I am trying to implement a drag and drop functionality where, on mouse-down and mouse-move in the QFrame, a thumbnail follows the cursor's position throughout until mouse-release. The mouse-release will happen within QQuickView.

    The hover event within QQuickView has no issues and I can successfully get the hover event. The problem arises when on mouse-down within QFrame followed by a mouse-move into QQuickView, I am unable to get any mouse events in QQuickView.

    On the left is the QFrame and the right is the QQuickView.

    Hovering in QQuickView independently:

    enter image description here

    Mouse-down in QFrame and mouse-move into QQuickView:

    enter image description here

    Any mouse events can only be captured after mouse-release.

    These are what I have written so far:

    import sys
    from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QFrame, QLabel, QGridLayout, QVBoxLayout
    from PyQt5.QtCore import Qt, QMimeData, QUrl
    from PyQt5.QtGui import QDrag, QPixmap
    from PyQt5.QtQuick import QQuickView
    
    class MainWindow(QMainWindow):
        def __init__(self, parent=None):
            super().__init__(parent)
            self.centralWidget = QWidget(self)
    
            gridlayout = QGridLayout(self.centralWidget)
            gridlayout.setContentsMargins(0,0,0,0)
            gridlayout.setHorizontalSpacing(0)
            gridlayout.setVerticalSpacing(0)
    
            self.setCentralWidget(self.centralWidget)
            self.leftPanel = QVBoxLayout()
            self.rightPanel = QVBoxLayout()
    
            gridlayout.addLayout(self.leftPanel, 0, 0, 1, 1)
            gridlayout.addLayout(self.rightPanel, 0, 1, 1, 1)
            gridlayout.setSpacing(0)
    
            self.setStyleSheet("background:grey")
            self.resize(300, 200)
            self.show()
    
    class Left(QFrame):
        def __init__(self):
            super().__init__()
            self.resize(500, 500)
            self.label = QLabel(self)
            self.label.resize(50, 50)
    
        def mouseMoveEvent(self, e):
            mimeData = QMimeData()
            drag = QDrag(self)
            self.thumbnail = QPixmap('./test.png').scaled(50, 50, Qt.KeepAspectRatio)
            drag.setPixmap(self.thumbnail)
            drag.setMimeData(mimeData)
            drag.exec_(Qt.MoveAction)
    
    class Right(QQuickView):
        def __init__(self, parent=None):
            super().__init__(parent)
            self.rootContext().setContextProperty('Right', self)
            self.setSource(QUrl('./drag.qml'))
            self.setMinimumHeight(200)
            self.setMinimumWidth(150)
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        main_window = MainWindow()
        main_window.leftPanel.addWidget(Left())
        main_window.rightPanel.addWidget(QWidget.createWindowContainer(Right()))
        app.exec_()
    

    Based on what I have read from different sources and the Qt documentation, I suppose I have to forward the events from the QFrame to the QQuickView or there seems to be some form of global mouse events to be handled.

    How can I go about achieving this?

    raven-worxR 1 Reply Last reply
    0
    • E eugeneoei

      In my QMainWindow, I have a QFrame and a QWidget that wraps a QQuickView and displays the ui through a qml file.

      I am trying to implement a drag and drop functionality where, on mouse-down and mouse-move in the QFrame, a thumbnail follows the cursor's position throughout until mouse-release. The mouse-release will happen within QQuickView.

      The hover event within QQuickView has no issues and I can successfully get the hover event. The problem arises when on mouse-down within QFrame followed by a mouse-move into QQuickView, I am unable to get any mouse events in QQuickView.

      On the left is the QFrame and the right is the QQuickView.

      Hovering in QQuickView independently:

      enter image description here

      Mouse-down in QFrame and mouse-move into QQuickView:

      enter image description here

      Any mouse events can only be captured after mouse-release.

      These are what I have written so far:

      import sys
      from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QFrame, QLabel, QGridLayout, QVBoxLayout
      from PyQt5.QtCore import Qt, QMimeData, QUrl
      from PyQt5.QtGui import QDrag, QPixmap
      from PyQt5.QtQuick import QQuickView
      
      class MainWindow(QMainWindow):
          def __init__(self, parent=None):
              super().__init__(parent)
              self.centralWidget = QWidget(self)
      
              gridlayout = QGridLayout(self.centralWidget)
              gridlayout.setContentsMargins(0,0,0,0)
              gridlayout.setHorizontalSpacing(0)
              gridlayout.setVerticalSpacing(0)
      
              self.setCentralWidget(self.centralWidget)
              self.leftPanel = QVBoxLayout()
              self.rightPanel = QVBoxLayout()
      
              gridlayout.addLayout(self.leftPanel, 0, 0, 1, 1)
              gridlayout.addLayout(self.rightPanel, 0, 1, 1, 1)
              gridlayout.setSpacing(0)
      
              self.setStyleSheet("background:grey")
              self.resize(300, 200)
              self.show()
      
      class Left(QFrame):
          def __init__(self):
              super().__init__()
              self.resize(500, 500)
              self.label = QLabel(self)
              self.label.resize(50, 50)
      
          def mouseMoveEvent(self, e):
              mimeData = QMimeData()
              drag = QDrag(self)
              self.thumbnail = QPixmap('./test.png').scaled(50, 50, Qt.KeepAspectRatio)
              drag.setPixmap(self.thumbnail)
              drag.setMimeData(mimeData)
              drag.exec_(Qt.MoveAction)
      
      class Right(QQuickView):
          def __init__(self, parent=None):
              super().__init__(parent)
              self.rootContext().setContextProperty('Right', self)
              self.setSource(QUrl('./drag.qml'))
              self.setMinimumHeight(200)
              self.setMinimumWidth(150)
      
      if __name__ == '__main__':
          app = QApplication(sys.argv)
          main_window = MainWindow()
          main_window.leftPanel.addWidget(Left())
          main_window.rightPanel.addWidget(QWidget.createWindowContainer(Right()))
          app.exec_()
      

      Based on what I have read from different sources and the Qt documentation, I suppose I have to forward the events from the QFrame to the QQuickView or there seems to be some form of global mouse events to be handled.

      How can I go about achieving this?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @eugeneoei said in How to forward Qt Mouse event to QQuickView?:

      Based on what I have read from different sources and the Qt documentation, I suppose I have to forward the events from the QFrame to the QQuickView or there seems to be some form of global mouse events to be handled.

      no, no event forwarding needed here. You are just listening to wrong events in QML.
      Since you start a drag, you should also listen to drag events in QML. See DropArea element and it's containsDrag property.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      E 1 Reply Last reply
      0
      • raven-worxR raven-worx

        @eugeneoei said in How to forward Qt Mouse event to QQuickView?:

        Based on what I have read from different sources and the Qt documentation, I suppose I have to forward the events from the QFrame to the QQuickView or there seems to be some form of global mouse events to be handled.

        no, no event forwarding needed here. You are just listening to wrong events in QML.
        Since you start a drag, you should also listen to drag events in QML. See DropArea element and it's containsDrag property.

        E Offline
        E Offline
        eugeneoei
        wrote on last edited by
        #3

        @raven-worx you sir saved my life! I cant believe i was stuck at this for 2 days, when it was simply just about using the right object in qml. thanks!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved