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. SetFocus() on QLineEdit not working (after other widget [QPushButton] is removed )
Forum Updated to NodeBB v4.3 + New Features

SetFocus() on QLineEdit not working (after other widget [QPushButton] is removed )

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 3.4k 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.
  • P Offline
    P Offline
    pumpkinhead
    wrote on last edited by
    #1

    In the example below I invoke a QLineEdit after a QPushButton is pressed. I want an automatic focus on the QLineEdit which is working fine if I do NOT delete the QPushButton. If I delete the QPushButton, then the QLineEdit appears but has no focus. Why does removing the Button (in the way I do it) lead to a loss of focus for the QLineEdit and how can I fix this?

    One way of making it work is to put the marked (self.removeItem...) line below the .setFocus() line. However, this is not an option for the real program.
    Thanks,
    Torsten

    @
    #!/usr/bin/python

    -- coding: utf-8 --

    from PyQt4.QtGui import *

    class Form(QWidget):

    def drawtextBox(self):
        self.answerLine = QLineEdit()
        self.textBox = QHBoxLayout()
        self.textBox.addWidget(self.answerLine)
        self.mainLayout.addLayout(self.textBox)
    
    def drawcontinueBox(self):
        self.submitButton = QPushButton("Continue")
        self.continueBox = QHBoxLayout()
        self.continueBox.addWidget(self.submitButton)
        self.mainLayout.addLayout(self.continueBox)
        self.submitButton.clicked.connect(self.submitContinue)
    
    ## Routine to Remove a Component ("which" = which BoxLayout)
    def removeItem(self, which):
        for cnt in reversed(range(which.count())):
            widget = which.takeAt(cnt).widget()
            if widget is not None:
                widget.deleteLater()
    
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
    
        self.mainLayout = QVBoxLayout(self)
    
        self.drawcontinueBox()
    
        self.setLayout(self.mainLayout)
        self.setWindowTitle("Test")
    
    def submitContinue(self):
        self.removeItem(self.continueBox)      ## Here is the problem
    
        self.drawtextBox()
        self.answerLine.setFocus()
    

    if name == 'main':
    import sys
    app = QApplication(sys.argv)
    screen = Form()
    screen.show()
    sys.exit(app.exec_())
    @

    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