【发布时间】:2018-04-07 06:21:22
【问题描述】:
我到处搜索,但找不到在 PyQt4 QLineEdit 中粘贴文本时触发插槽/事件的示例?
【问题讨论】:
-
Qt 中没有粘贴信号或事件。您可以使用
textChanged信号并使用QClipboard类检查新内容是否与剪贴板内容相同。
我到处搜索,但找不到在 PyQt4 QLineEdit 中粘贴文本时触发插槽/事件的示例?
【问题讨论】:
textChanged 信号并使用QClipboard 类检查新内容是否与剪贴板内容相同。
将以下代码添加到您的 MyForm 类中:
内部__init__()
self.ui.lineEdit_URL.textChanged.connect(self.valueChanged)
定义新方法:
def valueChanged(self, text):
if QtGui.QApplication.clipboard().text() == text:
self.pasteEvent(text)
定义另一个方法:
def pasteEvent(self, text):
# this is your paste event, whenever any text is pasted in the
# 'lineEdit_URL', this method gets called.
# 'text' is the newly pasted text.
print text
【讨论】:
pasteEvent。这与仅在发短信粘贴时被调用完全不同,这就是我解释 OP 问题的方式。
pasteEvent 仍然会被调用。但是,这并不一定意味着文本到达那里是因为它被粘贴了。虽然这可能对您的用例无关紧要。
您必须自己通过覆盖“keyPressEvent”来定义它。例如:
from PyQt4 import QtGui, QtCore
import sys
class NoteText(QtGui.QLineEdit):
def __init__(self, parent):
super (NoteText, self).__init__(parent)
def keyPressEvent(self, event):
if event.matches(QtGui.QKeySequence.Paste):
self.setText("Bye")
class Test(QtGui.QWidget):
def __init__( self, parent=None):
super(Test, self).__init__(parent)
le = QtGui.QLineEdit()
nt = NoteText(le)
layout = QtGui.QHBoxLayout()
layout.addWidget(nt)
self.setLayout(layout)
app = QtGui.QApplication(sys.argv)
myWidget = Test()
myWidget.show()
app.exec_()
【讨论】:
class MyForm(QtGui.QMainWindow): def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.threads = [] self.ui.pushButton_Start.clicked.connect(self.thread_start),请您帮忙。为此发布一个对新手友好的示例?
self.ui.lineEdit_URL.textChanged.connect(self.KeyDown) def KeyDown(self, event): # implement the method here if event.matches(QtGui.QKeySequence.Paste): print 'Pasted' 但这会导致AttributeError: 'QString' object has no attribute 'matches'