Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Redirect keyboard events to QTextEdit when it has no focus ?
Qt 6.11 is out! See what's new in the release blog

Redirect keyboard events to QTextEdit when it has no focus ?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 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 ?

    @
    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 = QTextEdit()
        vbox = QVBoxLayout()
        vbox.addWidget(self.textEdit)
        vbox.addWidget(ok)
        vbox.addWidget(cancel)
        self.setLayout(vbox)
    

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

    @

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Adrien Leravat
      wrote on last edited by
      #2

      Hi redstoneleo,

      (explained using the C++ prototypes)
      To receive keyboard events on your QTextEdit, you can

      • Give it the focus after creating it :
        @QWidget::setFocus(Qt::OtherFocusReason)
        @
      • Redirect focus from one widget to another (lets say windows to your edit)
        @QWidget::setFocusProcus(QWidget*)
        myWindow->setFocusProxy(myTextEdit);
        @
      • Prevent other widgets from gaining focus using
        @QWidget::setFocusPolicy(Qt::NoFocus)
        @

      Or if you need to intercept any key event at application level, you can create an QObject class: reimplement the method
      @bool QObject::eventFilter(QObject*, QEvent*)
      @

      And use QObject::installEventFilter(QObject*) with this class as parameter, on the window, or the application.

      Adeneo Embedded - www.adeneo-embedded.com

      1 Reply Last reply
      0
      • R Offline
        R Offline
        redstoneleo
        wrote on last edited by
        #3

        [quote author="Adrien Leravat" date="1374482323"]Hi redstoneleo,

        (explained using the C++ prototypes)
        To receive keyboard events on your QTextEdit, you can

        • Give it the focus after creating it :
          @QWidget::setFocus(Qt::OtherFocusReason)
          @
        • Redirect focus from one widget to another (lets say windows to your edit)
          @QWidget::setFocusProcus(QWidget*)
          myWindow->setFocusProxy(myTextEdit);
          @
        • Prevent other widgets from gaining focus using
          @QWidget::setFocusPolicy(Qt::NoFocus)
          @

        Or if you need to intercept any key event at application level, you can create an QObject class: reimplement the method
        @bool QObject::eventFilter(QObject*, QEvent*)
        @

        And use QObject::installEventFilter(QObject*) with this class as parameter, on the window, or the application.[/quote]

        thanks for your help !
        yes ,I did want to intercept any key event at application level, I have also been told to use event filter at such case before ,but I have difficulty in implementing the eventFilter() function here ,can you show some code ?thanks in advance !

        1 Reply Last reply
        0
        • R Offline
          R Offline
          redstoneleo
          wrote on last edited by
          #4

          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
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            you need to install the eventfilter on the QApplication instance ;)

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • R Offline
              R Offline
              redstoneleo
              wrote on last edited by
              #6

              [quote author="raven-worx" date="1374845629"]you need to install the eventfilter on the QApplication instance ;)[/quote]

              if I install the eventfilter on the QApplication instance ,so what should be modified in the eventfilter function ?
              this question has stuck me for a few days , I wonder if you could help ,thanks in advance !!!

              1 Reply Last reply
              0
              • B Offline
                B Offline
                butterface
                wrote on last edited by
                #7

                Where did you do that? As far as I can tell you installed it on the textEdit.

                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