QLineEdit设置icon的2种方式

方式一 采用qss:

QLineEdit 
{
    background: #f3f3f3;
    background-image: url(:Images/search.svg); /* actual size, e.g. 16x16 */
    background-repeat: no-repeat;
    background-position: left;
    color: #252424;
    font-family: SegoeUI;
    font-size: 12px;
    padding: 2 2 2 20; /* left padding (last number) must be more than the icon's width */
    border: 1px solid #dddddd;
}
ps:注意要设置border: 1px solid #dddddd;不然会出现背景图标闪烁(应该是qt的bug,也许高版本已经修复)

方式二 、 代码实现:

QAction * pActLeft = new QAction(this);
pActLeft->setIcon(QIcon(":/Images/123.png"));
QLineEdit edt(this);
edt.addAction(pActLeft,QLineEdit::LeadingPosition);

你可能感兴趣的:(Qss)