Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Below is a nearly direct translation of the Qt code from <a href="http://blog.qt.digia.com/blog/2007/06/06/lineedit-with-a-clear-button/" rel="noreferrer">here</a>. </p> <p>Differences:</p> <ul> <li>button is always visible</li> <li>clicking on the button emits <code>buttonClicked(bool)</code> signal</li> </ul> <p>Code:</p> <pre class="lang-py prettyprint-override"><code>from PyQt4 import QtGui, QtCore class ButtonLineEdit(QtGui.QLineEdit): buttonClicked = QtCore.pyqtSignal(bool) def __init__(self, icon_file, parent=None): super(ButtonLineEdit, self).__init__(parent) self.button = QtGui.QToolButton(self) self.button.setIcon(QtGui.QIcon(icon_file)) self.button.setStyleSheet('border: 0px; padding: 0px;') self.button.setCursor(QtCore.Qt.ArrowCursor) self.button.clicked.connect(self.buttonClicked.emit) frameWidth = self.style().pixelMetric(QtGui.QStyle.PM_DefaultFrameWidth) buttonSize = self.button.sizeHint() self.setStyleSheet('QLineEdit {padding-right: %dpx; }' % (buttonSize.width() + frameWidth + 1)) self.setMinimumSize(max(self.minimumSizeHint().width(), buttonSize.width() + frameWidth*2 + 2), max(self.minimumSizeHint().height(), buttonSize.height() + frameWidth*2 + 2)) def resizeEvent(self, event): buttonSize = self.button.sizeHint() frameWidth = self.style().pixelMetric(QtGui.QStyle.PM_DefaultFrameWidth) self.button.move(self.rect().right() - frameWidth - buttonSize.width(), (self.rect().bottom() - buttonSize.height() + 1)/2) super(ButtonLineEdit, self).resizeEvent(event) </code></pre> <p>Usage:</p> <pre class="lang-py prettyprint-override"><code>import sys from PyQt4 import QtGui def buttonClicked(): print 'You clicked the button!' if __name__ == '__main__': app = QtGui.QApplication(sys.argv) main = ButtonLineEdit('/path/to/my_fancy_icon.png') main.buttonClicked.connect(buttonClicked) main.show() sys.exit(app.exec_()) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload