Qt创建文件夹之间的链接 [英] Qt create Link between folders

查看:1049
本文介绍了Qt创建文件夹之间的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须建立一个小对话框,创建一个文件夹的符号链接。

I have to build a small dialog that creates a symbolic link to a folder.

在windows中,我将使用mklink / D命令。

In windows I would use mklink /D command.

有可能在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? I have only seen QFile creating links between files and that they need to end with .lnk (http://qt-project.org/doc/qt-4.8/qfile.html#link) QDir on the other hand does not provide anything.

有任何建议吗?

最好的问候,
Richard

Best regards, Richard

推荐答案


是否可以在Qt中创建此类链接?

Is there a possibility to create such links in Qt?

,但只有在Unix上。

Yes, it is, but only on Unix.

不幸的是,这不是QFile在Windows上,甚至QDir不支持。在我看来,这是一个非常有用的功能,可以在 Qt Bug tracker

Unfortunately, this is not supported by QFile on Windows, not even by QDir. In my opinion, this would be a useful feature to submit a report for on the Qt Bug tracker.

解决方法是编写以下内容:

The workaround would be to write something like this:

#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

这篇关于Qt创建文件夹之间的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆