Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Multiple directories in QML project



  • Hello qt devs.I am kinda new to this, but I really like QML so I decided to use Qt and QML for my next project.
    I plan on making a complex and relatively big application using QML and C++ (if neccessary)
    I plan it to run on Linux, Mac and Windows and also eventually Android (when QT neccessitas gets more stable).
    There will be a directory called widgets where I will have my widgets .qml file (more files , Button.qml,TextInput.qml...)
    There will be a directory called dialogs,src...
    Now onto my problem. I have not really done much stuff really, this post is just a way to stop bashing qml and qt later when things go wrong and there is a lot of code :D
    Here is the info I need :
    in a .qml source file
    import "src/code.js" as SomeNamespace
    How will that behave on windows where paths are escaped using \ ?
    Then if I have widgets/Button.qml
    how can I import that into my dialogs/SomeDialog.qml
    Will import "widgets/Button.qml" as Button work
    and will it work on windows.
    BTW - How to add new directories in Qt Creator?
    That's all that comes to my mind now.Thanks for helping.



  • Hi,

    Regarding relative imports: we transform paths into platform-canonical form, so doing:
    import "src/code.js" as JS
    should just work on all platforms.

    If you have project/widgets/Button.qml and project/dialogs/SomeDialog.qml you should be able to do:
    import "../widgets"
    inside SomeDialog.qml to gain access to all of the QML document-defined types in the widgets subdir.

    Regarding directories in QtCreator: it is important to note that QtCreator doesn't "see" directories. What it sees are Qt projects and subprojects (which are defined in .pro or .pri files).

    So, if you want a tree of "subdirectory-like" separations in QtCreator, you should use qmake .pro files (or .qmlproject files for pure-qml projects).

    example:

    project/myapplication.pro:

    #start
    TEMPLATE=subdirs
    CONFIG=ordered
    SUBDIRS += widgets dialogs
    #end

    project/widgets/widgets.pro:

    #start
    QT += widgets gui quick qml
    HEADERS += someCppWidget.h
    SOURCES += someCppWidget.cpp
    OTHER_FILES += MyButton.qml
    #end

    project/widgets/dialogs.pro

    #start
    QT += widgets gui quick qml
    HEADERS += someCppDialog.h
    SOURCES += someCppDialog.cpp
    OTHER_FILES += MyDialog.qml
    #end

    etc.

    Cheers,
    Chris.



  • Thank you for your post.
    It is very informative.
    I have one more question regarding paths.
    When I use c++ what should I use?
    Does qmake fix paths automatically like QML runtime does?



  • qmake will only parse .pro and .pri files, not cpp files.
    For paths in CPP, you can use QUrl::fromLocalFile("some/path") -- and in that case, yes, it should canonicalise the path on the local filesystem, doing slash replacement etc as required.


Log in to reply