栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

clion生成qt的qrc文件

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

clion生成qt的qrc文件

没找着生成qrc文件的工具,就搓一个.

随便建一个c++工程,复制以下代码到main.cpp:

#include
#include
#include
#include 
#include 

using namespace std;

void getFiles(const string &path, vector &files) {
  //文件句柄
  intptr_t hFile = 0;
  //文件信息
  struct _finddata_t fileInfo{};
  string p;
  if ((hFile = _findfirst(p.assign(path).append("\*").c_str(), &fileInfo)) != -1) {
    do {
      //如果是目录就迭代
      if ((fileInfo.attrib & _A_SUBDIR)) {
        if (strcmp(fileInfo.name, ".") != 0 && strcmp(fileInfo.name, "..") != 0)
          getFiles(p.assign(path).append("\").append(fileInfo.name), files);
      } else {
        files.push_back(p.assign(path).append("\").append(fileInfo.name));
      }
    } while (_findnext(hFile, &fileInfo) == 0);
    _findclose(hFile);
  }
}

string &replace_all(string &src, const string &old_value, const string &new_value) {
  for (string::size_type pos(0); pos != string::npos; pos += new_value.length()) {
    if ((pos = src.find(old_value, pos)) != string::npos) {
      src.replace(pos, old_value.length(), new_value);
    } else break;
  }
  return src;
}

void save_qrc(string &path, vector &filenames) {
  string targetFileName = path.substr(path.rfind("\", path.length()) + 1, path.length());
  ofstream ofs;
  ofs.open(path + "\" + targetFileName+".qrc", ios::out | ios::trunc);
  if (!ofs.is_open()) {
    cerr << "open dir error" << endl;
    return;
  }
  ofs << "" << endl;
  ofs << "    " << endl;

  int subPathSize = path.size() + 1;
  for (auto fileName: filenames) {

    string temp = replace_all(fileName.replace(0, subPathSize, ""), "\", "/");
    if (temp.length() > 4 && temp.substr(temp.length() - 4, 4) == ".qrc") {
      continue;
    }
    ofs << "        " + temp + "" << endl;
  }
  ofs << "    " << endl;
  ofs << "" << endl;
  ofs.close();
}

int main(int argc, char *argv[]) {
  if (argc < 2) {
    cout << "Missing target folder parameters." << endl;
    return 0;
  }
  string path = argv[1];
//  // 循环遍历路径
  vector filenames;
  getFiles(path, filenames);
  save_qrc(path, filenames);
  return 0;
}

构建后会生成.exe文件,重命名为qrc.exe,复制到存放的目录下,我的存放目录是:

D:installinstall_cqrc.exe

添加外部工具:

 测试,在video文件夹上右键:

 生成后的文件:

会在当前目录下创建[文件夹名称].qrc文件,该文件前缀prefix也是这个文件夹名称.

会忽略.qrc结尾的文件.

项目中使用

res上右键生成,在CMakeLists.txt中添加该文件到add_executable中:

 代码中使用:

  QWidget app;
  app.setWindowIcon(QIcon(":/res/images/icon.png"));
  app.show();

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/993825.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号