QTextEdit text edit box and text box browse QTextBrowser

Chapter VI QTextEdit text edit box and text box browse QTextBrowser
6.1 synchronous display text

6.2 Summary

6.1 synchronous display text
believe Chinese name these two controls you should also know what the functions of these two controls is that the former is used to edit the text, which can not be edited, only for display. We will explain the usage of these two controls by completing the following procedure:

QTextEdit controls for the left and right for QTextBrowser control. When entering text on the left, to the right of the display will be synchronized. Look at the code below:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QTextEdit, QTextBrowser, QHBoxLayout, QVBoxLayout
 
 
class Demo(QWidget):
    def __init__(self):
        super(Demo, self).__init__()
        self.edit_label = QLabel('QTextEdit', self)
        self.browser_label = QLabel('QTextBrowser', self)
        self.text_edit = QTextEdit(self)
        self.text_browser = QTextBrowser(self)
 
        self.edit_v_layout = QVBoxLayout()
        self.browser_v_layout = QVBoxLayout()
        self.all_h_layout = QHBoxLayout()
 
        self.layout_init()
        self.text_edit_init()
 
    def layout_init(self):
        self.edit_v_layout.addWidget(self.edit_label)
        self.edit_v_layout.addWidget(self.text_edit)
 
        self.browser_v_layout.addWidget(self.browser_label)
        self.browser_v_layout.addWidget(self.text_browser)
 
        self.all_h_layout.addLayout(self.edit_v_layout)
        self.all_h_layout.addLayout(self.browser_v_layout)
 
        self.setLayout(self.all_h_layout)
 
    def text_edit_init(self):
        self.text_edit.textChanged.connect(self.show_text_func)  # 1
 
    def show_text_func(self):
        self.text_browser.setText(self.text_edit.toPlainText())  # 2
 
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = Demo()
    demo.show ()
    sys.exit (app.exec_ ())
procedure is very simple. By way of example of two QLabel, a QTextEdit QTextBrowser and then through a horizontal layout and vertical layout can complete the entire interface. The key point is the connection signal and grooves.

1. Connect self.text_edit signal onto the groove textChanged custom functions. That is when self.text_edit text is changed, it will send out textChanged signal, then call show_text_func () function slot.

2. In the function we pass trough setText () method to set the text self.text_browser self.text_edit text, and the text self.text_edit () obtained by toPlainText, rather than text ().

 

Interestingly, when we enter the Html code edit box, browse box on the right will be its implementation:

Here only a brief introduction to use these controls, but in fact has also been very good enough. When used after other methods such as two controls, it will further detail.

 

6.2 Summary
1. As the name implies, is used to edit text QTextEdit, for displaying text and QTextBrowser;

2. setText () to set the text, toPlainText () to get the text, these methods have two controls;

3. Browse box will execute Html code.
----------------
 

Published 136 original articles · won praise 71 · views 160 000 +

Guess you like

Origin blog.csdn.net/u012308586/article/details/104965523