Important: Please read the Qt Code of Conduct -

How to make .qrc file depend on .qml in Visual Studio (how to force compiling of a project when QML code is changed)?

  • I have a C++/QML project under VS2010 with QML embedded into resource file and it works well. I get a stand-alone binary with my QML code, however when I change any QML file, VS doesn't recompile the resources. What I currently do is recompiling .qrc manually after I change .qml files and it is quite annoying.

    I know it is rather pure VS-related question on making a file depend on other files for building, but I think anyone using Qt + QML + VS will experience the same difficulties. So I would like to make a .qrc depend on all .qml and .js files (or maybe if it is easier on any file in a sub-folder), are there any simple solutions for that?

  • ok, I think I come up with an acceptable solution to the problem, learned some PowerShall along the way =)

    The solution is the following:
    1. create a pre-build event that checks whether files in a certain folder are newer than the .qrc and
    2. update the .qrc modification time if such files were found
    3. force VS to execute build commands when QML-related files change

    I'm using git, so modification of only the time stamp doesn't cause any version control issues, but if you want to keep source tree read-only you can in the step 2. update some other file (that is not under the version control) and then make .qrc depend on it.

    Advantages of this methods are: you don't need to manually add dependencies to your .qrc build, and change them if you rename files and you don't need to modify .vcxproj files or change anything in Windows Registry as some other methods do.

    Here is how I do the pre-build event / time modification:

    1. Create a PowerShall file (I made one in a project directory):

    @param ([string]$qrcFile, [string]$qmlDir)

    write-host "Checking for updated QML dependencies..."
    write-host " QML dir: "$qmlDir
    write-host " qrc file:"$qrcFile

    $q=$(ls $qrcFile);

    foreach( $i in get-childitem $qmlDir* )
    if( $i.LastWriteTime -gt $q.LastWriteTime )
    $q.LastWriteTime = Get-Date;
    write-host "newer qml-related file found: "$i

    write-host "none found."
    It takes two parameters: .qrc file and a folder path, then it checks all files in the folder and if it finds one with modification time greater than .qrc it sets the .qrc modification time to the current time, which forces VS to recompile resources.

    2. Call it from VS2010: Project->Properties->Build Events->Pre-Build Event->Command Line insert:
    powershell -file $(ProjectDir)\checkqml.ps1 $(ProjectDir)\src\gui\project.qrc $(ProjectDir)\src\gui\qml

    3. Make VS run build scrips when any of your QML changes: select all your .qml and .js in the Solution Navigator, then select Properties->General->Item Type: Custom Build Tool

    When building a solution you'll see something like:
    Checking for updated QML dependencies...
    QML dir: C:_project\src\gui\qml
    qrc file: C:_project\program.qrc
    newer qml-related file found: C:_project\src\gui\qml\globals.js

    When you add new files to the qml directory you only need to set them to do a custom build (step 3.), and you don't need to do anything if you rename or delete files. The PowerShell script can also be easily adjusted to only take .qml and .js files into account or use different or recursive directories if you have any.

Log in to reply