PyQt:自定義QLineEdit禁止選中復制粘貼


說明

  自定義的QLineEdit,當輸入文本之后,禁止選中復制粘貼等操作

 

實現方法

  MyQLineEdit類繼承了QLineEdit類,並重寫QLineEdit類中的mouseMoveEvent方法和keyPressEvent方法

  這樣還可以自己定義一些其他操作,比如ouseDoubleClickEvent 鼠標雙擊之類的

 

 1 class MyQLineEdite(QLineEdit):
 2     
 3     def __init__(self):
 4         super(MyQLineEdite, self).__init__()
 5 
 6     def mouseMoveEvent(self, mouse_event):
 7         '''
 8         重寫鼠標的左鍵右鍵選中
 9         :param mouse_event: 
10         :return: 
11         '''
12         if mouse_event.buttons == Qt.LeftButton or  mouse_event.buttons() == Qt.RightButton:
13             return
14         
15     def keyPressEvent(self, key_event):
16         '''
17         重寫鍵盤的全選,復制粘貼
18         :param key_event: 
19         :return: 
20         '''
21         if key_event == QKeySequence.SelectAll:  #禁止全選
22             return 
23         if key_event == QKeySequence.Paste:  #禁止粘貼
24             return 
25         if key_event == QKeySequence.Copy:  #禁止復制
26             return 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM