我不确定这是正确的方法。您可以使用mousePressEvent
:
import sys
from PyQt4 import QtGui, QtCore
def text_change():
print "change ok"
def text_click(event): # <--- NOTE: event object is passed.
print "clicked ok "
app = QtGui.QApplication(sys.argv)
textEdit = QtGui.QTextEdit()
textEdit.setGeometry(QtCore.QRect(30, 20, 351, 51))
textEdit.setObjectName("textEdit")
textEdit.mousePressEvent = text_click
QtCore.QObject.connect(textEdit, QtCore.SIGNAL("textChanged()"), text_change)
textEdit.show()
sys.exit(app.exec_())
用 PyQt 4.10.2 测试。(窗口 7,Python 2.7.5)
更新
mousePressEvent
也适用于 PyQt5。
import sys
from PyQt5 import QtCore, QtWidgets
def text_change():
print("change ok")
def text_click(event):
print("clicked ok ")
app = QtWidgets.QApplication(sys.argv)
textEdit = QtWidgets.QTextEdit()
textEdit.setGeometry(QtCore.QRect(30, 20, 351, 51))
textEdit.setObjectName("textEdit")
textEdit.mousePressEvent = text_click
textEdit.textChanged.connect(text_change)
textEdit.show()
sys.exit(app.exec_())
使用 PyQt 5.0(Windows 7、Python 3.3.2)测试