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. setFocus not working! I need this to work!

setFocus not working! I need this to work!

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 3.8k 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.
  • I Offline
    I Offline
    Inhahe
    wrote on 1 May 2021, 13:59 last edited by
    #1

    Here's a minimal (actually, it's probably not as minimal as it could be..) adaptation of my program that recreates the issue.
    Run the program and type ctrl-k into the QTextEdit with the focus (should be the bottom one) and a little widget will show up. I want the widget to remain showing and set the focus back onto the QTextEdit. Everything works except setting the focus back onto the QTextEdit.

    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from PyQt5.QtWidgets import *
    import sys
    
    app = QApplication(sys.argv)
    
    class MainWindow(QMainWindow):
      def __init__(self):
        QMainWindow.__init__(self)
        self.tab_widget = QTabWidget()
        self.vbox = QVBoxLayout() 
        self.vbox.addWidget(self.tab_widget)
        self.vbox.setContentsMargins(0, 0, 0, 0)
        self.setCentralWidget(self.tab_widget)
        self.showMaximized()
    
    class ChannelWindow(QWidget):
      def __init__(self):
        QWidget.__init__(self)
        self.tab_index = mainwin.tab_widget.addTab(self, "channel window")
        mainwin.tab_widget.setCurrentIndex(self.tab_index)
        self.textwindow = QTextEdit(self)
        self.splitter = QSplitter(self)
        self.splitter.addWidget(self.textwindow)
        self.vlayout = QVBoxLayout(self)
        self.vlayout.setContentsMargins(0, 0, 0, 0)
        self.vlayout.addWidget(self.splitter)
        self.editwindow = ChannelInputQTextEdit()
        self.editwindow.setFocus()
        self.vlayout.addWidget(self.editwindow)
        self.editwindow.setFixedHeight(40)
        self.editwindow.setFocus()
        
    class ChannelInputQTextEdit(QTextEdit):
      def keyPressEvent(self, event):
        if event.key() == 75 and (event.modifiers() & Qt.ControlModifier):
          widget = QDialog(channelwindow)
          label = QLabel()
          label.setText("Test")
          grid = QGridLayout()
          grid.addWidget(label, 1, 1, 1, 1)
          widget.setLayout(grid)
          widget.raise_()
          widget.show()
          size = widget.frameSize()
          w, h = size.width(), size.height()
          widget.move(max(self.mapToParent(self.cursorRect().topLeft()).x() - w/2, 0),  self.y() - h) 
          self.widget = widget
          self.setFocus()
        else:
          QTextEdit.keyPressEvent(self, event)
    
    mainwin = MainWindow()
    channelwindow = ChannelWindow()
    app.exec()
    
    1 Reply Last reply
    0
    • J Offline
      J Offline
      jeremy_k
      wrote on 1 May 2021, 22:45 last edited by
      #2

      Add a call to QWidget.activateWindow to the end of the keyPressEvent if clause.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      0
      • I Offline
        I Offline
        Inhahe
        wrote on 2 May 2021, 03:23 last edited by
        #3

        That worked, thanks!

        1 Reply Last reply
        0

        1/3

        1 May 2021, 13:59

        • Login

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