Decorating a line edit
-
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
-
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
...
}
}
@ -
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..
-
Thanks
I will give it a go