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

Qt creating file in wrong directory



  • I am creating a Javascript file and thus right click the folder I want it under, and go through the Add New.. process. In Qt Creator, it shows it in the appropriate location. However, when I try to import this JS file to my main.qml, it marks it as "File or directory not found". I am importing it exactly the same way as all the other JS files.

    Interestingly, when I look through the project structure in Windows Explorer, the file I just created is under the root project folder, not the js folder (where all of the other JS files are) as it is shown in Qt Creator. I don't recall having this much trouble creating past JS files and importing them.. what is the issue here? I have tried cleaning/building/closing and reopening dozens of times already.


  • Moderators

    @Ashtanga said in Qt creating file in wrong directory:

    I am importing it exactly the same way as all the other JS files.

    show the import statement,
    your qrc file or your folder structure in case you do not use qrc.



  • Try to import only the dir path of the js file, not of the file itself.
    I mean, if the path of the qml file is /home/name/someproject/hello.qml and the path of the js file is /home/name/someproject/myjsfiles/hello.js try to import with;

    import "./myjsfiles/"
    

  • Moderators

    @closx
    https://doc.qt.io/qt-5/qtqml-javascript-imports.html
    JS files need to be imported "directly", not just the folder they are contained in



  • @raven-worx Here is a snippet of how the file structure is in Windows Explorer:

    MyProject
    |
    +-- js
    |   |
    |   +-- oldJsFile1
    |   +-- oldJsFile2
    +-- newJsFile
    

    BUT it is displayed in Qt (and this is how I want it to be) as:

    MyProject
    |
    +-- js
    |   |
    |   + --oldJsFile1
    |   +-- oldJsFile2
    |   +-- newJsFile
    

    The import statement is as follows, which Qt underlines red as "File or directory not found" for only the new file. BUT references such as newJsFile.myFunction() do work...

    import "../js/oldJsFile1.js" as oldJsFile1
    import "../js/oldJsFile2.js" as oldJsFile2
    import "../js/newJsFile.js" as newJsFile // File or directory not found.
    

    For the qrc, I noticed there are 2 for js. The one under the root project folder has my new file included, the one under the js folder does not (they both have all old js files). Could this all have something to do with recently updating to a newer version of Qt/Creator (5.12.2/4.9.0)?


Log in to reply