2

I have a QLineEdit with a PlaceholderText.

I want to clear the PlaceholderText only when a person starts typing, else the blinking cursor and PlacehoderText both should be there in that QLineEdit.

It is the first field of the page, so I have set the focus to this QLineEdit, but the PlaceholderText disappears as soon as this page is displayed.

Please suggest if I'll have to add a SIGNAL/SLOT for this QLIneEdit, so that the PlaceholderText doesn't get cleared off.

NorthCat
  • 9,643
  • 16
  • 47
  • 50
Ejaz
  • 1,504
  • 3
  • 25
  • 51

2 Answers2

1

In PyQt4 it's not a bug but a feature. You can't edit this behaviour. In PyQt5, the placeholder text is shown until the text is not empty.

A simple way to solve problem is to focus some way before QLintEdit. When the user press TAB button, next focus is QLintEdit.

Bandhit Suksiri
  • 3,390
  • 1
  • 18
  • 20
0

Here you go:

from PyQt4 import QtGui

class LE(QtGui.QLineEdit):

    def __init__(self, parent=None, starttext="Sample"):
        QtGui.QLineEdit.__init__(self, parent)
        self.start = True
        self.setText(starttext)

    def keyPressEvent(self, e):
        if e.text():
            if self.start:
                self.clear()
                self.start = False
                e.accept()           
        QtGui.QLineEdit.keyPressEvent(self,e)
mdurant
  • 27,272
  • 5
  • 45
  • 74