Is there a way to toggle a button in qml from python (PySide6)
-
Im trying to test a login window in qml.
To do that i need to toggle
button
's and enter text to thetextField
, 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: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) } } }
-
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
-
@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...
-
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.
-
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.
-
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.