I'm trying to make a lineEdit widget have place holder text (look like 'MM/DD/YYYY') when not in focus and have input masking (looks like ' / / ', or you can put in slashes through other means) when in focus.
Currently the input mask setInputMask('99/99/9999')
takes precedence and I can't figure out a way to put a hasFocus (or similiar) signal on a LineEdit widget.
How would you implement both of these functionalities in one widget? Would it be easier to leave the place holder text as is and create a method that adds in a slash after entering a certain number of characters?
Also, the flicking cursor is really thick when you set a mask. What's up with that and how do you revert it?
Full code:
from PySide import QtCore, QtGui
class Ui_MainWindow(object):
def myChanges(self):
self.lineEdit.installEventFilter(self.lineEdit.setInputMask('99/99/9999'))
self.lineEdit.setPlaceholderText('MM/DD/YYYY')
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(557, 351)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtGui.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(100, 130, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtGui.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(320, 130, 113, 20))
self.lineEdit_2.setObjectName("lineEdit_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 557, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.myChanges()
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
If you want a custom behavior of a widget you must create a class that inherits from this, in your case of QLineEdit. To get your specific objective you must use the focusInEvent and focusOutEvent methods as described in the following section:
class LineEdit(QtGui.QLineEdit):
def __init__(self, parent=None):
QtGui.QLineEdit.__init__(self, parent=parent)
self.setPlaceholderText('MM/DD/YYYY')
def focusInEvent(self, event):
self.setInputMask('99/99/9999')
def focusOutEvent(self, event):
self.setInputMask('')
And then you should change to:
self.lineEdit = QtGui.QLineEdit(self.centralwidget)
to:
self.lineEdit = LineEdit(self.centralwidget)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With