Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt create Link between folders

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

like image 454
Richard Avatar asked Nov 28 '25 18:11

Richard


1 Answers

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
like image 184
lpapp Avatar answered Nov 30 '25 10:11

lpapp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!