我想以编程方式运行Linux命令并在文本浏览器中显示输出.这是我的代码:
void MainWindow::on_pushButton_clicked(){
QString qstr;
FILE *cl = popen("ifconfig eth0", "r");
char buf[1024];
while (fgets(buf, sizeof(buf), cl) != 0) {
qstr = QString::fromutf8(buf);
ui->textBrowser->setText(qstr);
}
pclose(ls);
}
Run Code Online (Sandbox Code Playgroud)
但我在文本浏览器中什么都没有.如果我改变qstr了ui->textBrowser->setText(qstr);一些任意"string",它工作正常.有帮助吗?!谢谢.
小智 5
在您使用popen的示例中:
qstr += QString::fromUtf8(buf);
Run Code Online (Sandbox Code Playgroud)
但最好使用QProcess.对于动态输出使用:
QProcess* ping_process = new QProcess(this);
connect(ping_process, &QProcess::readyReadStandardOutput, [=] {
ui->textBrowser->append(ping_process->readAllStandardOutput());
});
ping_process->start("ping", QStringList() << "8.8.8.8");
Run Code Online (Sandbox Code Playgroud)
使用lambda时不要忘记添加.pro文件:
CONFIG += c++11
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
512 次 |
| 最近记录: |