1.添加QICON
QAction *searchAction = new QAction(ui.LineEdit);
searchAction->setIcon(QIcon(":/resource/query.png"));
ui.LineEdit->addAction(searchAction,QLineEdit::TrailingPosition);
//QLineEdit::LeadingPosition 在左侧
注:资源路径“./pic.bmp” 表示编译生成路径,不是源码路径
QT工程添加资源文件
填写资源文件夹名称及创建路径
然后构建,右键资源COPYPATH
2.添加图片
QPushButton *getBackPushButton=new QPushButton("找回密码");
QLineEdit *passwordLineEdit=new QLineEdit();
//密码框设置文本的外边距,由于QPushButton是在右边的,所以要设置
passwordLineEdit->setTextMargins(0,0,getBackPushButton->width(),0);
QHBoxLayout *layout=new QHBoxLayout();
layout->setContentsMargins(0,0,0,0);//设置外边距
//以右对齐的方式添加QWidget
layout->addWidget(getBackPushButton,0,Qt::AlignRight);
passwordLineEdit->setLayout(layout);