gpt4 book ai didi

c++ - Qt & Raspberry Pi 取终端命令的响应值

转载 作者:太空宇宙 更新时间:2023-11-04 12:32:33 24 4
gpt4 key购买 nike

我想读取我板子上CPU的温度(Raspbian中典型的命令“/opt/vc/bin/vcgencmd measure_temp”) 我点击按钮可以执行命令,但是我找不到在我的 Qt 程序中将结果保存到变量中的方法。

代码:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QProcess>

QProcess process;

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{
QProcess lecturatemp;
lecturatemp.startDetached("/bin/sh",QStringList()<<"-c"<<"/opt/vc/bin/vcgencmd measure_temp"); //
//lecturatemp.waitForFinished();
}

最佳答案

根据documentation ,QProcess是QIODevice的子类,所以你可以像读取文件一样读取返回值,像这样:

QByteArray result = lecturatemp.readAll();

如果您想阅读该过程的输出,您应该阅读“通过 channel 沟通”段落:

Processes have two predefined output channels: The standard output channel (stdout) supplies regular console output, and the standard error channel (stderr) usually supplies the errors that are printed by the process. These channels represent two separate streams of data. You can toggle between them by calling setReadChannel(). QProcess emits readyRead() when data is available on the current read channel. It also emits readyReadStandardOutput() when new standard output data is available, and when new standard error data is available, readyReadStandardError() is emitted. Instead of calling read(), readLine(), or getChar(), you can explicitly read all data from either of the two channels by calling readAllStandardOutput() or readAllStandardError().

所以基本上调用以下内容,你应该得到输出:

QByteArray result = lecturatemp.readAllStandardOutput();
or
QByteArray result = lecturatemp.readAllStandardError();

关于c++ - Qt & Raspberry Pi 取终端命令的响应值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42904402/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com