Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. mouse movement as arrows up/down/left/right

mouse movement as arrows up/down/left/right

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 2 Posters 510 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.
  • D Offline
    D Offline
    dancaer69
    wrote on 22 Dec 2020, 10:28 last edited by
    #1

    I'm trying to implement a mouse movement to a Frame to simulate keyboard direction keys. I some relative info and I tried the following:

    class MainWindow(QMainWindow, Ui_MainWindow):
    
        def __init__(self, parent=None):
            super(MainWindow,self).__init__(parent)
    
            self.old_x = QCursor.pos().x()
            self.old_y = QCursor.pos().y()
            self.setupUi(self)
            self.frame.setMouseTracking(True)
            self.frame.installEventFilter(self)
    
            ....
    
    
            def eventFilter(self, source, event):
            if event.type() == QtCore.QEvent.MouseMove:
                #if event.buttons() == QtCore.Qt.NoButton:
                #    print("Simple mouse motion")
                if event.buttons() == QtCore.Qt.LeftButton:
                    print("Left click drag")
                    new_x = event.x()
                    new_y = event.y()
                    if new_x > self.old_x:
                        print("direction : right")
                        self.matrix, done = logic.right(self.matrix)
                        if done:
                            self.stateCheck()
                    elif new_x < self.old_x:
    
                        print("direction : left")
                        self.matrix, done = logic.left(self.matrix)
                        if done:
                            self.stateCheck()
                    elif new_y > self.old_y :
                        print("direction : down")
                    elif new_y < self.old_y:
                        print("direction : up")
                    self.old_x = new_x
                    self.old_y=new_y
            return super(QFrame, self.frame).eventFilter(source, event)
    

    This doesn't work as expected, and the movement changes directions most of the times, probably because when press the mouse button there is a very little movement to a different direction.
    What I want to achive, is the functionality of keyboard direction keys. Can be done with the above approach?

    J 1 Reply Last reply 22 Dec 2020, 10:55
    0
    • D dancaer69
      22 Dec 2020, 10:28

      I'm trying to implement a mouse movement to a Frame to simulate keyboard direction keys. I some relative info and I tried the following:

      class MainWindow(QMainWindow, Ui_MainWindow):
      
          def __init__(self, parent=None):
              super(MainWindow,self).__init__(parent)
      
              self.old_x = QCursor.pos().x()
              self.old_y = QCursor.pos().y()
              self.setupUi(self)
              self.frame.setMouseTracking(True)
              self.frame.installEventFilter(self)
      
              ....
      
      
              def eventFilter(self, source, event):
              if event.type() == QtCore.QEvent.MouseMove:
                  #if event.buttons() == QtCore.Qt.NoButton:
                  #    print("Simple mouse motion")
                  if event.buttons() == QtCore.Qt.LeftButton:
                      print("Left click drag")
                      new_x = event.x()
                      new_y = event.y()
                      if new_x > self.old_x:
                          print("direction : right")
                          self.matrix, done = logic.right(self.matrix)
                          if done:
                              self.stateCheck()
                      elif new_x < self.old_x:
      
                          print("direction : left")
                          self.matrix, done = logic.left(self.matrix)
                          if done:
                              self.stateCheck()
                      elif new_y > self.old_y :
                          print("direction : down")
                      elif new_y < self.old_y:
                          print("direction : up")
                      self.old_x = new_x
                      self.old_y=new_y
              return super(QFrame, self.frame).eventFilter(source, event)
      

      This doesn't work as expected, and the movement changes directions most of the times, probably because when press the mouse button there is a very little movement to a different direction.
      What I want to achive, is the functionality of keyboard direction keys. Can be done with the above approach?

      J Offline
      J Offline
      JonB
      wrote on 22 Dec 2020, 10:55 last edited by
      #2

      @dancaer69
      Not enough information how often/how much your things do/do not move.

      At a guess, you might want to build some "tolerance" in. I might move right a lot, but perhaps have a tiny bit of left at, say, the end. You may have to look for how much the movement is, and ignore small moves. You might also want a timer-delay, so that multiple movements are combined. Again, depends on more information as to what is/is not working right.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dancaer69
        wrote on 23 Dec 2020, 12:00 last edited by dancaer69
        #3

        Thanks, you had right. Needed to do something like that, but I didn't know how to do it. I tried QGestures which was my first choise, but I didn't find any information for pyqt and I didn't manage to make it work either. Finally I found moosegestrures library which does more that I want, but again no much info about that. It has some pygame examples though, and with those I managed to do what I wanted(probably not with an elegant way).
        So here is how I did it:

        class MainWindow(QMainWindow, Ui_MainWindow):
        
            def __init__(self, parent=None):
                super(MainWindow,self).__init__(parent)
        
                self.old_x = QCursor.pos().x()
                self.old_y = QCursor.pos().y()
                self.setupUi(self)
                self.frame.setMouseTracking(True)
                self.frame.installEventFilter(self)
                self.points[]
        ....
        
        
                def eventFilter(self, source, event):
                if event.type() == QtCore.QEvent.MouseMove:
                    #if event.buttons() == QtCore.Qt.NoButton:
                    #    print("Simple mouse motion")
                    if event.buttons() == QtCore.Qt.LeftButton:
                        if len(self.points) > 2:
                            startx, starty = self.points[0][0], self.points[0][1]
                            for i in range(len(self.points)):
                                self.points[i] = (self.points[i][0] - startx, self.points[i][1] - starty)
                    self.points.append((event.localPos().x(), event.localPos().y()))
                    print(self.points)
                    #self.points = []
                elif event.type() == QtCore.QEvent.MouseButtonRelease:
                    if event.button() == QtCore.Qt.LeftButton:
                        print("Released!")
                        self.mouseDown = False
                        strokes = moosegesture.getGesture(self.points)
                        strokeText=str(strokes[-1])
                        #print(strokeText)
                    if strokeText == "R":
                        self.matrix, done = logic.right(self.matrix)
                        if done:
                            self.stateCheck()
                    if strokeText == "L":
                        self.matrix, done = logic.left(self.matrix)
                        if done:
                            self.stateCheck()
                    if strokeText == "U":
                        self.matrix, done = logic.up(self.matrix)
                        if done:
                            self.stateCheck()
                    if strokeText == "D":
                        self.matrix, done = logic.down(self.matrix)
                        if done:
                            self.stateCheck()
                return self.frame.eventFilter(source, event)
        
        1 Reply Last reply
        0

        1/3

        22 Dec 2020, 10:28

        • Login

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