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. Redirect keyboard events to QTextEdit when it has no focus ?
Forum Updated to NodeBB v4.3 + New Features

Redirect keyboard events to QTextEdit when it has no focus ?

Scheduled Pinned Locked Moved Language Bindings
3 Posts 2 Posters 3.9k 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

    when the QTextEdit doesn’t have focus ,how to redirect keyboard events to the text editor ?
    as This saves the user from clicking the text editor before entering text, making the application more user-friendly.

    some people told me to use Event Filters ,I did use it ,the following is my code of using Event Filters ,but it doesn't work , anyone can help ?
    @
    import sys
    from PyQt4.QtGui import *
    from PyQt4.QtCore import *
    class BoxLayout(QWidget):
    def init(self, parent=None):
    super(BoxLayout, self).init(parent)
    self.resize(100, 300)

        ok = QPushButton("OK")
        cancel = QPushButton("Cancel")
        self.textEdit =QLineEdit("""This function returns true if the contents of the MIME data object, specified by source , can be decoded and inserted into the document. It is called for example when during a drag operation the mouse enters this widget and it is necessary to determine whether it is possible to accept the drag and drop operation.""")
        
        vbox = QVBoxLayout()
        vbox.addWidget(self.textEdit)
        vbox.addWidget(ok)
        vbox.addWidget(cancel)
        self.setLayout(vbox)
    
    
        self.textEdit.installEventFilter(self)
    
    def eventFilter(self, obj, event):
        if obj == self and event.type() == QKeyEvent:
                self.textEdit.setFocus () 
                self.textEdit.keyPressEvent(event)
                return True
        else:
            return QMainWindow.eventFilter(self, obj, event)
    

    app = QApplication(sys.argv)
    qb = BoxLayout()
    qb.show()
    sys.exit(app.exec_())

    @

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

      Firstly, I would not recommend that you actually do this in an application. What you see as making the app more 'user friendly' actually breaks with the key concepts of user expectation and OS native behaviours that Qt promotes. That said (and my conscience appeased ;o) and seeing as you asked me directly (via a PM) here is a simple solution:

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

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

          self.resize(100, 300)
               
          vbox=QVBoxLayout(self)
          self.textEdit = QTextEdit("""
              This function returns true if the contents of the MIME data object,
              specified by source, can be decoded and inserted into the document.
              It is called for example when during a drag operation the mouse
              enters this widget and it is necessary to determine whether it is
              possible to accept the drag and drop operation.
          """)
      
          vbox.addWidget(self.textEdit)
          vbox.addWidget(QPushButton("OK", self))
          vbox.addWidget(QPushButton("Cancel", self))
      
          self.setFocus()
      
      def keyPressEvent(self, event):
          self.textEdit.setFocus()
          self.textEdit.keyPressEvent(event)
      

      if name=="main":
      from sys import argv, exit

      app=QApplication(argv)
      qb=BoxLayout()
      qb.show()
      qb.raise_()
      exit(app.exec_())
      

      @

      The keyPressEvent() method is the important bit here: when the main widget receives a key press event focus is moved to the QTextEdit and the initial event is forwarded.

      Hope this answers your question ;o)

      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
      • R Offline
        R Offline
        redstoneleo
        wrote on last edited by
        #3

        thanks , jazzycamel,you're a very kind-hearted person .
        you said
        [quote author="jazzycamel" date="1374860919"] What you see as making the app more 'user friendly' actually breaks with the key concepts of user expectation and OS native behaviours that Qt promotes.
        [/quote]

        I am curious how you learned of this ?

        for this question ,I found another solution ,code here
        @
        def changeEvent(self, event):
        if event.type()==QEvent.ActivationChange:
        self.textEdit.setFocus()
        @

        as for you solution , I also thought of that, but there is some problem with it when you type words into the textEdit using a input software ,for example ,I use "Sogou Pinyin":http://en.wikipedia.org/wiki/Sogou_Pinyin
        ,I don’t know why the problem occurred …
        Thanks anyway.

        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