how to resolve and understand how / why it works or not
-
Please understand that I am asking here and it is NOT a Qt question. My task is to start a new process - the parameters are "application" and its parameters. The basic process builds a "Database" and I am not so sure about "bin/sh" - I guess it "runs" a shell... It works and there is no need to get into why at this point. The "problem" is - the "process" is running and I need to interact with it. I can use "write" but I have no idea when and where and how. In the enclosed snippet the "info\n" is "send" to the file ( shell?) before it is finished... I do understand that Qt process can "wait for finished " and I can also get a SIGNAL when the file is actually done building. What I do not get - if the process is actually /physically building / writing a file - what does "wait for finished " waiting for ? Again - I am not asking how to code Qt , I am asking how to code so I can "write" the additional stuff in right time.
std::string Database = BT\_DATABASE\_TEST; std::string Command = "bluetoothctl "; std::string command = Command + std::string(" | tee ") + Database + std::string(" | tee /tmp/temp"); QP->start("/bin/sh", QStringList() << "-c" << command.c\_str()); qDebug()<< "TEST QProcess CONTINUE DEBUG point @line " << QString::number(\_\_LINE\_\_); QP->waitForFinished(); QP->write("info\\n"); writes too soon qDebug() << QP->readAllStandardError(); qDebug() << QP->readAllStandardOutput();
-
Please understand that I am asking here and it is NOT a Qt question. My task is to start a new process - the parameters are "application" and its parameters. The basic process builds a "Database" and I am not so sure about "bin/sh" - I guess it "runs" a shell... It works and there is no need to get into why at this point. The "problem" is - the "process" is running and I need to interact with it. I can use "write" but I have no idea when and where and how. In the enclosed snippet the "info\n" is "send" to the file ( shell?) before it is finished... I do understand that Qt process can "wait for finished " and I can also get a SIGNAL when the file is actually done building. What I do not get - if the process is actually /physically building / writing a file - what does "wait for finished " waiting for ? Again - I am not asking how to code Qt , I am asking how to code so I can "write" the additional stuff in right time.
std::string Database = BT\_DATABASE\_TEST; std::string Command = "bluetoothctl "; std::string command = Command + std::string(" | tee ") + Database + std::string(" | tee /tmp/temp"); QP->start("/bin/sh", QStringList() << "-c" << command.c\_str()); qDebug()<< "TEST QProcess CONTINUE DEBUG point @line " << QString::number(\_\_LINE\_\_); QP->waitForFinished(); QP->write("info\\n"); writes too soon qDebug() << QP->readAllStandardError(); qDebug() << QP->readAllStandardOutput();
I am assuming that your
QP
pointer is a reference to an object of QProcess Class | Qt Core 6.3.2[^]. As stated in the documentation the start method assumes the device mode asReadWrite
, which if it follows the normal rules, suggests you can write to and read from the started process. But writing will only work if the started process is waiting for input on itsstdin
stream. As far as I can see your started process is a simple shell pipeline to write some data tostdout
and a couple of files. So it is not likely to be waiting for input from an external source. -
I am assuming that your
QP
pointer is a reference to an object of QProcess Class | Qt Core 6.3.2[^]. As stated in the documentation the start method assumes the device mode asReadWrite
, which if it follows the normal rules, suggests you can write to and read from the started process. But writing will only work if the started process is waiting for input on itsstdin
stream. As far as I can see your started process is a simple shell pipeline to write some data tostdout
and a couple of files. So it is not likely to be waiting for input from an external source.After more research - I have two distinct usage of the "command" (QProcess) - one writes to the file and terminates and the other one is "interactive" - writes to the file and expects input. As of now I can "monitor" the first one two ways - in code - wait for finished or using "finished" SIGNAL. Right now my SIGNAL is "readyWrite" which ( the name ) does not make much sense - BUT it works with non - interactive command. I am going to take a closer look at QProcess SIGNALs - at least make more sense with non interactive command.As of now I just start the QProcess , and I am not directly ( using SIGNALs) verifying it is actually running / started. After I clean that up I'll take a look at interactive part. Not sure where to start.
-
After more research - I have two distinct usage of the "command" (QProcess) - one writes to the file and terminates and the other one is "interactive" - writes to the file and expects input. As of now I can "monitor" the first one two ways - in code - wait for finished or using "finished" SIGNAL. Right now my SIGNAL is "readyWrite" which ( the name ) does not make much sense - BUT it works with non - interactive command. I am going to take a closer look at QProcess SIGNALs - at least make more sense with non interactive command.As of now I just start the QProcess , and I am not directly ( using SIGNALs) verifying it is actually running / started. After I clean that up I'll take a look at interactive part. Not sure where to start.
Member 14968771 wrote:
Not sure where to start.
The logical place is a tutorial on QProcess. You can only communicate with an independent process in this way, when the input and output streams are connected. The default option to the
start
method setsQIODeviceBase::OpenMode mode = ReadWrite
. Does that mean that you can write to the process or not? Only a QT expert can advise on the answer. -
Member 14968771 wrote:
Not sure where to start.
The logical place is a tutorial on QProcess. You can only communicate with an independent process in this way, when the input and output streams are connected. The default option to the
start
method setsQIODeviceBase::OpenMode mode = ReadWrite
. Does that mean that you can write to the process or not? Only a QT expert can advise on the answer.I have verified that I can "write" to QProcess - the question remains when. Until now the command I have been testing with - which is why I am reluctant to post in QT forum - has been one of the challenges. There is no definite pattern when the command is "single shot" or interactive - depends on how it has been used before.
-
I have verified that I can "write" to QProcess - the question remains when. Until now the command I have been testing with - which is why I am reluctant to post in QT forum - has been one of the challenges. There is no definite pattern when the command is "single shot" or interactive - depends on how it has been used before.
Member 14968771 wrote:
I have verified that I can "write" to QProcess
I am not sure what you mean, the write method is from the QProcess. There is a question on SO at https://stackoverflow.com/questions/45336371/qt-qprocess-how-to-write-to-standard-in[^] which may make some sense. But TBH it is still not clear exactly what you are trying to achieve.
-
Member 14968771 wrote:
I have verified that I can "write" to QProcess
I am not sure what you mean, the write method is from the QProcess. There is a question on SO at https://stackoverflow.com/questions/45336371/qt-qprocess-how-to-write-to-standard-in[^] which may make some sense. But TBH it is still not clear exactly what you are trying to achieve.