c++ - 获取 2 个 QDir 的共同父级

标签 c++ qt

有没有办法获得 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/

相关文章:

c++ - Qt、C++、3D 矩阵立方体

c++ - 无法关闭 QThread - 应用程序崩溃

c++ - 通过调用C++函数设置loader组件

c++ - Qt - 显示选择顶部的结果

c++ - 构建特定的 .cpps

c++ - 随机函数可以返回的范围是多少?

Qt QCompleter 多重匹配

c++ - 模板参数推导与 QT lambda 不匹配

c++ - 模拟对象 C++

c++ - 为什么相同大小的函数参数中的隐式转换不会引发警告?