[SOLVED]Scope of variable QML\JS



  • Hi everybody.
    I am from Russia, and I don,t now english well, so please don,t kick me too much for this.
    I am just starting learning QML so don`t kick me for stupid questions too.

    I have main.qml page which contains just one Rectangle element and Image element, which is used like close button -
    @
    import QtQuick 1.1
    import com.nokia.symbian 1.1
    import "lib.js" as LibJs
    Rectangle{
    .....
    Component.onCompleted: LibJs.initView("MainMenu.qml")
    ....
    }
    @

    MainMenu.qml contains-

    @
    import QtQuick 1.1
    import com.nokia.symbian 1.1
    import "lib.js" as LibJs

    Rectangle {
    height: 300
    anchors.verticalCenterOffset: 0
    anchors.verticalCenter: parent.verticalCenter
    rotation:90

    Button {
        id: button1
        height: 40
        text: "Начать"
        anchors.horizontalCenter: parent.horizontalCenter
        onClicked: LibJs.initView("DeckMemory.qml")
    }
    

    }
    @

    File lib.js which is imported into main.qml contains code -

    @
    var currentView;

    function initView(file){
    try{
    currentView.destroy();
    }catch(e){}
    var c = Qt.createComponent(file);
    currentView = c.createObject(window,{});
    }
    @

    Everything works ok, exept one thing - variable currentView is undefined when I click on a Button which is in a MainMenu.qml, when I click it next time it is defined and contains Rectangle object, but it is not what I want, because the first loaded object doesn`t destroyed and I see menu when the game is started.



  • Tell me if you need more information to understand my problem, as I told I,m new in QML and I gave those info which is usefull as I think.



  • I have a guess that it can be because of imports of lib.js in all files which I use. But if it is so, how can I use one import, just in main.qml? If I use import of lib.js just in main.qml functions which I call in other files do not whork...



  • I'm a bit confused about what the underlying question is, but here is some information which I hope will help:

    1. if you are importing the same js file from multiple QML files, and want it to be "shared", you need to specify (at the top of the js file) ".pragma library". That will ensure that it has a single, shared context which is used no matter which QML file it is imported from. Note that a library js cannot access symbols from parent contexts, so "currentView" (whatever that is) might not be visible / available. You'll have to pass currentView as a function parameter instead.

    2. depending on what "currentView" is there may be parenting / ownership issues. Is it a property of another QML element, or what? What is "window" - is it the id of another element?

    Cheers,
    Chris.



  • chriadam

    1. Thanks for this information, but I can`t figure out how to use it for resolving my problem.

    If I uses ".pragma library" as you said, I cant use qml objects in my js code. Problem now is I don,t understand how can I pass "currentView" as a function parametr. "currentView" - must be qml object which even havent got an id because I cant set "id" whith a createObject() method. So question is how can I access to dynamicly created qml object, which havent got "id" from qml document?

    1. "currentView" - js variable which contains qml object(Im trying to create a simple game, so currentView contains such things like game menu, game field, results screen etc. it is not an array, so each time it contains one of this things) "window" - id of PageStackWindow element which located in main.qml(in my example of code in a first post I didnt show it, because I thought that it is not important)

    May be I should post all my code here, since it`s realy difficult to explain my problem well on a language which is foreign for me.

    P.S May be there is more valid way to manage templates of the application, if it is, give me a link on a material please. Before c++\qt(qml) I was working with a PHP\JS\HTML\CSS for about two years and it`s difficult to me to understand how can I manage templates without "include" function.



  • As I thought there is very simple and convinient way to make template management in qml. Using PageStack element gives me all posible freedom I needed. I can imagine how much my english is bad, so I didn`t expected for quick answer. Thanks for your help chriadam, your comment was helpfull for me in a process of understanding QML.


Log in to reply
 

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