Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

In windows I would use mklink /D command.

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.

Any suggestions?

Best regards, Richard

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
887 views
Welcome To Ask or Share your Answers For Others

1 Answer

Is there a possibility to create such links in Qt?

Yes, it is, but only on Unix.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...