class SearchLineEdit(QLineEdit): def __init__(self, text="", clearable=True, parent=None): QLineEdit.__init__(self, text=text, parent=parent) searchPixmap = QPixmap(str(path("icons/search.png"))) clearPixmap = QPixmap(str(path("icons/clear.png"))) self.clearButton = QToolButton(self) self.clearButton.setIcon(QIcon(clearPixmap)) self.clearButton.setIconSize(QSize(16, 16)) self.clearButton.setCursor(Qt.ArrowCursor) self.clearButton.setStyleSheet( "QToolButton { border: none; padding: 0 px;}") self.clearButton.hide() if clearable: self.clearButton.clicked.connect(self.clear) self.textChanged.connect(self.updateCloseButton) self.searchButton = QToolButton(self) self.searchButton.setIcon(QIcon(searchPixmap)) self.searchButton.setIconSize(QSize(16, 16)) self.searchButton.setStyleSheet( "QToolButton { border: none; padding: 0 px;}") frameWidth = self.style().pixelMetric(QStyle.PM_DefaultFrameWidth) self.setStyleSheet( "QLineEdit { padding-left: %spx; padding - right: % spx;} " % (self.searchButton.sizeHint().width() + frameWidth + 1, self.clearButton.sizeHint().width() + frameWidth + 1)) msz = self.minimumSizeHint() self.setMinimumSize( max( msz.width(), self.searchButton.sizeHint().width() + self.clearButton.sizeHint().width() + frameWidth * 2 + 2), max(msz.height(), self.clearButton.sizeHint().height() + frameWidth * 2 + 2), ) # self.searchMenu = QtGui.QMenu(self.searchButton) # self.searchButton.setMenu(self.searchMenu) # self.searchMenu.addAction("Google") # self.searchButton.setPopupMode(QtGui.QToolButton.InstantPopup) def resizeEvent(self, event): sz = self.clearButton.sizeHint() frameWidth = self.style().pixelMetric(QStyle.PM_DefaultFrameWidth) self.clearButton.move(self.rect().right() - frameWidth - sz.width(), (self.rect().bottom() + 1 - sz.height()) / 2) self.searchButton.move(self.rect().left() + 1, (self.rect().bottom() + 1 - sz.height()) / 2) def updateCloseButton(self, text): if text: self.clearButton.setVisible(True) else: self.clearButton.setVisible(False)
class SearchLineEdit(QLineEdit): """Line edit search widget with icon and remove all button""" def __init__(self, parent=None, icon=True): super(SearchLineEdit, self).__init__(parent) self.setTextMargins(1, 0, 20, 0) if icon: self.setTextMargins(18, 0, 20, 0) self._label = QLabel(self) self._pixmap_icon = QPixmap(get_image_path('conda_search.png')) self._label.setPixmap(self._pixmap_icon) self._label.setStyleSheet('''border: 0px; padding-bottom: 0px; padding-left: 2px;''') self._pixmap = QPixmap(get_image_path('conda_del.png')) self.button_clear = QToolButton(self) self.button_clear.setIcon(QIcon(self._pixmap)) self.button_clear.setIconSize(QSize(18, 18)) self.button_clear.setCursor(Qt.ArrowCursor) self.button_clear.setStyleSheet("""QToolButton {background: transparent; padding-right: 2px; border: none; margin:0px; }""") self.button_clear.setVisible(False) # Layout self._layout = QHBoxLayout(self) self._layout.addWidget(self.button_clear, 0, Qt.AlignRight) self._layout.setSpacing(0) self._layout.setContentsMargins(0, 2, 2, 0) # Signals and slots self.button_clear.clicked.connect(self.clear_text) self.textChanged.connect(self._toggle_visibility) self.textEdited.connect(self._toggle_visibility) def _toggle_visibility(self): """ """ if len(self.text()) == 0: self.button_clear.setVisible(False) else: self.button_clear.setVisible(True) def sizeHint(self): return QSize(200, self._pixmap_icon.height()) # Public api # ---------- def clear_text(self): """ """ self.setText('') self.setFocus()