c++ - Qt 创建文件夹之间的链接

标签 c++ qt qprocess qtcore qfile

我必须构建一个小对话框来创建指向文件夹的符号链接(symbolic link)。

在 Windows 中我会使用 mklink/D 命令。

有没有可能在 Qt 中创建这样的链接?我只看到 QFile 在文件之间创建链接并且它们需要以 .lnk ( http://qt-project.org/doc/qt-4.8/qfile.html#link ) 结尾 另一方面,QDir 不提供任何内容。

有什么建议吗?

最好的问候, 理查德

最佳答案

Is there a possibility to create such links in Qt?

是的,它是,但仅限于 Unix。

不幸的是,Windows 上的 QFile 不支持它,甚至 QDir 也不支持。在我看来,这对于提交有关 Qt Bug tracker 的报告将是一个有用的功能。 .

解决方法是这样写:

#ifdef Q_OS_UNIX
    QFile::link(sourceDir.absolutePath(), destDir.absolutePath());
#elif Q_OS_WIN
    QProcess process;
    process.start("mklink /D");

    // Wait for it to start
    if(!process.waitForStarted())
        return 0;

    bool retval = false;
    QByteArray buffer;
    while ((retval = process.waitForFinished()));
        buffer.append(process.readAll());

    if (!retval) {
        qDebug() << "Process error:" << process.errorString();
        qDebug() << "Output:" << buffer;
        return 1;
    }
#endif

关于c++ - Qt 创建文件夹之间的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21011063/

相关文章:

c++ - 带空小部件的 Qt 拆分器

c++ - 极其简单的 POSIX C++ IPC

c++ - 类的属性可以是数组吗?

c++ - 如何打印 Qt 对话框或窗口?

c++ - 在 Qt 中旋转拨盘后的增量计数?

qt - 如何在 Qt 程序中嵌入二进制可执行文件(将在运行时执行)?

python - 与 QProcess Python 程序通信

c++ - 如何修复 Windows 上 Sublime Text 3 中的 "Permission denied collect2.exe: error: ld returned 1 exit status"

c++ - Qt:12.625 舍入 2 返回 12.62

python - 使用 QProcess 通过 PyQt 运行和监控系统进程