Skip to content
Advertisement

Qt5 Change Softlink of Currently Running Executable

I created 2 simple Qt5.5 test apps on Linux, “red” and “green”. I setup an initial soft link in the current directory that looks like this:

gui -> red

I want to be able to launch the gui app at any time and have it switch the softlink to the other ‘color’ app. So when I run gui and it points to red, the next time I run gui it will point to green.

The red app code to do this looks like this:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    // Get the current path
    QString cwd = QDir::currentPath();
    qDebug() << cwd + "/green";
    bool success = QFile::link(cwd + "/green",cwd + "/gui");
    qDebug() << "Was successful: " << success;
}

The green app code to do this looks like this:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    // Get the current path
    QString cwd = QDir::currentPath();
    qDebug() << cwd + "/red";
    bool success = QFile::link(cwd + "/red",cwd + "/gui");
    qDebug() << "Was successful: " << success;
}

The problem is that the “success” boolean always fails. I guess I cannot change the soft link of a currently running process. How can I achieve this?

Advertisement

Answer

Try to remove link before recreating it:

This function will not overwrite an already existing entity in the file system; in this case, link() will return false and set error() to return RenameError.

QFile::link().

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement