[SOLVED]QtQuick function if else condition issue



  • 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()
    }
    }
    }
    @



  • 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()
    }
    }@



  • 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()
    }
    

    }@



  • 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.



  • 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" }
     }
    

    @



  • 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.



  • 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



  • 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?



  • 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.



  • 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



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



  • 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.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.