Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: How to catch an error with system call?

I am building a GUI application where I do a system call and call for gnuplot to run a script. Now i want to build in an error message that says when something is wrong (e.g. gnuplot is not installed or in the wrong path).

So I've been thinking about just putting out a QMessageBox, but I don't know how I can check whether the system call succeeded or not.

if(//System call didn't work)
{
    QMessageBox msgBox;
    msgBox.setWindowTitle("Error");
    msgBox.setIcon(QMessageBox::Critical);
    msgBox.setText("GNUPLOT was not installed");
    msgBox.exec();
}

My system call looks like this:

system(gnuplot script.txt);

Any suggestions?

like image 214
Tcanarchy Avatar asked Dec 30 '13 12:12

Tcanarchy


1 Answers

You should use QProcess rather than low-level system call because it is a nice abstraction in a Qt codebase. Otherwise, you will end up dealing with platform specific bits. QProcess already solves that for you. If you happen to be fine with a blocking approach, aka. sync, you could write something like that code below.

QProcess process;

process1.start("gnuplot arg1 arg2 etc");

// Wait for it to start
if(!process.waitForStarted())
    return 0;

bool retval = false;
QByteArray buffer;
while ((retval = process.waitForFinished()));
    buffer.append(process.readAll());

if (!retval) {
    qDebug() << "Process 2 error:" << process.errorString();
    msgBox.setText(buffer);
    return 1;
}

If you would not like to block while the gnuplot script of yours is running, you could connect a slot, or simply lambda with C++11 and on, to the readyRead() signal. For sure, you would also need to connect to the error() signal. The code without lambda to work with pre C++11 environments would look something like this:

GnuPlotReader::GnuPlotReader(QQProcess *process, QObject *parent)
    : QObject(parent)
    , m_process(process)
    , m_standardOutput(stdout)
{
    connect(m_process, SIGNAL(readyRead()), SLOT(handleReadyRead()));
    connect(m_process, SIGNAL(error(QProcess::ProcessError)), SLOT(handleError(QProcess::ProcessError)));
    connect(&m_timer, SIGNAL(timeout()), SLOT(handleTimeout()));

    m_timer.start(5000);
}

GnuPlotReader::~GnuPlotReader()
{
}

void GnuPlotReader::handleReadyRead()
{
    m_readData = m_process->readAll();

    if (!m_timer.isActive())
        m_timer.start(5000);
}

void GnuPlotReader::handleTimeout()
{
    if (m_readData.isEmpty()) {
        m_standardOutput << QObject::tr("No data was currently available for reading from gnuplot") << endl;
    } else {
        m_standardOutput << QObject::tr("GnuPlot successfully run")<< endl;
    }

}

void GnuPlotReader::handleError(QProcess::ProcessError processError)
{
    if (processError == QProcess::ReadError) {
        m_standardOutput << QObject::tr("An I/O error occurred while reading the data, error: %2").arg(m_process->errorString()) << endl;
        messageBox.setText(m_readData);
    }
}
like image 77
lpapp Avatar answered Nov 18 '22 17:11

lpapp