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. Decorating a line edit
Qt 6.11 is out! See what's new in the release blog

Decorating a line edit

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.4k Views 1 Watching
  • 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.
  • G Offline
    G Offline
    GrahamL
    wrote on last edited by
    #1

    Hi
    My boss has seen that some applications show a small icon in the corner of some editable fields and has asked if I can implement something similar.

    Can anyone give me some hints on how to achieve this?

    many thanks

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rondog
      wrote on last edited by
      #2

      Maybe a subclass of QLineEdit with a custom paintEvent().

      if the text is empty then you can display an icon, gray text, or any kind of suitable background. If not, just have a normal display.

      Something along these lines might work (didn't try it):

      @
      void MyLineEdit::paintEvent(QPaintEvent *event)
      {
      // this will draw the control normally
      QLineEdit::paintEvent(event);

      if(!this->text().length())
      {
      // get the rect of the editable part
      QRect rect = this->editable_rect();

      QPainter painter(...);

      // draw highlight color
      // draw icon
      // draw gray text
      ...
      }
      }
      @

      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        If, apart from looking pretty, the icon is also suppose to do something when clicked it's more convenient to do something like this:

        @
        QIcon icon = ... //get it from anywhere
        QAction* action = someLineEdit->addAction(icon, QLineEdit::LeadingPosition);
        connect(action, &QAction::triggered, /some slot/);
        @

        The second param of addAction determines the position of the icon inside the lineedit. It also supports multiple actions and icons, positions and clips the text etc..

        1 Reply Last reply
        0
        • G Offline
          G Offline
          GrahamL
          wrote on last edited by
          #4

          Thanks
          I will give it a go

          1 Reply Last reply
          0

          • Login

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