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. How to extend the JavaScript environment in QML, reading a text file from js?
Forum Updated to NodeBB v4.3 + New Features

How to extend the JavaScript environment in QML, reading a text file from js?

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 3 Posters 11.3k 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.
  • K Offline
    K Offline
    komar_rus
    wrote on last edited by
    #1

    I need to read a text file from QML or js in QML. Can I use QScriptExtensionPlugin to write a plug-in extending QML JavaScript environment?

    Thanks

    1 Reply Last reply
    0
    • B Offline
      B Offline
      baysmith
      wrote on last edited by
      #2

      No. You have to use the "QDeclarativeExtensionPlugin":http://doc.qt.nokia.com/latest/qdeclarativeengine.html. There is a "tutorial":http://doc.qt.nokia.com/latest/declarative-tutorials-extending-chapter6-plugins.html for doing so.

      Nokia Certified Qt Specialist.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        komar_rus
        wrote on last edited by
        #3

        QDeclarativeExtensionPlugin extends QML environment only. I want to extend js in QML as well, something like "QML Global Object" - XMLHttpRequest. I found implementation of XMLHttpRequest in the QtDeclarative library source. I could go finish my own object allows to read a text file. However this would not be the right solution. We need to do it by utilizing plugins mechanism.
        Can I somehow extend js environment with QScriptExtensionPlugin or in some other way without changing QtDeclarative library?

        I have the following QML:
        import QtQuick 1.0
        import "TextFile.js" as TextFile

        Item {
        width: 300
        height: 200

        Text {
        id: idText1
        x: 28
        y: 48
        color: "red"
        text: ""
        font.pixelSize: 50
        }

        Timer {
        interval: 3000; running: true; repeat: true;
        onTriggered: {idText1.text = TextFile.readLine()}
        }

        Component.onCompleted: Test.createFile()
        }

        //TextFile.js
        var file = null;

        function createFile()
        {
        file = new StreamReader();
        file.open("c:\text.txt");
        }

        function readLine()
        {
        var str = file.readLine();
        return str;
        }

        1 Reply Last reply
        0
        • ? This user is from outside of this forum
          ? This user is from outside of this forum
          Guest
          wrote on last edited by
          #4

          komar, can u pls enclose the code blocks within @ tags ? easier to read for all of us

          1 Reply Last reply
          0
          • K Offline
            K Offline
            komar_rus
            wrote on last edited by
            #5

            QDeclarativeExtensionPlugin extends QML environment only. I want to extend js in QML as well, something like “QML Global Object” – XMLHttpRequest. I found implementation of XMLHttpRequest in the QtDeclarative library source. I could go finish my own object allows to read a text file. However this would not be the right solution. We need to do it by utilizing plugins mechanism.
            Can I somehow extend js environment with QScriptExtensionPlugin or in some other way without changing QtDeclarative library?

            I have the following QML:

            @
            import QtQuick 1.0
            import “TextFile.js” as TextFile

            Item {
            width: 300
            height: 200

            Text {
                    id: idText1 
                    x: 28 
                    y: 48 
                    color: “red” 
                    text: “” 
                    font.pixelSize: 50
                 } 
            
            Timer { 
                    interval: 3000
                    running: true 
                    repeat: true
                    onTriggered: {idText1.text = TextFile.readLine()} 
                  } 
            
            Component.onCompleted: TextFile.createFile()
            

            }
            @

            //TextFile.js

            @
            var file = null;
            function createFile()
            {
            file = new StreamReader();
            file.open(“c:\text.txt”);
            }

            function readLine()
            {
            var str = file.readLine();
            return str;
            }
            @

            1 Reply Last reply
            0
            • ? This user is from outside of this forum
              ? This user is from outside of this forum
              Guest
              wrote on last edited by
              #6

              komar thanks for the edited post ...

              IMO, QML is best used for the UI of an application. Now using the view to do file reads might not be the best approach. Using javascript also might not be good for performance.
              Why don't you read the file from Qt/C++, and let your QML pull this info ?

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mbrasser
                wrote on last edited by
                #7

                Adding a "namespace" (with functions, etc) in a plugin isn't currently supported, but its something we'd like to support in a future version (I didn't have any luck finding the task in the tracker, but from memory there is one). The same is true for QScriptEngine access in general (see http://bugreports.qt.nokia.com/browse/QTBUG-11942).

                You might still be able to use QDeclarativeExtensionPlugin for what you are after, by adding an object to the root context in "initializeEngine":http://doc.qt.nokia.com/4.7-snapshot/qdeclarativeextensionplugin.html#initializeEngine, though this isn't recommended for library-style plugins.

                Regards,
                Michael

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  komar_rus
                  wrote on last edited by
                  #8

                  Thanks for all your answers. I will try to go with QDeclarativeExtensionPlugin and initializeEngine, and I am looking forward for new version of QT, guys.

                  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