没找着生成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; ofs.close(); } int main(int argc, char *argv[]) { if (argc < 2) { cout << "Missing target folder parameters." << endl; return 0; } string path = argv[1]; // // 循环遍历路径 vector" << 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 << " " << endl; ofs << "" + temp + " " << endl; } ofs << "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();



