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. Is there a way to toggle a button in qml from python (PySide6)
Forum Updated to NodeBB v4.3 + New Features

Is there a way to toggle a button in qml from python (PySide6)

Scheduled Pinned Locked Moved Unsolved Qt for Python
6 Posts 3 Posters 829 Views 1 Watching
  • 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.
  • N Offline
    N Offline
    NYBL
    wrote on last edited by NYBL
    #1

    Im trying to test a login window in qml.

    To do that i need to toggle button's and enter text to the textField, is there a way to do it without signals?

    so far i'v found that i can interact with the QObject level of the qml items (but that only to set properties as far as i know) like this:

    main.py:

            myApp = QtGui.QGuiApplication(sys.argv)
            myEngine = QtQml.QQmlEngine()
            directory = os.path.dirname(os.path.abspath(__file__))
            component = QtQml.QQmlComponent(myEngine,('some_path')
            obj = component.create()
            if component.status() == QtQml.QQmlError:
                return -1
            uname = obj.findChild(QtCore.QObject, "usernameField")
            print(uname.property("width"))
    

    main.qml:

    ApplicationWindow{
        
        id: window 
        width: 400
        height: 580
        visible: true
        title: qsTr("חלון כניסה")
        maximumHeight: height
        maximumWidth: width
        minimumHeight: height
        minimumWidth: width
    
        // SET FLAGS
        flags: Qt.WindowCloseButtonHint | Qt.WindowMinimizeButtonHint | Qt.CustomizeWindowHint |  Qt.MSWindowsFixedSizeDialogHint | Qt.WindowTitleHint
    
        // SET MATERIAL STYLE
        Material.theme: Material.Dark
        Material.accent: Material.LightBlue
        Item{
            TextField{
                id: usernameField
                objectName : "usernameField" 
                Layout.preferredWidth: 300
                text: qsTr("")
                selectByMouse: true
                placeholderText: qsTr("Username")
                anchors.horizontalCenter: parent.horizontalCenter       
                anchors.topMargin: 60
            }
    
            // TEXT FIELD USERNAME
            TextField{
                id : passwordField
                Layout.preferredWidth: 300
                text: qsTr("")
                selectByMouse: true
                placeholderText: qsTr("Password")
                Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
                anchors.topMargin: 10
                echoMode: TextInput.Password
                objectName: "foo_object"
             }
              Button{
                   id: buttonLogin
                   Layout.preferredWidth: 100
                   text: qsTr("Login")
                   onClicked: funcs.login(usernameField.text, passwordField.text)                         
             }  
       }
    }
    
    1 Reply Last reply
    0
    • ndiasN Offline
      ndiasN Offline
      ndias
      wrote on last edited by
      #2

      Hi @NYBL,

      Can you provide more details about your problem?

      You can set usernameField text from Python side using:

      uname = obj.findChild(QtCore.QObject, "usernameField")
      uname.setProperty('text', 'Some text here.......')
      

      but I didn't realize if that was really your question.

      Regards

      1 Reply Last reply
      0
      • N Offline
        N Offline
        NYBL
        wrote on last edited by NYBL
        #3

        @ndias said in Is there a way to toggle a button in qml from python (PySide6):

        :

        Hey, i know i can set properties but i need to trigger the push button that says 'Login'
        in order to test the functionality of my backend logic.

        For example if user press submit with blank username and password
        my backend sends a response that the fields can be empty, and so on.

        P.S I know i can set some signals to handle the problem but that's a mess...

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          I haven't used it but Qt has QML support in the QtTest module which might help you.

          On a side note, you should be pro-active and not let your users enter invalid values like blank credentials. This will avoid sending useless requests to your backend and be more user friendly.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          N 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            I haven't used it but Qt has QML support in the QtTest module which might help you.

            On a side note, you should be pro-active and not let your users enter invalid values like blank credentials. This will avoid sending useless requests to your backend and be more user friendly.

            N Offline
            N Offline
            NYBL
            wrote on last edited by
            #5

            @SGaist
            Hi, Thanks for the reply.
            About your side note you say that i should do those checking inside qml (js) rather then in python?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Use a validator to ensure the input is valid and enable the send button when it's the case. The validator job is not to ensure that the entered value is correct but that it follows the requirements for its use.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

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