Accessing text field and processing in python
Unsolved
Qt for Python
-
Hello, I am trying to create a simple qt gui for login page. where I have 2 text fields and a button. I want to get the value of the text fields in python and then use those for logging in using API through python.
I was able to get the value of textfields in python using following code:
QML:import QtQuick 2.15 import QtQuick.Controls 2.15 import '../imports/Test' Item { id: login_item width: 800 height: 600 Rectangle { id: rectangle color: "#c2dcf7" anchors.fill: parent TextField { id: username objectName: "username" width: 220 height: 40 placeholderText: qsTr("Username") visible: true anchors.top: domain.bottom leftPadding: 10 topPadding: 8 font.pointSize: 12 anchors.topMargin: 25 anchors.horizontalCenter: parent.horizontalCenter } TextField { id: password objectName: "password" width: 220 height: 40 echoMode: TextInput.Password placeholderText: qsTr("Password") visible: true topPadding: 8 leftPadding: 10 anchors.top: username.bottom font.pointSize: 12 anchors.topMargin: 25 anchors.horizontalCenter: parent.horizontalCenter } Custom_btn { id: login_btn objectName: "login_btn" text: "Login" anchors.top: password.bottom anchors.topMargin: 50 anchors.horizontalCenter: parent.horizontalCenter onClicked: { var dom_name = domain.text var usrname = username.text var pwd = password.text // console.log(domain_name); credentials.get_domain_name(domain) credentials.get_user_name(usrname) credentials.get_password(pwd) } } Connections { target: credentials } } }
login_test.py:
from PySide6.QtCore import QObject, Slot, Signal, Property from PySide6.QtWidgets import QLineEdit class get_credentials(QObject): def __init__(self): QObject.__init__(self) self.domain_name = "" self.user_name = "" self.password = "" btn_signal = Signal(str) @Slot(str, result=str) def get_user_name(self, user): if self.user_name == user: return self.user_name = user # return self.user_name @Slot(str, result=str) def get_password(self, pwd): if self.password == pwd: return self.password = pwd # return self.password
# This Python file uses the following encoding: utf-8 # This Python file uses the following encoding: utf-8 import os from pathlib import Path import sys from api_login_test import get_credentials # from login_data import get_credentials from PySide6.QtCore import QCoreApplication, Qt, QUrl, QObject from PySide6.QtWidgets import QApplication, QLineEdit from PySide6.QtQml import QQmlApplicationEngine CURRENT_DIRECTORY = Path(__file__).resolve().parent def main(): app = QApplication(sys.argv) engine = QQmlApplicationEngine() credentials = get_credentials() engine.rootContext().setContextProperty('credentials', credentials) filename = os.fspath(CURRENT_DIRECTORY / "qml" / "main.qml") url = QUrl.fromLocalFile(filename) def handle_object_created(obj, obj_url): if obj is None and url == obj_url: QCoreApplication.exit(-1) engine.objectCreated.connect(handle_object_created, Qt.QueuedConnection) engine.load(url) tf = engine.rootObjects()[0] btn = tf.findChild(QObject,"login_btn") usr = tf.findChild(QObject, "domain") print(usr.property('text')) domnm = credentials.get_user_name(usr) print('user= {}'.format(domnm)) sys.exit(app.exec()) if __name__ == "__main__": main()
I am able to get values of username and password from qml file in
get_user_name()
method. now I want to call this method outsideget_credentials
class for other tasks. so how can I do that, as it takes argument from