Skip to content

Commit 94deba9

Browse files
committed
[Add] New pyqt_events snippets
1 parent 2a6bd07 commit 94deba9

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

pyqt_events/example.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
from PyQt4 import Qt, QtCore, QtGui
3+
4+
class MyLineEdit(QtGui.QLineEdit):
5+
6+
def __init__(self, *args):
7+
8+
QtGui.QLineEdit.__init__(self,*args)
9+
10+
self.last_value = "<value>"
11+
self.setStyleSheet("background-color: #aaa;")
12+
self.setText(self.last_value)
13+
14+
def mousePressEvent(self,ev):
15+
self.last_value = self.text()
16+
self.setStyleSheet("background-color: #fff;")
17+
self.setText("")
18+
19+
def keyPressEvent(self, e):
20+
if e.key() == QtCore.Qt.Key_Enter or e.key() == QtCore.Qt.Key_Return:
21+
print "Key pressed", e.key()
22+
self.checkRestore()
23+
24+
QtGui.QLineEdit.keyPressEvent(self, e)
25+
26+
def focusOutEvent(self, ev):
27+
self.checkRestore()
28+
QtGui.QLineEdit.focusOutEvent(self, ev)
29+
30+
def checkRestore(self):
31+
if str( self.text() ) == "":
32+
self.setText(self.last_value)
33+
self.setStyleSheet("background-color: #aaa;")
34+
35+
app = QtGui.QApplication([])
36+
win = QtGui.QMainWindow()
37+
but = MyLineEdit()
38+
win.setCentralWidget(but)
39+
win.show()
40+
app.exec_()
41+

0 commit comments

Comments
 (0)