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. QWidget.setFocus() does not work
Qt 6.11 is out! See what's new in the release blog

QWidget.setFocus() does not work

Scheduled Pinned Locked Moved Solved Qt for Python
6 Posts 3 Posters 2.7k 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.
  • nst0022N Offline
    nst0022N Offline
    nst0022
    wrote on last edited by nst0022
    #1

    I have a treeview and a textedit widget:

    self.treeview1 = QTreeView()
    self.textedit1 = MyTextEdit()
    

    where MyTextEdit has only two overridden methods:

    class MyTextEdit(QTreeView):
      def focusInEvent(self, event): 
        ...
      def focusOutEvent(self, event):
        ...
    

    Both widgets reside in a splitter:

      self.splitview1 = QSplitter()
    
      self.splitview1.addWidget(self.treeview1)
      self.splitview1.addWidget(self.textedit1)
    

    So far, so good.

    When I have finished editing the text in QTextEdit, I press a toolbar butten, where I process the modified text and then want to move the focus from QTextEdit to QTreeView:

    self.treeview1.setFocus(Qt.OtherFocusReason)
    

    Nothing happens, no error is indicated, the focus remains in QTextEdit.

    I tried all other focus reasons as well, to no avail.

    I know that setFocus() is a slot and I have not yet fully understood the slot concept.

    So, should this work at all?

    jsulmJ 1 Reply Last reply
    0
    • nst0022N nst0022

      I have a treeview and a textedit widget:

      self.treeview1 = QTreeView()
      self.textedit1 = MyTextEdit()
      

      where MyTextEdit has only two overridden methods:

      class MyTextEdit(QTreeView):
        def focusInEvent(self, event): 
          ...
        def focusOutEvent(self, event):
          ...
      

      Both widgets reside in a splitter:

        self.splitview1 = QSplitter()
      
        self.splitview1.addWidget(self.treeview1)
        self.splitview1.addWidget(self.textedit1)
      

      So far, so good.

      When I have finished editing the text in QTextEdit, I press a toolbar butten, where I process the modified text and then want to move the focus from QTextEdit to QTreeView:

      self.treeview1.setFocus(Qt.OtherFocusReason)
      

      Nothing happens, no error is indicated, the focus remains in QTextEdit.

      I tried all other focus reasons as well, to no avail.

      I know that setFocus() is a slot and I have not yet fully understood the slot concept.

      So, should this work at all?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @nst0022 said in QWidget.setFocus() does not work:

      class MyTreeView(QTreeView)

      I'm confused: don't you mean MyTextEdit here?
      Are you sure self.treeview1.setFocus(Qt.OtherFocusReason) is called (you do not show relevant code)?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      nst0022N 1 Reply Last reply
      2
      • jsulmJ jsulm

        @nst0022 said in QWidget.setFocus() does not work:

        class MyTreeView(QTreeView)

        I'm confused: don't you mean MyTextEdit here?
        Are you sure self.treeview1.setFocus(Qt.OtherFocusReason) is called (you do not show relevant code)?

        nst0022N Offline
        nst0022N Offline
        nst0022
        wrote on last edited by
        #3

        @jsulm Yes, sorry, I have updated the original post.

        Yes, the code is executed, I had added a print() statement, and I also tried instead sending a keypress event (Shift-Tab) to treeview1:

        keypress = QKeyEvent(QKeyEvent.KeyPress,          
                             Qt.Key_Tab,                  
                             Qt.ShiftModifier)            
                                                           
        QGuiApplication.sendEvent(self.treeview1, keypress)
        

        Same result, focus remained in QTextEdit.

        1 Reply Last reply
        0
        • nst0022N Offline
          nst0022N Offline
          nst0022
          wrote on last edited by
          #4

          I guess that @jsulm had answered my questen, whether it should work at all, with a Yes.

          So, I have created a minimum python script:

          2021-06-29_10.45.58.png

          where setFocus() does indeed work.

          So, somewhere in my 1900 lines Python script I mess something up, which I have to find now.

          JonBJ 1 Reply Last reply
          1
          • nst0022N nst0022

            I guess that @jsulm had answered my questen, whether it should work at all, with a Yes.

            So, I have created a minimum python script:

            2021-06-29_10.45.58.png

            where setFocus() does indeed work.

            So, somewhere in my 1900 lines Python script I mess something up, which I have to find now.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @nst0022 said in QWidget.setFocus() does not work:

            So, somewhere in my 1900 lines Python script I mess something up, which I have to find now.

            Can you use an eventFilter somewhere to detect a focus change event, to help you track it down?

            1 Reply Last reply
            0
            • nst0022N Offline
              nst0022N Offline
              nst0022
              wrote on last edited by
              #6

              Got it.

              In my specific logic, I had set

              self.treeview1.setEnabled(False)
              

              but not the corresponding

              self.treeview1.setEnabled(True)
              

              At the time I tried to set the focus, the widget was simply disabled.

              Now it works.

              Detecting this error was hampered by a style sheet I used, that had no entry for QTreeView:!active, so, enabled or not, it always looked the same.

              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