Search code examples
pythonqtpyqtfocusqlineedit

Automatic focus on showing deletes the placeholder text of a QLineEdit


I use PyQt4 and Python 2.7.9.

My program contains a few QLineEdit objects. The problem is that when the program is launched, one of the QLineEdits is being focused automatically, which causes my placeholder text to disappear.

Is there any way to prevent it, or at least don't let it hide the placeholder text?


Solution

  • You can use setFocus to put the focus on a different widget (although, depending on which widget you pick, you might also need to set the focus-policy first):

        self.some_other_widget.setFocusPolicy(QtCore.Qt.TabFocus)
        self.some_other_widget.setFocus()
    

    Alternatively, if you use Qt Designer to create the GUI, you could edit the tab-order so that the line-edit is not the first in the chain. This can also be done programmatically using QWidget.setTabOrder.