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



  • 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



  • 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}



  • @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?



  • 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!



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



  • @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



  • 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



  • 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


Log in to reply
 

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