QLineEdit: clear mask when not editing
-
Hi folks,
I did some research on this subject, without success. Is it possible to make a QLineEdit (with an input mask) display its "true" text (without the substitution characters of the mask) when it is not being edited ? I mean, I see several ways of doing it, but is there already something existing that does what I want, without reinventing the wheel ?
Thanks,
Justin
-
Hi
As far as I know it will display the mask and i have not seen option to change that
http://doc.qt.io/qt-5/qlineedit.html#echoMode-prop -
Hi mrjj,
Yep, that’s what I thought too. Actually, I was thinking about a solution where I would override the paint() method (for instance, don’t know if it’s appropriate in this case) to display text or mask depending on the focus status. Another idea was to add/remove the mask only when needed, which means when entering in edit mode (QLineEdit gets the focus). The final objective is to mimic the behavior I ended up with a QStyledItemDelegate using a QLineEdit as editor. I mean, when the item does not have the focus, it simply displays the normal text (without mask format). But, when it gets the focus (entering inside, in edit mode), it displays the text with mask format. Any idea how to make it properly ?
Justin
-
@Justin-Iurman
Well, the paintEvent override should be easy to make as you can just
either call the normal QLineEdit::paintEvent or
simply call the QStyle function to paint the frame etc and then just
print the text you want.
However, if you need disabled state etc, then slightly more code is needed.
There is also left/right text etc so might not be super trivial if you need all features.
https://code.woboq.org/qt5/qtbase/src/widgets/widgets/qlineedit.cpp.html
line 1930 shows how it paints.
The QStyledItemDelegate is not a bad idea either
https://forum.qt.io/topic/92246/qstyleditemdelegate-with-qlineedit
but im not sure if will just need extra code for the actual editing and
same amount of draw code.oh, and welcome to the forums :)
-
@mrjj
Thanks for the welcoming :-)Indeed, the QStyledItemDelegate works fine, but only when I use a QTableWidget. So a first solution was to create a QTableWidget with 1 row and 1 column, and insert my delegate inside. The problem with this technique is that it's hard to perfectly mimic a QLineEdit (behavior, style, etc). For instance, with a QTableWidget, there is no simple click on items to enter in edit mode (this is a double click, at least), as far as I know. It's still possible to redefine the behavior, I guess, but it's not worth it. I don't like the whole thing.
That's why I came up with another idea. This time, I keep my QLineEdit as is, and I add/remove the input mask when having/losing focus. Thanks to that, I'm now able to see the "true" text (without mask format) when I'm not editing it (QLineEdit does not have the focus), while I can see the text (with mask format) when I'm editing it (has focus). I know it's not totally elegant, but I don't think there is an elegant solution at all for this "problem".
For those interested, a quick example on how to achieve this:
(1) install an event filter (installEventFilter()) on the QLineEdit
(2) filter on focus in/out
(2.1) focus in: add the mask (setInputMask())
(2.1) focus out: remove the mask and set the text to the value before the mask was removed (because removing the mask erases the text inside the QLineEdit)