If your input field is a QtWidget.QLineEdit
, you get text with the call to method .text()
.
If it’s a field like QtWidgets.QTextEdit
, you first pick up a reference to QTextDocument
associated, and then you can have all the content in editing with a call to the method .toPlainText()
;
conteudo = widget.document().toPlainText()
App type "hello world" to always display the last line typed in the editing area:
from PyQt5 import QtWidgets
import sys
def update(document, line):
text = document.toPlainText()
last_line = text.split("\n")[-1]
line.setText(last_line)
def main():
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
manager = QtWidgets.QGridLayout(window)
area = QtWidgets.QTextEdit()
line = QtWidgets.QLineEdit()
document = area.document()
document.contentsChange.connect(lambda: update(document, line))
manager.addWidget(area, 0, 0)
manager.addWidget(line, 1, 0)
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
About this program: the line document.contentsChange.connect(lambda: update(document, line))
connects the text content change signal with an anonymous function set in the same place: lambda: update(document, line)
- Why Qt signals do not send other parameters together. As in callback update
want access to Document and the other widget, lambda is called without parameters, and calls update
passing the variables I want to have access to in the other function as parameters.
Please edit the question and add a snippet of your code that lets us see how you name and access your fields.
– jsbueno