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. Return a function when arrow up key pressed?!
Forum Updated to NodeBB v4.3 + New Features

Return a function when arrow up key pressed?!

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 2 Posters 2.2k 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.
  • B Offline
    B Offline
    Black Cat
    wrote on 3 Jan 2021, 00:37 last edited by Black Cat 1 Mar 2021, 00:39
    #1

    I have this line code for when the enter is pressed on the line edit. But, how to convert this code to call the function if the arrow up key is pressed on the line edit?

    self.lineEdit.returnPressed.connect(self.call_sc)
    
    def call_sc(self):
        print('working')
    

    Representation

    self.lineEdit.Key_upPressed.connect(self.call_sc)
    
    def call_sc(self):
        print('working')
    
    E 1 Reply Last reply 3 Jan 2021, 01:26
    0
    • B Black Cat
      3 Jan 2021, 00:37

      I have this line code for when the enter is pressed on the line edit. But, how to convert this code to call the function if the arrow up key is pressed on the line edit?

      self.lineEdit.returnPressed.connect(self.call_sc)
      
      def call_sc(self):
          print('working')
      

      Representation

      self.lineEdit.Key_upPressed.connect(self.call_sc)
      
      def call_sc(self):
          print('working')
      
      E Offline
      E Offline
      eyllanesc
      wrote on 3 Jan 2021, 01:26 last edited by eyllanesc 1 Mar 2021, 01:29
      #2

      @Black-Cat There is no default signal that is emitted when the up key is pressed, so you will have to create it and for this an option is to override the keyPressEvent method:

      import sys
      
      from PySide2 import QtCore, QtWidgets
      
      
      class LineEdit(QtWidgets.QLineEdit):
          upPressed = QtCore.Signal()
      
          def keyPressEvent(self, event):
              super().keyPressEvent(event)
              if event.key() == QtCore.Qt.Key_Up:
                  self.upPressed.emit()
      
      
      class Widget(QtWidgets.QWidget):
          def __init__(self, parent=None):
              super().__init__(parent)
      
              lineedit = LineEdit()
      
              lay = QtWidgets.QVBoxLayout(self)
              lay.addWidget(lineedit)
              lay.addStretch()
      
              lineedit.upPressed.connect(self.call_sc)
      
          def call_sc(self):
              print('working')
      
      
      if __name__ == "__main__":
          app = QtWidgets.QApplication(sys.argv)
          w = Widget()
          w.show()
          sys.exit(app.exec_())
      

      Another option is to implement it through an event filter:

      import sys
      
      from PySide2 import QtCore, QtWidgets
      
      
      class Widget(QtWidgets.QWidget):
          def __init__(self, parent=None):
              super().__init__(parent)
      
              self.lineedit = QtWidgets.QLineEdit()
      
              lay = QtWidgets.QVBoxLayout(self)
              lay.addWidget(self.lineedit)
              lay.addStretch()
      
              self.lineedit.installEventFilter(self)
      
          def eventFilter(self, obj, event):
              if obj is self.lineedit and event.type() == QtCore.QEvent.KeyPress:
                  if event.key() == QtCore.Qt.Key_Up:
                      self.call_sc()
              return super().eventFilter(obj, event)
      
          def call_sc(self):
              print("working")
      
      
      if __name__ == "__main__":
          app = QtWidgets.QApplication(sys.argv)
          w = Widget()
          w.show()
          sys.exit(app.exec_())
      

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      B 2 Replies Last reply 3 Jan 2021, 02:20
      1
      • E eyllanesc
        3 Jan 2021, 01:26

        @Black-Cat There is no default signal that is emitted when the up key is pressed, so you will have to create it and for this an option is to override the keyPressEvent method:

        import sys
        
        from PySide2 import QtCore, QtWidgets
        
        
        class LineEdit(QtWidgets.QLineEdit):
            upPressed = QtCore.Signal()
        
            def keyPressEvent(self, event):
                super().keyPressEvent(event)
                if event.key() == QtCore.Qt.Key_Up:
                    self.upPressed.emit()
        
        
        class Widget(QtWidgets.QWidget):
            def __init__(self, parent=None):
                super().__init__(parent)
        
                lineedit = LineEdit()
        
                lay = QtWidgets.QVBoxLayout(self)
                lay.addWidget(lineedit)
                lay.addStretch()
        
                lineedit.upPressed.connect(self.call_sc)
        
            def call_sc(self):
                print('working')
        
        
        if __name__ == "__main__":
            app = QtWidgets.QApplication(sys.argv)
            w = Widget()
            w.show()
            sys.exit(app.exec_())
        

        Another option is to implement it through an event filter:

        import sys
        
        from PySide2 import QtCore, QtWidgets
        
        
        class Widget(QtWidgets.QWidget):
            def __init__(self, parent=None):
                super().__init__(parent)
        
                self.lineedit = QtWidgets.QLineEdit()
        
                lay = QtWidgets.QVBoxLayout(self)
                lay.addWidget(self.lineedit)
                lay.addStretch()
        
                self.lineedit.installEventFilter(self)
        
            def eventFilter(self, obj, event):
                if obj is self.lineedit and event.type() == QtCore.QEvent.KeyPress:
                    if event.key() == QtCore.Qt.Key_Up:
                        self.call_sc()
                return super().eventFilter(obj, event)
        
            def call_sc(self):
                print("working")
        
        
        if __name__ == "__main__":
            app = QtWidgets.QApplication(sys.argv)
            w = Widget()
            w.show()
            sys.exit(app.exec_())
        
        B Offline
        B Offline
        Black Cat
        wrote on 3 Jan 2021, 02:20 last edited by
        #3

        @eyllanesc Thank you man

        1 Reply Last reply
        0
        • E eyllanesc
          3 Jan 2021, 01:26

          @Black-Cat There is no default signal that is emitted when the up key is pressed, so you will have to create it and for this an option is to override the keyPressEvent method:

          import sys
          
          from PySide2 import QtCore, QtWidgets
          
          
          class LineEdit(QtWidgets.QLineEdit):
              upPressed = QtCore.Signal()
          
              def keyPressEvent(self, event):
                  super().keyPressEvent(event)
                  if event.key() == QtCore.Qt.Key_Up:
                      self.upPressed.emit()
          
          
          class Widget(QtWidgets.QWidget):
              def __init__(self, parent=None):
                  super().__init__(parent)
          
                  lineedit = LineEdit()
          
                  lay = QtWidgets.QVBoxLayout(self)
                  lay.addWidget(lineedit)
                  lay.addStretch()
          
                  lineedit.upPressed.connect(self.call_sc)
          
              def call_sc(self):
                  print('working')
          
          
          if __name__ == "__main__":
              app = QtWidgets.QApplication(sys.argv)
              w = Widget()
              w.show()
              sys.exit(app.exec_())
          

          Another option is to implement it through an event filter:

          import sys
          
          from PySide2 import QtCore, QtWidgets
          
          
          class Widget(QtWidgets.QWidget):
              def __init__(self, parent=None):
                  super().__init__(parent)
          
                  self.lineedit = QtWidgets.QLineEdit()
          
                  lay = QtWidgets.QVBoxLayout(self)
                  lay.addWidget(self.lineedit)
                  lay.addStretch()
          
                  self.lineedit.installEventFilter(self)
          
              def eventFilter(self, obj, event):
                  if obj is self.lineedit and event.type() == QtCore.QEvent.KeyPress:
                      if event.key() == QtCore.Qt.Key_Up:
                          self.call_sc()
                  return super().eventFilter(obj, event)
          
              def call_sc(self):
                  print("working")
          
          
          if __name__ == "__main__":
              app = QtWidgets.QApplication(sys.argv)
              w = Widget()
              w.show()
              sys.exit(app.exec_())
          
          B Offline
          B Offline
          Black Cat
          wrote on 3 Jan 2021, 15:14 last edited by
          #4

          @eyllanesc
          Basicly I need to implemente this key press reaction in a script that I will call by another script, so, it don't have the "if name == "main":"

          I tried some changes but it return the same erro

                  self.lineEdit.installEventFilter(self)
          
              def eventFilter(self, obj, event):
                  if obj is self.lineEdit and event.type() == QtCore.QEvent.KeyPress:
                      if event.key() == QtCore.Qt.Key_Up:
                          self.call_sc()
                  return super().eventFilter(obj, event)
          

          00f4892b-ad5c-4241-afb5-d5ced8f21837-image.png

          What is supposed to input in this arguments on line 1053?

          self.lineEdit.installEventFilter(self)
          
          E 1 Reply Last reply 3 Jan 2021, 15:19
          0
          • B Black Cat
            3 Jan 2021, 15:14

            @eyllanesc
            Basicly I need to implemente this key press reaction in a script that I will call by another script, so, it don't have the "if name == "main":"

            I tried some changes but it return the same erro

                    self.lineEdit.installEventFilter(self)
            
                def eventFilter(self, obj, event):
                    if obj is self.lineEdit and event.type() == QtCore.QEvent.KeyPress:
                        if event.key() == QtCore.Qt.Key_Up:
                            self.call_sc()
                    return super().eventFilter(obj, event)
            

            00f4892b-ad5c-4241-afb5-d5ced8f21837-image.png

            What is supposed to input in this arguments on line 1053?

            self.lineEdit.installEventFilter(self)
            
            E Offline
            E Offline
            eyllanesc
            wrote on 3 Jan 2021, 15:19 last edited by
            #5

            @Black-Cat With the little code it is impossible to help you so if you want more help then you must provide a minimal and reproducible example

            If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

            1 Reply Last reply
            0

            1/5

            3 Jan 2021, 00:37

            • Login

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