Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. emiting Signal from C++ and showing Error Dialog Problem:
QtWS25 Last Chance

emiting Signal from C++ and showing Error Dialog Problem:

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 3 Posters 565 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.
  • M Offline
    M Offline
    Mohamad Ayrot
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • I Offline
      I Offline
      Imynn
      wrote on last edited by
      #2

      Probably it's because target is the same, and both dialogs react on the signal. You need to check whether the operation is made in Registration or Login and open dialog for only one of these. Maybe there's another way though.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mohamad Ayrot
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • I Offline
          I Offline
          Imynn
          wrote on last edited by
          #4

          Maybe you could add a property to both of these files, like property bool isCurrent: false and when StackView currentItem changes, you change the property and just add check in connection if (isCurrent) then show dialog. I haven't used StackView 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.

          1 Reply Last reply
          0
          • GrecKoG Offline
            GrecKoG Offline
            GrecKo
            Qt Champions 2018
            wrote on last edited by GrecKo
            #5

            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.

            1 Reply Last reply
            1
            • M Offline
              M Offline
              Mohamad Ayrot
              wrote on last edited by
              #6

              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.

              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