有没有办法获得 2 个 QDir 对象最接近的公共(public)父对象?例如:
QDir d1("/someroot/parent/test/folder");
QDir d2("/someroot/parent/another/folder");
QDir d3 = commonParent(d1, d2);
// d3 == "/comeroot/parent/"
遗憾的是,Boost.Filesystem 不是一个选项。
最佳答案
const QString commonParent(const QString &path1, const QString &path2)
{
QString ret = path2;
while (!path1.startsWith(ret))
ret.chop(1);
if (ret.isEmpty())
return ret;
while (!ret.endsWith('/'))
ret.chop(1);
return ret;
}
关于c++ - 获取 2 个 QDir 的共同父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15713529/