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. [SOLVED]QtQuick function if else condition issue

[SOLVED]QtQuick function if else condition issue

Scheduled Pinned Locked Moved QML and Qt Quick
13 Posts 3 Posters 22.9k 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.
  • T Offline
    T Offline
    Traxx
    wrote on last edited by
    #1

    Hello all.I have a problem with function if else condition.In my main.qml which import QtQuick 1.1 and use PageStackWindow component.On Component.onCompleted,it is suppose to call a function below which open either dialogone or dialogtwo but why both dialog open at the same time?If it match both dialog open but if it doesn't match then only dialogtwo open.In other qml with Page component,if else condition doesn't have this problem.

    //PROBLEMATIC FUNCTION IN MAIN QML
    @
    function matchString() {
    for(var i=0;i<imei.count;i++)
    if(imei.get(i).imeistring === deviceInfo.imei ) {
    dialogone.open()
    }
    else {
    dialogtwo.open()
    }
    }@

    //OTHER FUNCTION THAT WORK IN OTHER QML
    @onStopped: {
    if (status == Player.EndOfStatus) {
    nextbanner.open()
    if (loopButton.state == "shuffle") {
    Player.start()
    }
    else if(Player.state == pause") {
    Player.play()
    }
    else {
    GoNext.start()
    }
    }
    }
    @

    1 Reply Last reply
    0
    • L Offline
      L Offline
      Lucijan
      wrote on last edited by
      #2

      Have you tried checking the console output? It's hard to say without more code, but it could be that one of them opens in one iteration, and the other opens in another. The for loop is quick so it could look like it's happening at the same time.

      Try changing it to this and see what happens:

      @function matchString() {
      for(var i=0;i<imei.count;i++)
      if(imei.get(i).imeistring === deviceInfo.imei ) {
      console.log("Iteration: " + i + ", true");
      dialogone.open();
      }
      else {
      console.log("Iteration: " + i + ", false");
      dialogtwo.open()
      }
      }@

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Traxx
        wrote on last edited by
        #3

        I'm sorry you dont't understand.I though posting just a function is enough.Below is the whole qml.The model is just a listmodel with a couple dozen of listelement and contain single roles(imeistring) only.What bug me is why the same if else condition work on Page component but not on PageStackWindow.

        The example you advice didn't work.Still the same.

        @
        import QtQuick 1.1
        import com.nokia.symbian 1.1
        import QtMobility.systeminfo 1.2

        PageStackWindow {
        id: window
        initialPage: Control { id: mainPage}
        showStatusBar: true
        showToolBar: false

        QtObject{
            id: dynamiccreate
            property Component append:null;
            function create(qmlfile){
                append=Qt.createComponent(qmlfile);
                append.createObject(mainPage)
            }
        }
        
        ImeiModel {
            id: imei
        }
        
        function matchString() {
            for(var i=0;i<imei.count;i++)
                if(imei.get(i).imeistring === deviceInfo.imei ) {
                    console.log("Iteration: " + i + ", true");
                    testdialog.open();
                }
                else {
                    console.log("Iteration: " + i + ", true");
                    verifyfaildialog.open();
                }
            }
        
        DeviceInfo {
            property string deviceimei: deviceInfo.imei
            id: deviceInfo
            }
        
        QueryDialog {
            id: verifyfaildialog
            titleText: "WARNING !!!"
            message: "Goddamm fail again"
            rejectButtonText: "Back"
            onRejected: {
                verifyfaildialog.close()
            }
        }
        
        QueryDialog {
            id: testdialog
            titleText: "TEST"
            message: "Finally it is working"
            rejectButtonText: "Back"
            onRejected: {
                testdialog.close()
            }
        }
        
        StatusBar {
                id: statusBar
                anchors.top: window.top
                Text {
                    id: statusbar
                    text:"JustAnApp"
                    color: "blue"
                }
            }
        
        Component.onCompleted: {
            window.matchString()
        }
        

        }@

        1 Reply Last reply
        0
        • O Offline
          O Offline
          onek24
          wrote on last edited by
          #4

          Traxx, Lucijan just wanted to know the output so it would be easier to understand your problem and find a solution. It was obviously not a fix or a solution. Why don't you post us the output of Lucijans function? Also you didn't provide us your model imei. This model would probably be necessary for a solution.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            Traxx
            wrote on last edited by
            #5

            Oh sorry for misreading.Did you mean this ?I purposely leave other message as it is private.I

            [Qt Message] Iteration: 0, true
            [Qt Message] Iteration: 1, true
            [Qt Message] Iteration: 2, true
            [Qt Message] Iteration: 3, true
            [Qt Message] Iteration: 4, true

            and the listmodel is below

            @ListModel {
            id: imeimodel

                    ListElement{imeistring: "354863595" }
                    ListElement{imeistring: "354870672" }
                    ListElement{imeistring: "359050560" } //MY specific
                    ListElement{imeistring: "354875072" }
                    ListElement{imeistring: "358261609" }
             }
            

            @

            1 Reply Last reply
            0
            • O Offline
              O Offline
              onek24
              wrote on last edited by
              #6

              Yes i meant this. The ListModel shouldn't be the problem, but your function is false for testings. Please replace the else{} in your function with:

              @else {
              console.log("Iteration: " + i + ", false");
              verifyfaildialog.open();
              }@

              After you have done this post us the output again so we can see when it hits the else.

              1 Reply Last reply
              0
              • T Offline
                T Offline
                Traxx
                wrote on last edited by
                #7

                Sorry for my typo.Thank you guys for being so helpful.The help is very appreciated

                [Qt Message] Iteration: 0, false
                [Qt Message] Iteration: 1, false
                [Qt Message] Iteration: 2, true
                [Qt Message] Iteration: 3, false
                [Qt Message] Iteration: 4, false

                1 Reply Last reply
                0
                • O Offline
                  O Offline
                  onek24
                  wrote on last edited by
                  #8

                  bq. Sorry for my typo.Thank you guys for being so helpful.The help is very appreciated

                  It's no problem and you're welcome.

                  The third ListElement in your ListModel is your imei. When your function iterates trough the imeis, it checks for your imei and if i look at your output it returns true when it hits the third value. It all looks fine, where is the problem or what do you want to manage?

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    Traxx
                    wrote on last edited by
                    #9

                    The function is suppose to match listmodel database imei with imei string using qml deviceInfo.imei to query device imei .If the imei match then the testdialog should open otherwise verifyfaildiaolog that open.The problem is if it not match verifyfaildialog open but if it match both dialog open when it should open testdialog only.

                    If else condition work in my other function and qml signal correctly and expected but this particular function just won't work.

                    1 Reply Last reply
                    0
                    • O Offline
                      O Offline
                      onek24
                      wrote on last edited by
                      #10

                      Alright. Looks like you just misunderstood the for loop. It iterates trough each Element in your model, it even continues if it already has a dialog open. Opening a dialog is no return or break.

                      Just replace your for-loop with this one and it should work:

                      @for(var i = 0; i < imei.count; i++){
                      if(imei.get(i).imeistring === deviceInfo.imei){
                      testdialog.open()
                      return
                      }
                      }
                      verifyfaildialog.open()@

                      It's pretty simple: It iterates trough all of your Elements in your model until it found the imei which matches your imei. If that happens it opens the right dialog and quits the function. It will continue with the lines after the for-loop and open the fail dialog if it could not find your imei in your Model.

                      For further information about the for-loop please read:
                      "C++ For-loop Tutorial":http://www.tutorialspoint.com/cplusplus/cpp_for_loop.htm
                      "Javascript For-loop Tutorial":http://www.w3schools.com/js/js_loop_for.asp

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        Traxx
                        wrote on last edited by
                        #11

                        Thanks,i will try the code tommorrow.Thanks very much for helping me learn new stuff

                        1 Reply Last reply
                        0
                        • O Offline
                          O Offline
                          onek24
                          wrote on last edited by
                          #12

                          You're welcome. Feel free to ask if you have got more questions and don't forget to add "[Solved]" at the beginning of this threads title.

                          1 Reply Last reply
                          0
                          • T Offline
                            T Offline
                            Traxx
                            wrote on last edited by
                            #13

                            Thanks all.The code work as suggested.This topic is solved

                            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