Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Accessing text field and processing in python
Forum Updated to NodeBB v4.3 + New Features

Accessing text field and processing in python

Scheduled Pinned Locked Moved Unsolved Qt for Python
pythonqt for pythonpyside2pyside
1 Posts 1 Posters 764 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aayushlakkad
    wrote on 12 Jun 2022, 10:24 last edited by
    #1

    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
    

    main.py:

    # 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 outside get_credentials class for other tasks. so how can I do that, as it takes argument from

    1 Reply Last reply
    0

    1/1

    12 Jun 2022, 10:24

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved