Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLineEdit: clear mask when not editing
QtWS25 Last Chance

QLineEdit: clear mask when not editing

Scheduled Pinned Locked Moved Solved General and Desktop
qlineeditmask
5 Posts 2 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Justin Iurman
    wrote on 29 Oct 2018, 15:56 last edited by
    #1

    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

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 29 Oct 2018, 17:59 last edited by
      #2

      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

      1 Reply Last reply
      2
      • J Offline
        J Offline
        Justin Iurman
        wrote on 29 Oct 2018, 19:33 last edited by
        #3

        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

        M 1 Reply Last reply 29 Oct 2018, 19:42
        0
        • J Justin Iurman
          29 Oct 2018, 19:33

          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

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 29 Oct 2018, 19:42 last edited by mrjj
          #4

          @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 :)

          1 Reply Last reply
          1
          • J Offline
            J Offline
            Justin Iurman
            wrote on 30 Oct 2018, 16:18 last edited by Justin Iurman
            #5

            @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)

            1 Reply Last reply
            2

            1/5

            29 Oct 2018, 15:56

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved