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



  • 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_())
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.