问题描述
我正在使用Qt在Linux中进行编码.我了解使用popen或QProcess可以从程序启动终端,但是如何写入呢?我在周围的人周围google都在建议fork()和pipe().我的目的是使用终端执行ICMP ping,并在ping成功后停止.我用popen做到了,但是我无法停止ping进程,因此我的程序无法运行.
I am coding in linux using Qt. I understand that with popen or QProcess I can launch terminal from my program, but how do I write into to it? I google around people are suggesting fork() and pipe().My purpose is to do an ICMP ping with the terminal, and stop when ping successfully. I made it with popen, but I couldn't stop the ping process thus my program won't run.
推荐答案
您没有向终端写任何东西,因为没有终端.您传递要运行的程序的名称及其参数作为 QProcess::start
方法.如果您只需要知道ping是否成功,就足以检查您先前使用QProcess::start
启动的进程的退出代码;您不必读取其输出.
You don't write anything to terminal because there's no terminal. You pass name of a program to run and its arguments as arguments of the QProcess::start
method. If you only need to know if ping was successful or not it's enough to check the exit code of the process which you started earlier using QProcess::start
; you don't have to read its output.
默认情况下,Linux下的ping
会运行,直到将其停止为止.但是,您可以使用-c X
选项仅发送X个数据包,并使用-w X
选项将整个过程的超时设置为X秒.这样,您可以限制ping的运行时间.
以下是使用QProcess
在Windows上运行ping程序的工作示例.对于Linux,您必须相应地更改ping选项(例如,将-n
更改为-c
).在此示例中,ping最多运行X次,其中X是您赋予Ping
类构造函数的选项.一旦这些执行中的任何一个以退出代码0返回(表示成功),就会发出值为true的result
信号.如果没有成功执行,则发出result
信号,其值为false.
By default ping
under Linux runs until you stop it. You can however use -c X
option to send only X packets and -w X
option to set timeout of the whole process to X seconds. This way you can limit the time ping will take to run.
Below is a working example of using QProcess
to run ping program on Windows. For Linux you have to change ping options accordingly (for example -n
to -c
). In the example, ping is run up to X times, where X is the option you give to Ping
class constructor. As soon as any of these executions returns with exit code 0 (meaning success) the result
signal is emitted with value true. If no execution is successful the result
signal is emitted with value 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"
这篇关于在Qt中启动并写入终端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!