問題描述
文本內容一旦改變則觸發事件
解決方案
使用裝飾器定義信號和槽函數,結合文本改變事件 textChanged
代碼
import sys
from PyQt5.QtCore import pyqtSlot, QMetaObject
from PyQt5.QtWidgets import QApplication, QWidget, QTextEdit, QVBoxLayout, QLabel
class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.label = QLabel(self)
self.edit = QTextEdit()
self.edit.setObjectName("edit")
layout = QVBoxLayout()
layout.addWidget(self.label)
layout.addWidget(self.edit)
self.setLayout(layout)
QMetaObject.connectSlotsByName(self)
@pyqtSlot()
def on_edit_textChanged(self):
text = self.edit.toPlainText()
self.label.setText(text)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())