python command to "grab" text from Qt Designer fields and save to sqlite

Asked

Viewed 1,063 times

1

Good afternoon. I’m enthusiastic and I’m starting in python. With Tkinter I can send the text of the fields to the database with the function "get()", in gtk, if I’m not mistaken, with "get_text()". I would like to know what that role would be in Pyqt. Thank you for your attention.

  • Please edit the question and add a snippet of your code that lets us see how you name and access your fields.

1 answer

1

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.

Browser other questions tagged

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