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]Scope of variable QML\JS
QtWS25 Last Chance

[SOLVED]Scope of variable QML\JS

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 3.6k 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.
  • V Offline
    V Offline
    vamfirius
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vamfirius
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vamfirius
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • C Offline
          C Offline
          chriadam
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • V Offline
            V Offline
            vamfirius
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • V Offline
              V Offline
              vamfirius
              wrote on last edited by
              #6

              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.

              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