按钮放在QlineEdit内部,界面更紧凑,体现了按钮和文本框的强关联。
def addButton(self,lineEdit):btn = QtWidgets.QPushButton("")icon1 = QtGui.QIcon()icon1.addPixmap(QtGui.QPixmap(":/image/images/th.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)btn.setIcon(icon1)btn.setStyleSheet("""QPushButton {border: 0px solid #32CD32;}""")btn.setCursor(Qt.PointingHandCursor);widgetAction = QtWidgets.QWidgetAction(self);widgetAction.setDefaultWidget(btn);lineEdit.addAction(widgetAction,QtWidgets.QLineEdit.TrailingPosition);return btn
实现的如下效果: