2

In my QT widget application I am attempting to run a shellscript that opens a C++ program and provides inputs to the program as well. The program starts a command prompt that requires the users input to start. Once the program is started the output of the program is redirected via the standard output to a text file. I am attempting to use QProcess to open and run this shellscript, then read the standard output that is being used to print the result of the C++ program to the text file. The shell script only runs this process and does not terminate it. This is because I need to continuously read this output into the GUI as the program is running. It will not be sufficient to wait until the program is finished to read this information. I am fairly new to QT and C++ programming. I was hoping that someone could help me with my implementation of this.

QProcess process;
process.start("/home/pi/Desktop/ShellScripts/RunTutorial3.sh");
QString output =process.readAllStandardOutput();
qDebug() << output;
QString err = process.readAllStandardError();
qDebug() << err;

I have experimented with using other read function such as readline and also trying to start the process as a detatched process. I have not had success with any of my experimentations. Is it possible to do what I am attempting in QT. I just need the program to run continuously and for QT to read this output every so often.

Shell script:

#!/bin/bash
cd
cd Desktop
cd tutorial3App
cd bin
echo "start" | ./tutorial3 

C++ code: I need the meanTOE value to be captured in standard output to use in my GUI.

/ Calculate average time to end of discharge
            double meanToE = std::accumulate(ToESamples.begin(), ToESamples.end(), 0.0)/ToESamples.size();
            file << ": EOL in " << meanToE << " s" << std::endl;
Gage Haas
  • 51
  • 1
  • 2
  • 8
  • why do you use a bash to run tutorial3 and do not run it directly with QProcess? when you execute through the bash the tutorial process is separated from the bash. – eyllanesc Mar 01 '18 at 20:58
  • I think my issue is that Qprocess starts the script, but I dont want to provide a closing argument to it. When the script is running it will not let me read from it continuously as its running. I tried opening it with qprocess, but thought itd be easier to use the shell script first. – Gage Haas Mar 01 '18 at 21:10
  • On the contrary it is more difficult, your code has several problems, the first is that probably process is a local variable and is deleted after the 2 print empty things, another problem is that the tutorial process3 is detached from the bash so you can never get the output that prints tutorial3. If you run tutorial3 directly it is more manageable because Qt knows the pid and can get the output – eyllanesc Mar 01 '18 at 21:21
  • I am currently working on running it directly through QT now. Thank you for explaining the issue with this to me. I was just unsure of QProcess at first, as the documentation on it doesn't provide many examples and my knowlege of c++ programming isn't very extensive. So understanding how to implement it is proving to be difficult – Gage Haas Mar 01 '18 at 21:28
  • I recommend you check my example code, if you have problems use `CONFIG += c++11` in the .pro. – eyllanesc Mar 01 '18 at 21:37

2 Answers2

0

I think you have to read about signals and slots in Qt. QProcess has got a signal readyReadStandardOutput. So you have to connect to this signal and in your slot you should use QProcess function readAllStandardOutput. In other words when your shell programm outputs something you catch it in your slot and dump it or whatever you want.

Check the answer on this question. It might help you.

reading and writing to QProcess in Qt Console Application

Polina Bodnar
  • 171
  • 3
  • 15
0

As I said in my comments one of the main problems is that when you run tutorial3 that process is separated so you can not get the output. Therefore, I recommend executing it directly, and QProcess is probably a local variable, eliminating after printing an empty text, a possible solution is to create a pointer. Another improvement would be to use the readyReadStandardOutput and readyReadStandardError signals since the impressions are not automatic.

QProcess *process = new QProcess(this);

connect(process, &QProcess::readyReadStandardOutput, [process, this](){
    QString output =process->readAllStandardOutput();
    qDebug() << "output: "<< output;
});

connect(process, &QProcess::readyReadStandardError, [process](){
    QString err = process->readAllStandardError();
    qDebug() << "error: "<<err;
});

process->setWorkingDirectory("/home/pi/Desktop/tutorial3App/bin/")
process->start("tutorial3", QStringList() << "start");
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • this doesn't seem to even open the program, but I will try to go off of this. Thank you – Gage Haas Mar 01 '18 at 22:19
  • @GageHaas What do you get as an output? – eyllanesc Mar 01 '18 at 22:20
  • It output a 0 and then a 1 on the next line – Gage Haas Mar 01 '18 at 22:37
  • @GageHaas I could provide a [mcve] of your code, it works great for me – eyllanesc Mar 01 '18 at 22:40
  • @GageHaas You must use the slots, that's the main part. Does the program compile you? You could show more information on how you are implementing my solution. – eyllanesc Mar 01 '18 at 22:50
  • Is there anything I would need to change in the header files for this code to work? If I leave out the connects, the above code should at very least open the application and it does not. – Gage Haas Mar 01 '18 at 22:51
  • @GageHaas I find it strange, you do not need to add anything, please show your full code. – eyllanesc Mar 01 '18 at 22:52
  • I must have overlooked something. I ran your code again and it opens the program. However it doesn't seem to pass the start argument to the program. – Gage Haas Mar 01 '18 at 23:03
  • @GageHaas if you do not provide a [mcve] of your problem I will not be able to help you, it seems to me that those *problems* are caused by your lack of experience, and probably I can not help you anymore since I do not have any more relevant information. :D – eyllanesc Mar 01 '18 at 23:06