Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How is parameter "QIODevice::OpenMode mode = ReadWrite" used in QProcess?



  • Could somebody kindly assist me in better understanding of this QProcess parameter ?
    I understand QIODevice: can be assigned to QFile, but I fail to see how it

    actually ties to QProcess.

    All I see it has default mode assignment.

    The attached copy of doc really does not say much , except as highlighted.

    void QProcess::start(const QString &program, const QStringList &arguments, QIODevice::OpenMode mode = ReadWrite)
    
    Starts the given program in a new process, passing the command line arguments in arguments.
    
    The QProcess object will immediately enter the Starting state. If the process starts successfully, QProcess will emit started(); otherwise, errorOccurred() will be emitted.
    
    Note: Processes are started asynchronously, which means the started() and errorOccurred() signals may be delayed. Call waitForStarted() to make sure the process has started (or has failed to start) and those signals have been emitted.
    
    Note: No further splitting of the arguments is performed.
    
    Windows: The arguments are quoted and joined into a command line that is compatible with the CommandLineToArgvW() Windows function. For programs that have different command line quoting requirements, you need to use setNativeArguments(). One notable program that does not follow the CommandLineToArgvW() rules is cmd.exe and, by consequence, all batch scripts.
    
    **The OpenMode is set to mode.**
    
    


  • A QIODevice is not "assigned to" a QFile.
    A QFile is-a QIODevice (through QFileDevice).
    A QProcess is-a QIODevice.
    A QTcpSocket is-a QIODevice also (through QAbstractSocket).

    When you have a QProcess object with a running sub-process then you can read from or write to the QProcess object in the same general way as you can with any QIODevice. QProcess produces signals when data is available just like any other QIODevice.
    Unlike QFile, QProcess is not reading from or writing to a file, it is reading from or writing to the the standard output or input streams of the subprocess. The default is ReadWrite because that is generally what you want.



  • @ChrisW67 said in How is parameter "QIODevice::OpenMode mode = ReadWrite" used in QProcess?:

    A QProcess is-a QIODevice.

    OK. just humor me - where is is written that A QProcess is-a QIODevice?

    So if "mode" is set to "write" - do I still get the stdio /stderr from QProcess?



  • @AnneRanch said in How is parameter "QIODevice::OpenMode mode = ReadWrite" used in QProcess?:

    OK. just humor me - where is is written that A QProcess is-a QIODevice?

    https://doc.qt.io/qt-5/qprocess.html :

    QProcess Class

    ...

    Inherits: QIODevice

    So if "mode" is set to "write" - do I still get the stdio /stderr from QProcess?

    No! Your calling application needs read access to the QProcess/QIODevice in order to read from its stdout/stderr, and it would need write access to in order to write to its stdin, if you need that for the particular command. That is why the default is QIODevice::ReadWrite, to allow either/both. In your particular case QIODevice::ReadOnly would work, but not QIODevice::WriteOnly.


Log in to reply