在每次构建时强制对 qrc 文件进行 rcc-ing [英] Force rcc-ing of qrc file on each build

查看:14
本文介绍了在每次构建时强制对 qrc 文件进行 rcc-ing的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Visual Studio 2015 中的每个构建中强制对 qrc 文件进行 rcc-ing?我们将资源嵌入到二进制文件中,因此如果诸如 qml 或图像资产之类的内容发生变化,我们需要运行 rcc 以获取当前状态的新 .cpp 文件.我看到了几个选项 - 在预构建事件中粗暴地触摸 .qrc 文件,运行一个脚本,在构建之前检查资产文件夹中的所有内容,并检查时间戳并将它们与之前构建的状态进行比较.是否有更干净、更优雅的选择?

How to force rcc-ing of a qrc file on each build in Visual Studio 2015? We are embedding the resources in the binary, so if something like qml or image assets change, we need to run rcc to get a fresh .cpp file for the current state. I see several options - brutally touching the .qrc file in a pre build event, running a script which checks everything in the asset folder before build and checking the timestamps and comparing them to a state at the previous build. Are there cleaner and more elegant options?

推荐答案

既然是在评论中要求的,我就发布我想出的解决方案.所以,在我的例子中,qrc 文件被添加到 Visual Studio 解决方案中,并且有一个适当的自定义构建工具集(但这是常见的方式,应该由 VS Qt 插件设置),如这个:

Since it was requested in the comments, I'm publishing the solution I came up with. So, in my case the qrc file is added to the Visual Studio solution, and has a proper Custom Build Tool set (but that is the common way and should be set up by the VS Qt plugin) like this:

我所要做的就是编写一个简单的 C# 程序,它读取 qrc 的内容并更新 qrc 文件的修改时间戳,如果有的话包含的项目比 qrc 本身更新.这就是全部:

All I had to do was to make a trivial C# program which reads the contents of the qrc and updates the modification timestamp of the qrc file, if any of the contained items was newer than the qrc itself. This is all it takes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.IO;

namespace QrcValidator
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                System.Console.WriteLine("usage: QrcValidator.exe <qrc file path>");
                return;
            }
            Console.WriteLine(string.Format("Validating {0}", args[0]));
            XDocument qrcDocument = XDocument.Load(args[0]);
            var qrcFileLastWrtieTimeUtc = File.GetLastWriteTimeUtc(args[0]);
            foreach (var file in qrcDocument.Descendants("RCC").Descendants("qresource").Descendants("file"))
            {
                if (File.GetLastWriteTimeUtc(file.Value) >= qrcFileLastWrtieTimeUtc)
                {
                    Console.WriteLine("{0} is dirty, setting last write time of {1} to current UTC time", file.Value, args[0]);
                    File.SetLastWriteTimeUtc(args[0], DateTime.UtcNow);
                    Environment.Exit(0);
                }
            }
        }
    }
}

此工具将 qrc 文件作为第一个参数.所以我只是添加了调用这个工具作为一个预构建事件,如果资源发生了任何变化,qrc 文件被触及,并且它是正常的构建命令被调用.

This tool takes the qrc file as the first argument. So I just added calling this tool as a pre build event, and if any changes have happened to the resources, the qrc file is touched, and it's normal build command is invoked.

这篇关于在每次构建时强制对 qrc 文件进行 rcc-ing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆