Reputation: 1361
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_())
Upvotes: 4
Views: 2872
Reputation: 680
Novice here, I was looking for just that. eyllanesc pointed in the right direction, but a few things may have changed. QtGui.QLineEdit
is not a thing strictly speaking, or else it's just a subtlety between PyQt (what I use here) and PySide.
QLineEdit
has methods setFocus
, setFocusPolicy
, setFocusProxy
, but unfortunately none to help us here. Subclassing is the simplest answer (that I know). This is for a "general" answer, not for patching a ui file:
from PyQt5 import QtWidgets as qtw
class NewLineEdit(qtw.QLineEdit):
def __init__(self, parent):
super().__init__(parent)
def focusInEvent(self, event):
self.setInputMask("99/99/9999")
def focusOutEvent(self, event):
self.setInputMask("")
Then, in your MainWindow
or whatever:
dateField = NewLineEdit(self)
dateField.setPlaceholderText("MM/DD/YYYY")
It can be refined without having to hardcode the mask in the bespoke widget.
class NewLineEdit(qtw.QLineEdit):
def __init__(self, parent, mymask, *args, **kwargs):
self.mymask = mymask
super().__init__(parent, *args, **kwargs)
def focusInEvent(self, event):
self.setInputMask(self.mymask)
def focusOutEvent(self, event):
self.setInputMask("")
Then
dateField = NewLineEdit(self, "99/99/9999")
dateField.setPlaceholderText("MM/DD/YYYY")
PyQt 5.15.9
Qt 5.15.10
Python 3.10.13
Upvotes: 0
Reputation: 243975
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)
Upvotes: 3