How to use the information of a text field in qml in a py file

Asked

Viewed 27 times

-2

Next, I have 2 Qml files, the first has multiple text Fields that I want to extract the contents and allocate in a python variable then work with this variable in an algorithm and then export the result of this variable in another qml file. Basically, Usuaria enters the data. I work with the data and then I play a response to this data in another part of my app... follows code snippet to facilitate the visualization:

Row {
    id: inputRow1
    width: 400
    height: 30
    anchors.top: parent.top
    anchors.topMargin: 5

    PlusButton{
        anchors.left: parent.left
        anchors.leftMargin: 0
        onClicked: {
            if(inputRow3.visible == false){
                inputRow2.visible = true
                inputRow9.anchors.topMargin = 40
            }
        }
    }

    TextFieldBar{
        id: playerTfb
        text:qsTr("ola")
        width: 200
        height: 30
        anchors.left: parent.left
        font.pointSize: 12
        anchors.leftMargin: 40
        placeholderTextColor: "#92959e"
        colorOnFocus: "#a89f62"
        colorMouseOver: "#918955"
        colorDefault: "#767045"
        hoverEnabled: true
        placeholderText: "Player Name"
    }

    TextFieldBar {
        id: altTfb
        width: 60
        height: 30
        anchors.left: playerTfb.right
        hoverEnabled: true
        colorMouseOver: "#918955"
        placeholderTextColor: "#92959e"
        colorDefault: "#767045"
        anchors.leftMargin: 10
        colorOnFocus: "#a89f62"
        placeholderText: "#Alts"
        maxLenghtTfb: 1
        horizontalAlignment: Text.AlignHCenter
        font.pointSize: 12
    }

}

1 answer

1

I don’t know if it helps but I’ve tried it now and I think it might help:

QML: main.qml

    import QtQuick 2.13
    import QtQuick.Window 2.13
    import QtQuick.Controls 2.12
    import QmlTools 1.0

    Window {
      width: 640
      height: 480
      visible: true
      title: "Sample..."

    Tools {
        id: pyTools
    }

    TextField {
        id: _textField1
        width: 300
        height: 40
        onTextChanged: pyTools.setValueFromQml(text)
    }
}

Python: main.py

# This Python file uses the following encoding: utf-8
import sys
import os

from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine
from PySide2.QtQuick import QQuickView

from PySide2.QtQml import qmlRegisterType
from PySide2.QtCore import QUrl
from PySide2 import QtCore

textField1 = ""

class MainApp(QtCore.QObject):

    def __init__(self):
        QtCore.QObject.__init__(self)

    @QtCore.Slot(str)
    def setValueFromQml(self, data):
        textField1 = data
        print("VALUE: ", textField1)

if __name__ == "__main__":
    app = QGuiApplication(sys.argv)

    qmlRegisterType(MainApp, 'QmlTools', 1, 0, 'Tools')

    engine = QQmlApplicationEngine()
    engine.load(os.path.join(os.path.dirname(__file__), "main.qml"))

    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec_())

I hope I’ve helped!

Browser other questions tagged

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