PyQt5實(shí)現(xiàn)QLineEdit添加clicked信號的方法
大家都知道很多控件是沒有clicked信號的,我在網(wǎng)上找了很多終于總結(jié)出2個(gè)方法來實(shí)現(xiàn)類似需求,比如給QLineEdit添加clicked信號,這樣的話,當(dāng)點(diǎn)擊輸入框時(shí)就會發(fā)送clicked信號,其它控件也是一樣的做法,如下:
方法1:創(chuàng)建一個(gè)繼承自QLineEdit的類,然后重寫mousePressEvent。
class MyLineEdit(QLineEdit):
clicked = pyqtSignal()
def mouseReleaseEvent(self, QMouseEvent):
if QMouseEvent.button()==Qt.LeftButton:
self.clicked.emit()
方法2:重寫eventFilter事件也可以達(dá)到類似的效果。
def eventFilter(self, widget, event):
if widget == self.edit:
if event.type() == QEvent.FocusOut:
pass
elif event.type() == QEvent.FocusIn:
self.clicked.emit() #當(dāng)焦點(diǎn)再次落到edit輸入框時(shí),發(fā)送clicked信號出去
else:
pass
return False
test.py
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
#方法1
class mylineedit(QLineEdit):
clicked=pyqtSignal() #定義clicked信號
def mouseReleaseEvent(self, QMouseEvent):
if QMouseEvent.button()==Qt.LeftButton:
self.clicked.emit() #發(fā)送clicked信號
class Wind(QDialog):
clicked=pyqtSignal()
def __init__(self):
super().__init__()
self.lnd=mylineedit()
self.edit=QLineEdit()
self.edit.installEventFilter(self) #方法2(1)
vb=QVBoxLayout()
vb.addWidget(self.lnd)
vb.addWidget(self.edit)
self.setLayout(vb)
self.lnd.clicked.connect(self.showData)
self.clicked.connect(self.showData) #該clicked信號是W1的信號而非edit的信號,但可以實(shí)現(xiàn)焦點(diǎn)落到edit時(shí)觸發(fā)信號
# 方法2(2)
def eventFilter(self, widget, event):
if widget == self.edit:
if event.type() == QEvent.FocusOut:
pass
elif event.type() == QEvent.FocusIn:
self.clicked.emit() #當(dāng)焦點(diǎn)再次落到edit輸入框時(shí),發(fā)送clicked信號出去
else:
pass
return False
def showData(self):
print('ok')
if __name__=="__main__":
app=QApplication(sys.argv)
w=Wind()
w.show()
sys.exit(app.exec_())
以上這篇PyQt5實(shí)現(xiàn)QLineEdit添加clicked信號的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Pycharm中安裝Pygal并使用Pygal模擬擲骰子(推薦)
這篇文章主要介紹了Pycharm中安裝Pygal并使用Pygal模擬擲骰子,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
python+selenium 鼠標(biāo)事件操作方法
今天小編就為大家分享一篇python+selenium 鼠標(biāo)事件操作方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
python使用正則搜索字符串或文件中的浮點(diǎn)數(shù)代碼實(shí)例
這篇文章主要介紹了python使用正則搜索字符串或文件中的浮點(diǎn)數(shù)代碼實(shí)例,同時(shí)包含一個(gè)讀寫到文件功能,需要的朋友可以參考下2014-07-07
Python應(yīng)用自動化部署工具Fabric原理及使用解析
這篇文章主要介紹了Python應(yīng)用自動化部署工具Fabric原理及使用解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
對Python subprocess.Popen子進(jìn)程管道阻塞詳解
今天小編就為大家分享一篇對Python subprocess.Popen子進(jìn)程管道阻塞詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
Pytorch實(shí)現(xiàn)List?Tensor轉(zhuǎn)Tensor,reshape拼接等操作
這篇文章主要介紹了Pytorch實(shí)現(xiàn)List?Tensor轉(zhuǎn)Tensor,reshape拼接等操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
python使用numpy尋找二維數(shù)組的最值及其下標(biāo)方法分析
這篇文章主要為大家介紹了python使用numpy尋找二維數(shù)組的最值及其下標(biāo)實(shí)現(xiàn)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08

