KIO::CommandLauncherJob Class

CommandLauncherJob runs a command and watches it while running. More...

Header: #include <KIO/CommandLauncherJob>
CMake: find_package(KF6 REQUIRED COMPONENTS KIO)
target_link_libraries(mytarget PRIVATE KIOGui)
Since: KIO 5.69
Inherits: KJob

Public Functions

CommandLauncherJob(const QString &command, QObject *parent = nullptr)
CommandLauncherJob(const QString &executable, const QStringList &args, QObject *parent = nullptr)
virtual ~CommandLauncherJob() override
(since KIO 5.83) QString command() const
qint64 pid() const
(since KIO 5.83) void setCommand(const QString &command)
void setDesktopName(const QString &desktopName)
void setExecutable(const QString &executable)
(since KIO 5.82) void setProcessEnvironment(const QProcessEnvironment &environment)
void setStartupId(const QByteArray &startupId)
void setWorkingDirectory(const QString &workingDirectory)
(since KIO 5.83) QString workingDirectory() const

Reimplemented Public Functions

virtual void start() override

Detailed Description

It creates a startup notification and finishes it on success or on error (for the taskbar). It also emits a "program not found" error message if the requested command did not exist.

The job finishes when the command is successfully started; at that point you can query the PID with pid(). Note that no other errors are handled automatically after the command starts running. As far as CommandLauncherJob is concerned, if the command was launched, the result is a success. If you need to query the command for its exit status or error text later, it is recommended to use QProcess instead.

For error handling, either connect to the result() signal, or for a simple messagebox on error, you can do

job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, this));

Member Function Documentation

[explicit] CommandLauncherJob::CommandLauncherJob(const QString &command, QObject *parent = nullptr)

Creates a CommandLauncherJob.

command the shell command to run

The command is given "as is" to the shell, it must already be quoted if necessary. If command is instead a filename, consider using the other constructor, even if no args are present.

parent the parent QObject

Please consider also calling setDesktopName() for better startup notification.

[explicit] CommandLauncherJob::CommandLauncherJob(const QString &executable, const QStringList &args, QObject *parent = nullptr)

Creates a CommandLauncherJob.

executable the name of the executable

args the commandline arguments to pass to the executable

parent the parent QObject

Please consider also calling setDesktopName() for better startup notification.

[override virtual noexcept] CommandLauncherJob::~CommandLauncherJob()

Destructor

Note that jobs auto-delete themselves after emitting result

[since KIO 5.83] QString CommandLauncherJob::command() const

Returns the command executed by this job.

This function was introduced in KIO 5.83.

See also setCommand().

qint64 CommandLauncherJob::pid() const

Returns the PID of the command that was started

Available after the job emits result().

[since KIO 5.83] void CommandLauncherJob::setCommand(const QString &command)

Sets the command to execute, this will change the command that was set by any of the constructors.

This function was introduced in KIO 5.83.

See also command().

void CommandLauncherJob::setDesktopName(const QString &desktopName)

Set the name of the desktop file (e.g. "org.kde.dolphin", without the ".desktop" filename extension).

This is necessary for startup notification to work.

void CommandLauncherJob::setExecutable(const QString &executable)

Sets the name of the executable, used in the startup notification (see KStartupInfoData::setBin()).

executable executable name, with or without a path

Alternatively, use setDesktopName().

[since KIO 5.82] void CommandLauncherJob::setProcessEnvironment(const QProcessEnvironment &environment)

Can be used to pass environment variables to the child process.

environment set of environment variables to pass to the child process

This function was introduced in KIO 5.82.

See also QProcessEnvironment.

void CommandLauncherJob::setStartupId(const QByteArray &startupId)

Sets the platform-specific startup id of the command launch.

startupId startup id, if any (otherwise "").

For X11, this would be the id for the Startup Notification protocol. For Wayland, this would be the token for the XDG Activation protocol.

void CommandLauncherJob::setWorkingDirectory(const QString &workingDirectory)

Sets the working directory from which to run the command.

workingDirectory path of a local directory

See also workingDirectory().

[override virtual] void CommandLauncherJob::start()

Reimplements: KJob::start().

Starts the job. You must call this, after having called all the necessary setters.

[since KIO 5.83] QString CommandLauncherJob::workingDirectory() const

Returns the working directory, which was previously set with @c setWorkingDirectory().

This function was introduced in KIO 5.83.

See also setWorkingDirectory().