KPtyProcess Class

This class extends KProcess by support for PTYs (pseudo TTYs). More...

Header: #include <KPtyProcess>
CMake: find_package(KF6 REQUIRED COMPONENTS Pty)
target_link_libraries(mytarget PRIVATE KF6::Pty)
Inherits: KProcess

Public Types

enum PtyChannelFlag { NoChannels, StdinChannel, StdoutChannel, StderrChannel, AllOutputChannels, AllChannels }
flags PtyChannels

Public Functions

KPtyProcess(QObject *parent = nullptr)
KPtyProcess(int ptyMasterFd, QObject *parent = nullptr)
bool isUseUtmp() const
KPtyDevice *pty() const
KPtyProcess::PtyChannels ptyChannels() const
void setPtyChannels(KPtyProcess::PtyChannels channels)
void setUseUtmp(bool value)

Detailed Description

The PTY is opened as soon as the class is instantiated. Verify that it was opened successfully by checking that pty()->masterFd() is not -1.

The PTY is always made the process' controlling TTY. Utmp registration and connecting the stdio handles to the PTY are optional.

No attempt to integrate with QProcess' waitFor*() functions was made, for it is impossible. Note that execute() does not work with the PTY, too. Use the PTY device's waitFor*() functions or use it asynchronously.

Note: If you inherit from this class and use setChildProcessModifier() in the derived class, you must call the childProcessModifier() of KPtyProcess first (using setChildProcessModifier() in the derived class will "overwrite" the childProcessModifier() std::function that was previously set by KPtyProcess).

For example:

class MyProcess : public KPtyProcess
{
    MyProcess()
    {
        auto parentChildProcModifier = KPtyProcess::childProcessModifier();
        setChildProcessModifier([parentChildProcModifier]() {
            // First call the parent class modifier function
            if (parentChildProcModifier) {
                parentChildProcModifier();
            }
            // Then whatever extra code you need to run
            ....
            ....
        });
    }

Member Type Documentation

enum KPtyProcess::PtyChannelFlag
flags KPtyProcess::PtyChannels

ConstantValueDescription
KPtyProcess::NoChannels0The PTY is not connected to any channel
KPtyProcess::StdinChannel1Connect PTY to stdin
KPtyProcess::StdoutChannel2Connect PTY to stdout
KPtyProcess::StderrChannel4Connect PTY to stderr
KPtyProcess::AllOutputChannels6Connect PTY to all output channels
KPtyProcess::AllChannels7Connect PTY to all channels

The PtyChannels type is a typedef for QFlags<PtyChannelFlag>. It stores an OR combination of PtyChannelFlag values.

Member Function Documentation

[explicit] KPtyProcess::KPtyProcess(QObject *parent = nullptr)

Constructor

KPtyProcess::KPtyProcess(int ptyMasterFd, QObject *parent = nullptr)

Construct a process using an open pty master.

ptyMasterFd an open pty master file descriptor. The process does not take ownership of the descriptor; it will not be automatically closed at any point.

bool KPtyProcess::isUseUtmp() const

Get whether to register the process as a TTY login in utmp.

Returns whether to register in utmp

KPtyDevice *KPtyProcess::pty() const

Get the PTY device of this process.

Returns the PTY device

KPtyProcess::PtyChannels KPtyProcess::ptyChannels() const

Query to which channels the PTY is assigned.

Returns the output channel handling mode

See also setPtyChannels().

void KPtyProcess::setPtyChannels(KPtyProcess::PtyChannels channels)

Set to which channels the PTY should be assigned.

This function must be called before starting the process.

channels the output channel handling mode

See also ptyChannels().

void KPtyProcess::setUseUtmp(bool value)

Set whether to register the process as a TTY login in utmp.

Utmp is disabled by default. It should enabled for interactively fed processes, like terminal emulations.

This function must be called before starting the process.

value whether to register in utmp.

See also isUseUtmp().