Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Javascript in qml error.
Forum Updated to NodeBB v4.3 + New Features

Javascript in qml error.

Scheduled Pinned Locked Moved General and Desktop
14 Posts 2 Posters 5.0k Views 1 Watching
  • 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.
  • J Offline
    J Offline
    Julie1986
    wrote on last edited by
    #1

    Could anyone help me in fixing this error. I am fed up of this. I get an error from js file stating "check.js:6: ReferenceError: Can't find variable: model"

    my code is as follows. .qml file

    @
    import QtQuick 1.1
    import "check.js" as Chk

    Rectangle{

    width:400
    height:width

    XmlListModel {
    id: model
    source: "example.xml"
    query: "/rss/channel/item"

    XmlRole { name: "title"; query: "title/string()" }
    XmlRole { name: "pubDate"; query: "pubDate/string()" }
    XmlRole { name: "id"; query: "id/string()" }
    

    }
    ListView {
    width: 180; height: 300
    model: model

    delegate: Text { text:Chk.title+ ": " + pubDate +id
    }
    

    }

    }@

    and check.js is as follows

    @.pragma library
    var title = model.get(0).title;@

    but here it says thsi cannot locate the model:(

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Julie1986
      wrote on last edited by
      #2

      @<?xml version="1.0" encoding="utf-8"?>
      <rss version="2.0">
      ...
      <channel>
      <item>
      <title>A blog post</title>
      <pubDate>Sat, 07 Sep 2010 10:00:01 GMT</pubDate>
      <id> 0 </id>
      </item>
      <item>
      <title>A blog post</title>
      <pubDate>Sat, 07 Sep 2010 10:00:01 GMT</pubDate>
      <id> 1 </id>
      </item>
      <item>
      <title>A blog post</title>
      <pubDate>Sat, 07 Sep 2010 10:00:01 GMT</pubDate>
      <id> 2 </id> </item>
      <item>
      <title>A blog post</title>
      <pubDate>Sat, 07 Sep 2010 10:00:01 GMT</pubDate>
      <id> 1 </id>
      </item>
      <item>
      <title>Another blog post</title>
      <pubDate>Sat, 07 Sep 2010 15:35:01 GMT</pubDate>
      </item>
      </channel>
      </rss>@

      Xml file. its an example file

      1 Reply Last reply
      0
      • U Offline
        U Offline
        unai_i
        wrote on last edited by
        #3

        Hello,
        I don't really understand what you are trying to achieve. From your code It seems you want all your list items to have the same title. Is it really what you want? If you just want the title corresponding to each item on your XML you can change Chk.title with title in your QML and you don't need your js file.
        Good luck!

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Julie1986
          wrote on last edited by
          #4

          But I want to use js in my file. This will clear my basic concept so that I can further build complex ones.
          I know that chk.title to title ll solve my problem but I need to use js. thanks

          1 Reply Last reply
          0
          • U Offline
            U Offline
            unai_i
            wrote on last edited by
            #5

            So in your javascript try:
            @
            .pragma library

            function getTitle(model, index){
            return model.get(index).title;
            }
            @

            And in your qml Chk.getTitle(model, index);

            Note that I'm not sure you can directly use model in your JavaScript I remember reading some sync stuff but I don't remember exactly what it was (maybe to do with WorkerScript element).

            1 Reply Last reply
            0
            • U Offline
              U Offline
              unai_i
              wrote on last edited by
              #6

              Just for later, sync must be applied when changing models from WorkerScripts.
              See:
              "ListModel documentation":http://qt-project.org/doc/qt-4.8/qml-listmodel.html#using-threaded-list-models-with-workerscript

              1 Reply Last reply
              0
              • J Offline
                J Offline
                Julie1986
                wrote on last edited by
                #7

                Nope the above js does not work. TypeError: Result of expression 'model.get' [undefined] is not a function.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  Julie1986
                  wrote on last edited by
                  #8

                  I tried this in the same .qml file and I got this error

                  @Component.onCompleted: {

                  console.log(model.get(1).title);
                  

                  }@

                  TypeError: Result of expression 'model.get(1)' [undefined] is not an object.
                  Qml debugging is enabled. Only use this in a safe environment!

                  1 Reply Last reply
                  0
                  • U Offline
                    U Offline
                    unai_i
                    wrote on last edited by
                    #9

                    Are you sure model has items when the function is called? (i.e: is the function called after setting model data?)

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      Julie1986
                      wrote on last edited by
                      #10

                      yes.. the above code is the one am currently using. If you have some time kindly check it and let me know. thanks in advance

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        Julie1986
                        wrote on last edited by
                        #11

                        @Rectangle{

                        width:400
                        height:width
                        property int index:0

                        ListView {
                        width: 180; height: 300
                        model: model
                        delegate: Text { text:title+ ": " + pubDate+id
                        }
                        XmlListModel {
                        id: model
                        source: "example.xml"
                        query: "/rss/channel/item"

                        XmlRole { name: "title"; query: "title/string()" }
                        XmlRole { name: "pubDate"; query: "pubDate/string()" }
                        XmlRole { name: "id"; query: "id/string()" }}
                        

                        }

                        Component.onCompleted:console.log(model.get(0).title);

                        }@

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          Julie1986
                          wrote on last edited by
                          #12

                          I get_TypeError: Result of expression ‘model.get(1)’ [undefined] is not an object.
                          Qml debugging is enabled. Only use this in a safe environment!

                          1 Reply Last reply
                          0
                          • J Offline
                            J Offline
                            Julie1986
                            wrote on last edited by
                            #13

                            The javascrpit does not recognize the qml file. any idea on how to link js and qml

                            1 Reply Last reply
                            0
                            • J Offline
                              J Offline
                              Julie1986
                              wrote on last edited by
                              #14

                              [Solved] I passed the model as a js function. if there is any other alternative kindly let me know.

                              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