c++qtterminaldeprecatedqprocess

`QProcess::startDetached` deprecated: any alternatives?


im trying to launch the cmd on windows. i used the following code to launch the terminal, and it worked 100% fine.

but the Qt Creator keep saying that QProcess::startDetached(const QString &) is deprecated, i just dont know why.

ive looked at these (Alternatives to QProcess::startDetached() and QProcess start() as they are deprecated) alternatives but none of it worked as my command require special string handling.

whats worse in qt 6 the deprecated methods are even removed. now i have no way to access this method.

my code (as spoke it works fine but it's deprecated):

void MainWindow::on_actionShowFolderInTerminal(bool isChecked) {
    Q_UNUSED(isChecked)
#if defined(Q_OS_WIN) || defined(Q_OS_WIN32) || defined(Q_OS_WINDOWS)
    QProcess::startDetached("cmd /C start \"\" cmd /K cd /d \"" + (m_editor->absPath()) + "\"");
    return;
#endif
    CriticalMessage::exec("Currently this function is supported in Windows only. We'll add support to macOS and Linux as soon as possible");
}

any help will be appreciated!


Solution

  • A part of the overloaded QProcess::startDetached, QProcess::startDetached(const QString &) is deprecated in Qt 5.15 and removed in Qt 6 (based on 6.7.1, as far as I know). It is because it's quite unsafe to pass non-segmented command-arguments mixture to the process rather than sliced and arrayed arguments, as the slicing and separating work for the arguments is ambiguous.

    Back to the topic, @pptaszni suggested a good way to tackle your problem:

    void MainWindow::on_actionShowFolderInTerminal(bool isChecked) {
        Q_UNUSED(isChecked)
    #if defined(Q_OS_WIN) || defined(Q_OS_WIN32) || defined(Q_OS_WINDOWS)
        QProcess::startDetached("cmd /C start \"\" cmd /K cd /d \"" + (m_editor->absPath()) + "\"", {});
        return;
    #endif
        CriticalMessage::exec("Currently this function is supported in Windows only. We'll add support to macOS and Linux as soon as possible");
    }
    

    Except that if only the original and deprecated QProcess::startDetached(const QString & overloaded method works, but not neither the above answer nor in the Qt official documentation, you can still try:

    void MainWindow::on_actionShowFolderInTerminal(bool isChecked) {
        Q_UNUSED(isChecked)
    #if defined(Q_OS_WIN) || defined(Q_OS_WIN32) || defined(Q_OS_WINDOWS)
        command = "cmd /C start \"\" cmd /K cd /d \"" + (m_editor->absPath()) + "\"";
        QProcess process;
        QStringList arguments = QProcess::splitCommand(command);
        if (arguments.isEmpty())
            return;
        process.setProgram(arguments.takeFirst());
        process.setArgument(arguments);
        process.startDetached();
        return;
    #endif
        CriticalMessage::exec("Currently this function is supported in Windows only. We'll add support to macOS and Linux as soon as possible");
    }
    

    The second answer is based on Qt source code. Hope them help.