Advertisement
Guest User

line edit event filter

a guest
Oct 23rd, 2018
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. class LineEditStyleEventFilter(QObject):
  2.  
  3.     def __init__(self, lineEdit, defaultStyle, placeholderStyle, **kwargs):
  4.         super().__init__(parent=lineEdit, **kwargs)
  5.         self.defaultStyle = defaultStyle
  6.         self.placeholderStyle = placeholderStyle
  7.         lineEdit.setStyleSheet(placeholderStyle)
  8.         lineEdit.installEventFilter(self)
  9.  
  10.    
  11.     def eventFilter(self, obj, event):
  12.         if isinstance(event, (QFocusEvent, QInputEvent)):
  13.             if obj.text():
  14.                 obj.setStyleSheet(self.defaultStyle)
  15.             else:
  16.                 obj.setStyleSheet(self.placeholderStyle)
  17.         return super().eventFilter(obj, event)
  18.  
  19.  
  20. # ...
  21. # LineEditStyleEventFilter(someLineEdit, 'color: black', 'color: red')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement