Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. How to implement key press events ?
Forum Updated to NodeBB v4.3 + New Features

How to implement key press events ?

Scheduled Pinned Locked Moved Language Bindings
2 Posts 2 Posters 7.1k Views 1 Watching
  • 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.
  • R Offline
    R Offline
    redstoneleo
    wrote on last edited by
    #1

    The UI is as following .
    http://img.my.csdn.net/uploads/201211/03/1351957960_1171.jpg

    What I want is :
    When I press the Enter key ,the label should shows “Enter”
    When I press The blank space key ,the label should shows “space”

    How to implement this ?

    I know I should reimplement the keyPressEvent handler ,but I guss I was got stuck by the focus

    1 Reply Last reply
    0
    • jazzycamelJ Offline
      jazzycamelJ Offline
      jazzycamel
      wrote on last edited by
      #2

      You're right, you are going to have issues with focus if you re-implement keyPressEvent for each button. The example below uses QAction's bound to the widget to capture the key events. The buttons then simply invoke these actions "triggered" method.

      @
      from PyQt4.QtCore import *
      from PyQt4.QtGui import *

      class Widget(QWidget):
      def init(self, parent=None):
      QWidget.init(self, parent)

          l=QVBoxLayout(self)
      
          self.label=QLabel("(None)", self)
          l.addWidget(self.label)
      
          self.setEnterAction=QAction("Set Enter", self, shortcut=Qt.Key_Return, triggered=self.setEnter)
          self.addAction(self.setEnterAction)
          l.addWidget(QPushButton("Enter", self, clicked=self.setEnterAction.triggered))
      
          self.setSpaceAction=QAction("Set Space", self, shortcut=Qt.Key_Space, triggered=self.setSpace)
          self.addAction(self.setSpaceAction)
          l.addWidget(QPushButton("Space", self, clicked=self.setSpaceAction.triggered))
      
      def setEnter(self): self.label.setText("Enter")
      def setSpace(self): self.label.setText("Space")
      

      if name=="main":
      from sys import argv, exit
      a=QApplication(argv)
      w=Widget()
      w.show()
      w.raise_()
      exit(a.exec_())
      @

      Hope this helps.

      For the avoidance of doubt:

      1. All my code samples (C++ or Python) are tested before posting
      2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
      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