Outputting QProcess readAll response to label - c++

I have a QProcess where i would like to output the response in a label. First off, here is what i have tried:
QProcess *proc = new QProcess();
proc->setProcessChannelMode(QProcess::MergedChannels);
proc->start(cmdLineRequest.toUtf8().constData()); // cmdLineRequest is omitted
if (!proc->waitForFinished()) {
qDebug() << "Make failed:" << proc->errorString();
ui->topBarcode->setText(QString(proc->errorString()));
} else {
qDebug() << "Make output:" << proc->readAll();
ui->topBarcode->setText(QString(proc->readAll()) + "asdf");
}
proc->readAll() is a QByteArray and setText accepts a QString. From what i've read, i should be able to cast the QByteArray to a QString, howver it does not work. I have also tried to convert proc->readAll() with the QString class
->setText(QString::fromUtf8(proc->readAll())) // not working
->setText(QString::fromLatin1(proc->readAll())) // not working
->setText(QString::fromLocal8Bit(proc->readAll())) // not working
... etc ...
It seems weird, since i'm adding pictures to labels in nearly the same matter using setPixmap(QPixmap::fromImage(image))
Any help appreciated, thank you.
Update:
If i add a QMessageBox before the end of the method that the above block of code belongs to, i can see the text added to the label. However when i close the QMessageBox, the text dissapears. Am i giving an address position to the label with proc->readAll() or how come this behaviour? Thank you.

The problem here is that you're calling proc->readAll twice; the first for the qDebug output and then again for the string which you set on the label.
{
qDebug() << "Make output:" << proc->readAll();
ui->topBarcode->setText(QString(proc->readAll()) + "asdf");
}
I expect that as QProcess is a QIODevice, it's returning a buffered byte array. When you read it, it removes that from the buffer.
Therefore, create a temporary string and read the buffer once, before calling qDebug and setting the string to the label: -
{
QString output = proc->readAll();
qDebug() << "Make output:" << output;
ui->topBarcode->setText(output + "asdf");
}

You should listen to readyReadStandardOutput() signal and call readAll() when you receive signal.
or you can call
bool waitForReadyRead(int msecs = 30000)
before calling readAll().

Related

Connect signal of superclass of object

I am working with a QProcess and have connected QProcess's signal readyReadStandardOutput().
That process normally spits out data to the console regularly, but the readyReadStandardOutput() seems to batch results and only emit every half a minute or so (with all accumulated data).
I want to access the "live feed" of the QProcess so I thought maybe QProcess's superclass QIODevice has some other signals.
Other solutions instead of using bytesWritten are also welcome
Now I'm trying to connect bytesWritten, but it doesn't let me.
Code:
void MainWindow::on_Program_clicked() {
program= new QProcess(this);
QString file = "../folder/program/program.exe";
QString directory = "../folder/program/";
//qint64 pid;
program->setWorkingDirectory(directory);
program->start(file, {""});
program->waitForStarted();
connect(program, SIGNAL(readyReadStandardOutput()), this, SLOT(readOutput()));
//ERROR: "QObject::connect: No such signal QProcess::bytesWritten() in ..\---\mainwindow.cpp:45
connect(program, SIGNAL(bytesWritten()), this, SLOT(myBytesWritten()));
}
void MainWindow::myBytesWritten() {
QProcess *program = dynamic_cast<QProcess *>(sender());
QByteArray outData = program->readAll();
qDebug() << "DEBUG: " + outData;
}
//Works, but only emits a signal every so often, and not every time a new line is written to the console as when I launch the exe normally
void MainWindow::readOutput(){
QProcess *program = dynamic_cast<QProcess *>(sender());
QByteArray outData = program->readAll();
qDebug() << "DEBUG: " + outData;
}
If you want to use the old, deprecated way of connecting signals to slots, you need to also include the parameter list:
connect(program, SIGNAL(bytesWritten(int)), this, SLOT(myBytesWritten()));
Or you can just use the modern, compile-time Qt5 way:
connect(program, &QProcess::bytesWritten, this, &MainWindow::myBytesWritten);
Also note that bytesWritten fires when a write command from YOUR end has succeeded. You don't seem to be sending any input so this will never fire.

Read stdoutput of continuous QProcess in Qt

I am having some problem regarding QProcess using Qt. I have connected the following function with the onClick event of a push button. Basically, I want to execute another file when this button is clicked, and get its output on my Qt program. This file calculator executes, displays some output, and then waits for an input from the user.
void runPushButtonClicked() {
QProcess myprocess;
myprocess.start("./calculator")
myprocess.waitForFinished();
QString outputData= myprocess.readStandardOutput();
qDebug() << outputData;
}
In a scenario, when the calculator is such a file that only outputs some results and terminates eventually, this works perfect. But, in case when the calculator waits for some further input from the user after outputting some results, I get nothing in my outputData. In fact, waitForFinished() would time out, but even when I remove waitForFinished(), the outputData would still be empty.
I have already tried some of the solutions available here on SO, but have been unable to handle this case. Any guidance will be much appreciated.
I would suggest, that you setup a signal handler, that gets called when the subprocess produces output. E.g. you have to connect to readyReadStandardOutput.
Then you can identify, when subprocess demands input and send the input you want. This will be done in readSubProcess().
main.cpp
#include <QtCore>
#include "Foo.h"
int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
Foo foo;
qDebug() << "Starting main loop";
app.exec();
}
In the following, the subprocess is started and the input checked. When the calculator program finishes, the main also exits.
Foo.h
#include <QtCore>
class Foo : public QObject {
Q_OBJECT
QProcess myprocess;
QString output;
public:
Foo() : QObject() {
myprocess.start("./calculator");
// probably nothing here yet
qDebug() << "Output right after start:"
<< myprocess.readAllStandardOutput();
// get informed when data is ready
connect(&myprocess, SIGNAL(readyReadStandardOutput()),
this, SLOT(readSubProcess()));
};
private slots:
// here we check what was received (called everytime new data is readable)
void readSubProcess(void) {
output.append(myprocess.readAllStandardOutput());
qDebug() << "complete output: " << output;
// check if input is expected
if (output.endsWith("type\n")) {
qDebug() << "ready to receive input";
// write something to subprocess, if the user has provided input,
// you need to (read it and) forward it here.
myprocess.write("hallo back!\n");
// reset outputbuffer
output = "";
}
// subprocess indicates it finished
if (output.endsWith("Bye!\n")) {
// wait for subprocess and exit
myprocess.waitForFinished();
QCoreApplication::exit();
}
};
};
For the subprocess calculator a simple script is used. You can see the where output is generated and where input is expected.
#/bin/bash
echo "Sub: Im calculator!"
# some processing here with occasionally feedback
sleep 3
echo "Sub: hallo"
sleep 1
echo "Sub: type"
# here the script blocks until some input with '\n' at the end comes via stdin
read BAR
# just echo what we got from input
echo "Sub: you typed: ${BAR}"
sleep 1
echo "Sub: Bye!"
If you do not need to do anything else in your main process (e.g. show a GUI, manage other threads/processes....), the easiest would be to just in a sleep in a loop after subprocess creation and then something like readSubprocess.

Get the checksum of an open QIODevice

i need the checksum of a file and found this, which works perfectly fine. Now i want to change this function to take a pointer to a QIODevice that has been opened before with the following lines:
if (!file.open(QFile::ReadOnly | QFile::Text))
{
...
}
This is passed to read (reader.read(&file);) as device:
bool XmlReader::read(QIODevice* device)
{
QByteArray b = fileChecksum(device);
...
}
This is my implementation of fileChecksum. It returns a checksum, but i am caught in a loop forever and i am getting an xml parse error. What am i doing wrong here?
QByteArray XmlReader::fileChecksum(QIODevice* device)
{
if (device->isOpen())
{
QCryptographicHash hash(QCryptographicHash::Sha256);
if (hash.addData(device)) {
return hash.result();
}
}
return QByteArray();
}
EDIT
right after QByteArray b = fileChecksum(device); i do:
qDebug() << "Checksum: " << b.toHex();
whick keeps printing and printing and printing...
The parse error is: premature end of document which is rubbish.
Hope this helps.
Since the lines of code that eventually caused the error are not in view I can only speculate about what happened.
The function fileChecksum called hash.addData(device) which read the QIODevice until the end and kept the cursor position there.
Most likely you tried to read from the QIODevice afterwards which would explain the premature end of documen message.
As a fast workaround you can just try to reset the position afterwards with
auto pos = device->pos();
QByteArray b = fileChecksum(device);
device->seek(pos);
But you should only read the data once if you can (to support non random-access QIODevices too). For example you can store the result in a QBuffer and use that as a QIODevice. Like this:
bool XmlReader::read(QIODevice* device)
{
QByteArray contents = device->readAll();
QBuffer buffer(&contents);
device = &buffer;//you can also just use &buffer from here on out instead of overwriting the pointer
QByteArray b = fileChecksum(device);
device->reset();
/* ... further reads from device here */
}

Qt getting network requests with QNetworkReply that download data to temp file

I am having some issues reading an online file. I'm trying to read what is in the file after it gets downloaded to a temporary file. Here is my code:
void MainWindow::fileIsReady( QNetworkReply * reply)
{
QTemporaryFile tmpFile;
tmpFile.write(reply->readAll());
QByteArray asdf = reply->readAll();
qDebug() (QString("%1").arg(asdf.length())); // returns 0
if (tmpFile.open())
{
qDebug << "attempting to read file";
QTextStream stream(&tmpFile);
QString value = stream.readAll();
qDebug << value; // value is returning nothing
}
else
{
qDebug() << "failed to open internet file";
}
}
// in MainWindow constructor (MainWindow::MainWindow)...
QNetworkAccessManager * manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(fileIsReady(QNetworkReply*)) );
manager->get(QNetworkRequest(QUrl("https://www.website.com/stuff/file.exe.md5")));
I'm going to be using this to compare two md5 strings.
There are several issues in your code:
You need to open tmpFile before writing to it.
reply->readAll() will return data only once. Further calls will return empty array. Once you received data using readAll, it's your responsibility to store it in a variable if you need it later.
After you wrote someting to file, the file pointer is at its end. You cannot read anything from it because there is no data there. You can use seek to move pointer to the beginning of the file and read its content.
There is no point in reading from file just after you wrote data to it. You can use QTextStream on QNetworkReply directly to read text from it. (Maybe this was just for debugging, I don't know.)
It's hard to believe that you need to create a temporary file just to calculate md5. There are simplier ways to do that.
So turns out I was dumb and forgot to open the reply first. Also, it was unnecessary for me to create a temp file. Here is my solution::
void MainWindow::fileIsReady( QNetworkReply * reply)
{
if (reply->error() == QNetworkReply::NoError)
{
if (reply->open(QIODevice::ReadOnly))
{
QByteArray asdf = reply->readAll();
qDebug() << (QString("asdf %1").arg(asdf.length()));
qDebug() << (QString(asdf));
}
else
{
qDebug << "cant open reply";
}
}
}

Qt Serial Port communication

I am writing a Qt application to communicate with another computer over a serial port. I have 2 real issues.
1.
I can send and receive data fine, but sometimes the serial port "eats" part of my input.
For example if I send:
cd /application/bin
sometimes (not always) it will only receive:
cd /applica
(Since it's a terminal it echoes the input back. Also my prompt tells me I'm clearly in the wrong spot.)
2.
Also, sometimes the Qt slot which fires when there is data available doesn't fire even though I know that there's data I can receive. If I send another \r\n down the port the slot will fire.
For example sometimes I'll ls something, and the command name will be read back from the port, but the contents of the folder sit there in limbo until I hit return again. Then I get the listing of the directory and two prompts.
Here's my code:
void Logic::onReadyRead(){
QByteArray incomingData;
incomingData = port->readAll();
QString s(incomingData);
emit dataAvailable(s);// this is a Qt slot if you don't know what it is.
qDebug() << "in:"<< s.toLatin1();
}
void Logic::writeToTerminal(QString string )
{
string.append( "\r\n");
port->write((char*)string.data(), string.length());
if ( port->bytesToWrite() > 0){
port->flush();
}
qDebug() << "out:" << string.toLatin1();
}
I found the solution, and I suspect it was an encoding error, but I'm not sure. Instead of sending a QString down the serial port, sending a QByteArray fixed both problems. I changed the writeToTerminal() method:
void Logic::writeToTerminal(QString string )
{
string.append( "\r");
QByteArray ba = string.toAscii();
port->write(ba);
}
From this forum, it appears that sometimes not all the data gets sent, and whatever does gets sent has a '\0' appended to it. So if
cd /applica'\0'
got sent, then the port->readAll() would stop there, because it thinks it has read everything.
One suggested answer on that forum was to read line by line, which your code almost does. So I think in your case, you can change your code to:
void Logic::onReadyRead(){
QByteArray incomingData;
if(port->canReadLine()) {
incomingData = port->readLine();
QString s(incomingData);
emit dataAvailable(s);// this is a Qt slot if you don't know what it is.
qDebug() << "in:"<< s.toLatin1();
}
}
void Logic::writeToTerminal(QString string )
{
string.append( "\r\n");
port->write((char*)string.data(), string.length());
if ( port->bytesToWrite() > 0){
port->flush();
}
qDebug() << "out:" << string.toLatin1();
}