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.7k 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.
  • Black CatB Offline
    Black CatB Offline
    Black Cat
    wrote on last edited by Black Cat
    #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')
    
    eyllanescE 1 Reply Last reply
    0
    • Black CatB Black Cat

      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')
      
      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by eyllanesc
      #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.

      Black CatB 2 Replies Last reply
      1
      • eyllanescE eyllanesc

        @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_())
        
        Black CatB Offline
        Black CatB Offline
        Black Cat
        wrote on last edited by
        #3

        @eyllanesc Thank you man

        1 Reply Last reply
        0
        • eyllanescE eyllanesc

          @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_())
          
          Black CatB Offline
          Black CatB Offline
          Black Cat
          wrote on 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)
          
          eyllanescE 1 Reply Last reply
          0
          • Black CatB Black Cat

            @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)
            
            eyllanescE Offline
            eyllanescE Offline
            eyllanesc
            wrote on 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

            • Login

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