Run and write to terminal in Qt

I am coding in linux using Qt. I understand that with popen or QProcess I can start the terminal from my program, but how can I write to it? I google around people offers fork () and pipe (). My goal is to perform ICMP ping with the terminal and stop when ping is successful. I did this with popen, but I could not stop the ping process, so my program will not work.

+1
source share
1 answer

You do not write anything to the terminal, because there is no terminal. You pass the name of the program to be launched and its arguments as arguments to the method QProcess::start. If you only need to know if ping was successful or not, just check the exit code of the process that you started with QProcess::start; you do not need to read its output.

from ping (8) - Linux man page

If ping did not receive a packet response at all, it will exit with code 1. If the number of packets and the deadline are indicated, and less than count packets are received by the deadline, it will also exit with code 1. If there is another error, it will exit with code 2. In Otherwise, it exits with code 0. This allows you to use the exit code to find out if the host is alive or not.

ping Linux , . -c X X- -w X, X . , , ping.
QProcess ping Windows. Linux ping ( -n - -c). ping X , X - , ping. 0 ( ), result true. , result false.

#include <QCoreApplication>
#include <QObject>
#include <QProcess>
#include <QTimer>
#include <QDebug>


class Ping : public QObject {

    Q_OBJECT

public:

    Ping(int count)
    : QObject(), count_(count) {

        arguments_ << "-n" << "1" << "example.com";

        QObject::connect(&process_,
                         SIGNAL(finished(int, QProcess::ExitStatus)),
                         this,
                         SLOT(handlePingOutput(int, QProcess::ExitStatus)));
    };

public slots:

    void handlePingOutput(int exitCode, QProcess::ExitStatus exitStatus) {
        qDebug() << exitCode;
        qDebug() << exitStatus;
        qDebug() << static_cast<QIODevice*>(QObject::sender())->readAll();
        if (!exitCode) {
            emit result(true);
        } else {
            if (--count_) {
                QTimer::singleShot(1000, this, SLOT(ping()));
            } else {
                emit result(false);
            }
        }
    }

    void ping() {
        process_.start("ping", arguments_);
    }

signals:

    void result(bool res);

private:

    QProcess process_;
    QStringList arguments_;
    int count_;
};


class Test : public QObject {

    Q_OBJECT

public:
    Test() : QObject() {};

public slots:
    void handle(bool result) {
        if (result)
            qDebug() << "Ping suceeded";
        else
            qDebug() << "Ping failed";
    }
};


int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    Test test;
    Ping ping(3);
    QObject::connect(&ping,
                     SIGNAL(result(bool)),
                     &test,
                     SLOT(handle(bool)));

    ping.ping();
    app.exec();
}

#include "main.moc"
+1

Source: https://habr.com/ru/post/1711396/


All Articles