How to place an icon onto a QLineEdit?

Simple Way for Dummies

  1. Add a QLineEdit, and set it frameless by QLineEdit::setFrame
  2. Add a QLabel with background color in white (by stylesheet) and a icon
  3. Combine the line edit and the label with a layout, set spacing to 0
  4. Set placeholder text with QLineEdit::setPlaceholderText

Result

enter image description here


Advanced Way

Check this thread: "Can QLineEdit do this?"

And the related python code: http://bazaar.launchpad.net/~henning-schroeder/%2Bjunk/qtwidgets/annotate/head:/qtwidgets/lineedit.py

Or

"How to do - inside in QLineEdit insert the button.[pyqt4]"

Basically customized a QLineEdit by painting a widget(label, button or even combobox) onto it. Then reset the margin, cursor, padding and the paint event. No magics!


QLineEdit* _lineEdit = new QLineEdit();
_lineEdit->setClearButtonEnabled(true);
_lineEdit->addAction(":/resources/search.ico", QLineEdit::LeadingPosition);
_lineEdit->setPlaceHolderText("Search...");

extracted from: http://saurabhg.com/programming/search-box-using-qlineedit/