PyQt5多行文本框QTextEdit文本改變事件

問題描述

文本內容一旦改變則觸發事件

在這裏插入圖片描述




解決方案

使用裝飾器定義信號和槽函數,結合文本改變事件 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_())




參考文獻

  1. PyQt之單行文本框(QLineEdit)控件中的常用方法和信號
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章