3

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?

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
ohad987
  • 326
  • 2
  • 4
  • 15

2 Answers2

3

Another way is

self.this_widget.clearFocus()

after window has been shown. Only in Qt5 placeholder texts are displayed even with focus. So maybe switch to PyQt5.

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
  • 1
    Plus one for `clearFocus` and the different focus behaviour in Qt5. However, I don't understand why you think `text()` should return the placeholder text: it doesn't do that in Qt-5.4, and it would be a nasty bug if it did. But in any case, on the rare occasions that you might want that behaviour, you can easily do `text = widget.text() or widget.placeholderText()`. – ekhumoro Jan 06 '15 at 20:20
  • @ekhumoro Thanks for the comment. I think I made an error here with the behavior of text(). It was what I remembered but obviously wrong. It's more that I wanted this behavior because I liked placeholders as form of defaults and your `text() or placeholderText()` solves it elegantly. Your Qt knowledge is really excellent. I had to upvote it quite often in the last days. ;) – NoDataDumpNoContribution Jan 06 '15 at 20:34
  • 1
    As an aside, it is unfortunate that Qt5 keeps placeholder text even with focus. Every UI design guideline I've seen that is from past couple years is clear that on focus, the hint should disappear, it it easy to think that there is a value in the box, or the box is readonly. – Oliver Nov 13 '15 at 05:12
  • @Schollii Good observation. The problem is here that one component is automatically focused. The real solution would therefore probably be that no component is focused in Qt when a widget is made visible. It should be possible to display and bring something into foreground without input focusing. – NoDataDumpNoContribution Nov 13 '15 at 09:12
  • I'm not sure that's a valid design guideline to be honest. The textbox I am typing this is doesn't follow the guideline actually. The reason why I am saying this is that when you display a dialog you should always focus the most important widget in that dialog. For example, in a login dialog, if there is not default hostname, then focus that by default because that is the first thing a user will need to fill. Then make sure tab takes you to login and password fields next and that enter logs you in. No need to take your hands off the keyboard. So clearing placeholders on focus is bad IMHO. – fronsacqc Aug 23 '16 at 13:02
  • @fronsacqc I agree with you. Please note, that the other answer suffers from exactly the same problem (suggest to set focus to some other widget which might not be the most important widget where you want the focus on). The better solution, especially now, is using PyQt5 which does not suffer from the problem (you can have focus and displayed placeholder text). – NoDataDumpNoContribution Aug 23 '16 at 13:29
  • 1
    I was looking into fixing this issue yesterday and unfortunately my code is going to run in environments Qt5 is not avaiable (Houdini, Nuke, etc). I ended up subclassing QLineEdit, overrode the paintEvent method and reimplement the placeholder rendering based on the C++ source code (http://code.metager.de/source/xref/lib/qt/src/gui/widgets/qlineedit.cpp#1928). It was pretty straightforward and it seems like it holds up. As for when Qt5 is available, my code uses QLineEdit directly. – fronsacqc Aug 24 '16 at 15:35
1

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.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Thank you! I created the GUI using Qt Designer, so I changed the tab order and it works as expected. – ohad987 Dec 20 '14 at 12:13