How to add text to a Qplaintextedit(PYQT5)

Asked

Viewed 556 times

0

At first I would just like to know how to add values in a Textedit, however my goal is to take the values of the two comboBoxs and use them to generate the password.

Follow the code:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'GestorDeSenhas.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QToolTip,QMessageBox
from PyQt5.QtPrintSupport import QPrinter

class Ui_frameGerador(object):
    def setupUi(self, frameGerador):
        frameGerador.setObjectName("frameGerador")
        frameGerador.setEnabled(True)
        frameGerador.resize(489, 310)
        frameGerador.setMinimumSize(QtCore.QSize(489, 0))
        frameGerador.setMaximumSize(QtCore.QSize(927, 310))
        frameGerador.setStyleSheet("QWidget#frameGerador{\n"
"background-color: white;\n"
"\n"
"}\n"
"\n"
"QPushButton{\n"
"color: black;\n"
"height: 25px;\n"
"background-color: rgba(255, 255, 255, 1);\n"
"border-radius: 10px;\n"
"border: 2px solid rgb(212, 212, 212);\n"
"\n"
"}\n"
"\n"
"QPushButton:hover{\n"
"\n"
"background-color: rgb(0, 170, 255);\n"
"}")
        self.formLayout = QtWidgets.QFormLayout(frameGerador)
        self.formLayout.setContentsMargins(20, -1, 20, -1)
        self.formLayout.setObjectName("formLayout")
        self.label_2 = QtWidgets.QLabel(frameGerador)
        self.label_2.setMaximumSize(QtCore.QSize(16777215, 50))
        font = QtGui.QFont()
        font.setPointSize(15)
        font.setBold(True)
        font.setUnderline(False)
        font.setWeight(75)
        self.label_2.setFont(font)
        self.label_2.setAlignment(QtCore.Qt.AlignCenter)
        self.label_2.setObjectName("label_2")
        self.formLayout.setWidget(2, QtWidgets.QFormLayout.SpanningRole, self.label_2)
        self.label_3 = QtWidgets.QLabel(frameGerador)
        font = QtGui.QFont()
        font.setPointSize(11)
        self.label_3.setFont(font)
        self.label_3.setObjectName("label_3")
        self.formLayout.setWidget(6, QtWidgets.QFormLayout.LabelRole, self.label_3)
        self.cboxQntsSenhas = QtWidgets.QSpinBox(frameGerador)
        self.cboxQntsSenhas.setFocusPolicy(QtCore.Qt.WheelFocus)
        self.cboxQntsSenhas.setStyleSheet("")
        self.cboxQntsSenhas.setMinimum(1)
        self.cboxQntsSenhas.setProperty("value", 1)
        self.cboxQntsSenhas.setObjectName("cboxQntsSenhas")
        self.formLayout.setWidget(6, QtWidgets.QFormLayout.FieldRole, self.cboxQntsSenhas)
        self.label = QtWidgets.QLabel(frameGerador)
        font = QtGui.QFont()
        font.setPointSize(11)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.formLayout.setWidget(7, QtWidgets.QFormLayout.LabelRole, self.label)
        self.cboxDigitos = QtWidgets.QSpinBox(frameGerador)
        self.cboxDigitos.setFocusPolicy(QtCore.Qt.WheelFocus)
        self.cboxDigitos.setStyleSheet("broder-radius:20pt;")
        self.cboxDigitos.setMinimum(1)
        self.cboxDigitos.setObjectName("cboxDigitos")
        self.formLayout.setWidget(7, QtWidgets.QFormLayout.FieldRole, self.cboxDigitos)
        self.radioMaiusculasMinusculas = QtWidgets.QRadioButton(frameGerador)
        font = QtGui.QFont()
        font.setItalic(False)
        font.setUnderline(False)
        self.radioMaiusculasMinusculas.setFont(font)
        self.radioMaiusculasMinusculas.setChecked(False)
        self.radioMaiusculasMinusculas.setAutoRepeat(False)
        self.radioMaiusculasMinusculas.setAutoExclusive(False)
        self.radioMaiusculasMinusculas.setObjectName("radioMaiusculasMinusculas")
        self.formLayout.setWidget(9, QtWidgets.QFormLayout.LabelRole, self.radioMaiusculasMinusculas)
        self.radioCaracEspeciais = QtWidgets.QRadioButton(frameGerador)
        self.radioCaracEspeciais.setIconSize(QtCore.QSize(16, 16))
        self.radioCaracEspeciais.setChecked(False)
        self.radioCaracEspeciais.setAutoRepeat(False)
        self.radioCaracEspeciais.setAutoExclusive(False)
        self.radioCaracEspeciais.setObjectName("radioCaracEspeciais")
        self.formLayout.setWidget(10, QtWidgets.QFormLayout.LabelRole, self.radioCaracEspeciais)
        self.txtBoxResultado = QtWidgets.QPlainTextEdit(frameGerador)
        self.txtBoxResultado.setEnabled(True)
        self.txtBoxResultado.setMaximumSize(QtCore.QSize(16777215, 100))
        self.txtBoxResultado.setObjectName("txtBoxResultado")
        self.formLayout.setWidget(11, QtWidgets.QFormLayout.SpanningRole, self.txtBoxResultado)
        self.frameButtons = QtWidgets.QFrame(frameGerador)
        self.frameButtons.setMinimumSize(QtCore.QSize(50, 50))
        self.frameButtons.setStyleSheet("QFrame#frameButtons{\n"
"border: 1px;\n"
"\n"
"}")
        self.frameButtons.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frameButtons.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frameButtons.setObjectName("frameButtons")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.frameButtons)
        self.horizontalLayout.setContentsMargins(0, -1, 0, -1)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.btnGerar = QtWidgets.QPushButton(self.frameButtons)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.btnGerar.sizePolicy().hasHeightForWidth())
        self.btnGerar.setSizePolicy(sizePolicy)
        self.btnGerar.setMinimumSize(QtCore.QSize(0, 0))
        self.btnGerar.setSizeIncrement(QtCore.QSize(1, 0))
        self.btnGerar.setAutoDefault(False)
        self.btnGerar.setDefault(True)
        self.btnGerar.setObjectName("btnGerar")
        self.horizontalLayout.addWidget(self.btnGerar)
        self.btnSair = QtWidgets.QPushButton(self.frameButtons)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.btnSair.sizePolicy().hasHeightForWidth())
        self.btnSair.setSizePolicy(sizePolicy)
        self.btnSair.setObjectName("btnSair")
        self.horizontalLayout.addWidget(self.btnSair)
        self.formLayout.setWidget(14, QtWidgets.QFormLayout.SpanningRole, self.frameButtons)

        self.retranslateUi(frameGerador)
        self.btnSair.clicked.connect(frameGerador.close)
        QtCore.QMetaObject.connectSlotsByName(frameGerador)
        frameGerador.setTabOrder(self.cboxQntsSenhas, self.cboxDigitos)
        frameGerador.setTabOrder(self.cboxDigitos, self.radioMaiusculasMinusculas)
        frameGerador.setTabOrder(self.radioMaiusculasMinusculas, self.radioCaracEspeciais)
        frameGerador.setTabOrder(self.radioCaracEspeciais, self.btnGerar)
        frameGerador.setTabOrder(self.btnGerar, self.btnSair)
        frameGerador.setTabOrder(self.btnSair, self.txtBoxResultado)


        self.btnGerar.clicked.connect(self.addValorGerado)


    def addValorGerado(self):
        self.txtBoxResultado.clear()







    def retranslateUi(self, frameGerador):
        _translate = QtCore.QCoreApplication.translate
        frameGerador.setWindowTitle(_translate("frameGerador", "Form"))
        self.label_2.setText(_translate("frameGerador", "Gerador de senhas"))
        self.label_3.setText(_translate("frameGerador", "Quantas senhas:"))
        self.label.setText(_translate("frameGerador", "Digitos: "))
        self.radioMaiusculasMinusculas.setText(_translate("frameGerador", "Incluir Maiusculas e minusculas"))
        self.radioCaracEspeciais.setText(_translate("frameGerador", "Incluiir caracteres especiais"))
        self.btnGerar.setText(_translate("frameGerador", "Gerar"))
        self.btnSair.setText(_translate("frameGerador", "Sair"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    frameGerador = QtWidgets.QWidget()
    ui = Ui_frameGerador()
    ui.setupUi(frameGerador)
    frameGerador.show()
    sys.exit(app.exec_())

1 answer

1

The Widget PlainTextEdit has associated with it an object of the type QTextDocument - is this object that has the methods and attributes both to read the user’s typed content, or to change the content, or to configure the presentation. (Already aspects like cursor location, width of the same, are configured directly in the object QPlainTextEdit)

The method .document() returns this object associated with the editor, which in turn has the method .setHtml() which allows you to insert the desired content in the editor. You can send a string with any valid HTML, including inline style - Qt will automatically translate your HTML to the style it uses internally (text-formatted extensions use the tag <span ...>, even if they are sent with the tag <h1>, for example).

So in your case, you could just do, at the desired point:

doc = self.txtBoxresultado.document()
doc.setHtml("""<h1>Senhas Geradas:</h1>abc123<br/>qazwsx<br/>123456<br/>""")

And here’s the tip for the next time you ask a question: Reduce your code to only contain what you need to solve - a minimal, self-contained, verifiable example - that is, that anyone with Pyqt5 installed can copy and run, and that has only the window and the Texteditor. Sending the question this way you force people to study your entire program, to take a question just punctual.

  • 1

    Qplaintextedit has slots so you don’t need Document directly insertPlainText(const QString & text) appendPlainText(const QString & text) setPlainText(const QString & text) - interestingly has even the appendHtml(const QString & html), that although it does not accept tables and floats, serves for formatting paragraphs via HTML. Docs (en)

  • hmm -- true - I’m more used to Tkinter, which always requires intermediate objects to handle the text of widgets. Python bindings don’t even need this casting - normal Python strings can be used in all these methods.

  • Thank you so much! I’m a beginner, and had tried every way I could imagine to do it.

  • Really thank you! It helped me a lot!

  • @jsbueno ah, is that I copied straight from the original manual (C++), is just description of the same type. I usually get it straight from the Qt documentation, although in practice I usually use bindings in Harbour and even Py’s more rarely. The wrapper for Harbour that I use also accepts, whenever possible, the native types

Browser other questions tagged

You are not signed in. Login or sign up in order to post.