emiting Signal from C++ and showing Error Dialog Problem:
-
i send error string using signals from C++ to QML.
i used Connections method as follows:Connections{ target: UserRegistration function onErrorApi(errorMessage){ errorDialog.open() errorTxt.text = errorMessage } }
i used it twice on in Registration.qml and the other in Login.qml.
when the signal emits, the two dialogs in Login.qml and in Registration.qml open up. but the operation made by Registration.qml file not Login.qml.
i use StackView element to change between Login and Registration -
what am trying to do is, i send request to API and the get the response back,
after processing the API response i want to check if there is an error or not then printing the the error message in QML Registration or Login pages (depends which page sends the request to API ).the response function is this:
QJsonDocument jDoc = QJsonDocument::fromJson(response); if(jDoc.object().contains("error")){ QJsonArray jArray = jDoc.object()["error"].toObject()["errors"].toArray(); for(auto i: jArray){ QString errorMessage = i.toObject()["message"].toString(); qDebug() << "ERROR Message: " << errorMessage; emit errorApi(errorMessage); } } else if(jDoc.object().contains("kind")){ qDebug() << response.data(); m_idToken = jDoc.object().value("idToken").toString(); m_userId = jDoc.object().value("localId").toString(); qDebug() << m_userId; qDebug() << "ID TOKEN: " << m_idToken; emit userSignedIn(m_userId); }
Registration.qml & Login.qml Connections{ target: UserRegistration function onErrorApi(errorMessage){ errorDialog.open() errorTxt.text = errorMessage } }
when emitting errrorApi(errorMessage), i wanted to show the message in Dialog but because am accessing the signal using Connections in both Registration.qml and Login.qml it is showing both dialogs above each other. but the thing is i wanted to get response error just from registration page not Login.
is there anyway to handle this issue without using lots of functions on signals ?
i -
Maybe you could add a property to both of these files, like
property bool isCurrent: false
and whenStackView
currentItem changes, you change the property and just add check in connection if (isCurrent) then show dialog. I haven't usedStackView
so I don't know for sure, but maybe this will help somehow. If it helps, it still doesn't sound like a good solution. Maybe someone else can suggest a better one. -
You can use a
StackView
attached property for that:StackView.visible
:Page { id: pageInStackView Connections { enabled: pageInStackView.StackView.visible target: UserRegistration function onErrorApi(errorMessage) { errorDialog.open() errorTxt.text = errorMessage } } }
Alternatively you can put the Connections elsewhere in a place that is always loaded, not in one of the page you push on the StackView.
-
Thank you, i made the connections in mian.qml and it worked.
other solution i used this in Login and Registration files.login.qml if(stack.depth == 2){ function onErrorApi(errorMessage) { errorDialog.open() errorTxt.text = errorMessage } } registration.qml if(stack.depth == 1){ function onErrorApi(errorMessage) { errorDialog.open() errorTxt.text = errorMessage } }
but making it in main.qml with one Connections was better solution for that.