通过将内置QLineEdit设为“双击”来定义自定义小部件:
class Clickable_LineEdit(QtGui.QLineEdit):
def __init__(self, type, parent=None):
super(DCLineEdit, self).__init__(parent)
def mouseDoubleClickEvent(self, event):
print "CLICK"
现在,我可以使用此“自定义” Clickable_LineEdit 小部件来填充主对话框窗口。一切正常。但每次双击发生时,它都会在主对话框窗口类的外部注册...意味着所有main_dialog类的变量,所有留下的数据都不能用于 Clickable_LineEdit 类实例。
我想知道是否可以在不离开主对话框类的情况下从LineEdit获取doubleClick功能。
稍后编辑: 以下是来自以下帖子的修改示例:
from PyQt4 import QtCore, QtGui
class Clickable_LineEdit(QtGui.QLineEdit):
def __init__(self, parent=None):
super(QtGui.QLineEdit, self).__init__(parent)
def mouseDoubleClickEvent(self, event):
print "CLICK"
class Ui_Dialog(QtGui.QWidget):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(348, 195)
self.lineEdit = Clickable_LineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(50, 40, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
def runOnDoubleClick(self):
print "DOUBLE CLICK"
import sys
def main():
app = QtGui.QApplication(sys.argv)
ex = Ui_Dialog()
ex.setupUi(ex)
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
我在“Ui_Dialog()”类下添加了一个“ runOnDoubleClick ”函数我想在每次双击self.lineEdit小部件时运行(而不是mouseDoubleClickEvent()函数) 。如何实现?
答案 0 :(得分:1)
嗯,根据我的理解,你想要一个lineEdit,当lineEdit被双击时,它会感知一个双击信号。
from PyQt4 import QtCore, QtGui
class Clickable_LineEdit(QtGui.QLineEdit):
def __init__(self, parent=None):
super(QtGui.QLineEdit, self).__init__(parent)
def mouseDoubleClickEvent(self, event):
print "CLICK"
class Ui_Dialog(QtGui.QWidget):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(348, 195)
self.lineEdit = Clickable_LineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(50, 40, 113, 20))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
import sys
def main():
app = QtGui.QApplication(sys.argv)
ex = Ui_Dialog()
ex.setupUi(ex)
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
这似乎对我有用。