1 from python_qt_binding.QtCore import Qt
2 from python_qt_binding.QtGui import QIcon
3 try:
4 from python_qt_binding.QtGui import QLineEdit, QToolButton, QStyle
5 except:
6 from python_qt_binding.QtWidgets import QLineEdit, QToolButton, QStyle
7
8
10
12 QLineEdit.__init__(self, parent)
13
14 self.clearBtn = clearBtn = QToolButton(self)
15 icon = QIcon.fromTheme("edit-clear", QIcon(":/icons/crystal_clear_button_close.png"))
16 clearBtn.setIcon(icon)
17 clearBtn.setCursor(Qt.ArrowCursor)
18 clearBtn.setStyleSheet("QToolButton { border: none; padding: 0px; }")
19 clearBtn.hide()
20
21
22 clearBtn.clicked.connect(self.clear)
23 self.textChanged[str].connect(self.update_close_button)
24
25 frameWidth = self.style().pixelMetric(QStyle.PM_DefaultFrameWidth)
26 self.setStyleSheet("QLineEdit { padding-right: %dpx; } " % (clearBtn.sizeHint().width() + frameWidth + 1))
27 msz = self.minimumSizeHint()
28 self.setMinimumSize(max(msz.width(), clearBtn.sizeHint().height() + frameWidth * 2 + 2),
29 max(msz.height(), clearBtn.sizeHint().height() + frameWidth * 2 + 2))
30
32 sz = self.clearBtn.sizeHint()
33 frameWidth = self.style().pixelMetric(QStyle.PM_DefaultFrameWidth)
34 self.clearBtn.move(self.rect().right() - frameWidth - sz.width(),
35 (self.rect().bottom() + 1 - sz.height()) / 2)
36
39
41 '''
42 Enable the ESC handling
43 '''
44 if event.key() == Qt.Key_Escape and self.text():
45 self.setText('')
46 else:
47 event.accept()
48 QLineEdit.keyPressEvent(self, event)
49