要实现将 fileToSave 的路径变成 './scenes/json/文件名.json',可以使用 QString 的 replace() 函数来实现替换路径部分。以下是一种可能的实现方式:

QString fileToSave = QFileDialog::getSaveFileName(
		this,
		tr('保存当前场景'),
		'./scenes/',
		tr('inventor(*.iv)'));

// 替换路径部分
QString newPath = fileToSave;
newPath.replace('/scenes/', '/scenes/json/');

// 替换文件名后缀
QString newFileName = QFileInfo(newPath).baseName();
newFileName += '.json';
newPath.replace(QFileInfo(newPath).fileName(), newFileName);

// 更新 fileToSave
fileToSave = newPath;

这段代码首先使用 replace() 函数将路径中的 '/scenes/' 替换为 '/scenes/json/',然后使用 QFileInfo 类获取文件名并替换文件名后缀为 '.json'。最后将更新后的路径赋值给 fileToSave。

Qt C++:将 QString 路径转换为 ./scenes/json/文件名.json

原文地址: https://www.cveoy.top/t/topic/qAli 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录