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 we can import a file qml in other file qml?
Forum Updated to NodeBB v4.3 + New Features

how we can import a file qml in other file qml?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 5 Posters 4.4k Views 2 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.
  • stackprogramerS Offline
    stackprogramerS Offline
    stackprogramer
    wrote on last edited by stackprogramer
    #1

    i import a file.js (java script)in my qml according below;

    import "file.js" as  JSGen
    
    

    for js file it wroks correctlly
    when i want to add a file.qml in the same directory file js according below;

    import "qrc:///SpiralCoreFunctions.qml"
    
    

    or

    import "SpiralCoreFunctions.qml"
    
    

    it retrun errors in below:

    qrc:///main.qml:11:1: "SpiralCoreFunctions.qml": no such directory
    

    how can i solve my problem?thanks for reply

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sruthi
      wrote on last edited by
      #2

      Hi you need not import it if it is in the same directory.
      just instantiate by its filename

      example
      import QtQuick 2.0
      Clock.qml
      Item {
      id : clock
      property alias city: cityLabel.text
      property real shift
      width: {
      if (ListView.view && ListView.view.width >= 200)
      return ListView.view.width / Math.floor(ListView.view.width / 200.0);
      else
      return 200;
      }
      }
      you can instantiatein main.qml
      by Clock { city: "Bangalore"; shift: 5}

      1 Reply Last reply
      0
      • L Offline
        L Offline
        literA2
        wrote on last edited by
        #3

        @Sruthi is correct, you don't need to import the file if they're on the same directory. You just have to call its filename.

        May we know what are you trying to do?

        1 Reply Last reply
        0
        • stackprogramerS Offline
          stackprogramerS Offline
          stackprogramer
          wrote on last edited by
          #4

          i want to define some function in file.qml and i use method of these in new file qml ,main.qml

          i test in Qt 5.2 and i write the name file qml in same directory,but i doesn't work.!!!!your idea don't work!

          1 Reply Last reply
          0
          • speqtrS Offline
            speqtrS Offline
            speqtr
            wrote on last edited by speqtr
            #5

            Hi,

            you need to add qmldir file in your folder:

            SpiralCoreFunctions 1.0 ./SpiralCoreFunctions.qml
            

            Your folder structure will look like this:

            main.qml
            SpiralCoreFunctions.qml
            qmldir
            

            In this case you will be able to create SpiralCoreFunctions object in your main.qml.

            Another option is:

            1. Create folder components
            2. Put SpiralCoreFunctions.qml and qmldir files in this directory.
            3. In main.qml add import "./components"

            Your folder structur will look like this:

            components
            |-SpiralCoreFunctions.qml
            └-qmldir
            main.qml
            

            Anyway, don't forget to add all this files to your .qrc file.

            stackprogramerS 1 Reply Last reply
            0
            • speqtrS speqtr

              Hi,

              you need to add qmldir file in your folder:

              SpiralCoreFunctions 1.0 ./SpiralCoreFunctions.qml
              

              Your folder structure will look like this:

              main.qml
              SpiralCoreFunctions.qml
              qmldir
              

              In this case you will be able to create SpiralCoreFunctions object in your main.qml.

              Another option is:

              1. Create folder components
              2. Put SpiralCoreFunctions.qml and qmldir files in this directory.
              3. In main.qml add import "./components"

              Your folder structur will look like this:

              components
              |-SpiralCoreFunctions.qml
              └-qmldir
              main.qml
              

              Anyway, don't forget to add all this files to your .qrc file.

              stackprogramerS Offline
              stackprogramerS Offline
              stackprogramer
              wrote on last edited by
              #6

              @speqtr said:

              Hi,

              you need to add qmldir file in your folder:

              SpiralCoreFunctions 1.0 ./SpiralCoreFunctions.qml
              

              Your folder structure will look like this:

              main.qml
              SpiralCoreFunctions.qml
              qmldir
              

              In this case you will be able to create SpiralCoreFunctions object in your main.qml.

              Another option is:

              1. Create folder components
              2. Put SpiralCoreFunctions.qml and qmldir files in this directory.
              3. In main.qml add import "./components"

              Your folder structur will look like this:

              components
              |-SpiralCoreFunctions.qml
              └-qmldir
              main.qml
              

              Anyway, don't forget to add all this files to your .qrc file.

              your idea don't work,same before errors

              1 Reply Last reply
              0
              • jeremy_kJ Offline
                jeremy_kJ Offline
                jeremy_k
                wrote on last edited by
                #7

                Import statements for qml files omit the name of the file itself. Either import a directory or a namespace.

                http://doc.qt.io/qt-5/qtqml-syntax-directoryimports.html

                http://doc.qt.io/qt-5/qtqml-syntax-imports.html

                Asking a question about code? http://eel.is/iso-c++/testcase/

                1 Reply Last reply
                0
                • stackprogramerS Offline
                  stackprogramerS Offline
                  stackprogramer
                  wrote on last edited by stackprogramer
                  #8

                  i want to use qml as a class in my app,
                  my result is that :we should answer this if we cannot use qml for define new class,with method and property??????
                  we when use js file we can create new class with method and property but with qml i could n't create a class and property and method. so my question is reformed in new post.goodluck

                  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