Qt QProcess获取内存信息
Qt QProcess get memory information
我正在尝试使用 QProcess 获取内存,我使用的是 RedHat 7.3,如果我打开一个终端并输入 free,这会给我:
total used free shared buff/cache available
Mem: 7865728 1602988 3984928 297040 2277812 5552268
Swap: 8126460 0 8126460
我尝试在 QT 中生成相同的内容:
QProcess p;
p.start("free");
p.waitForFinished();
QString strMemory = p.readAllStandardOutput();
qDebug() << strMemory;
p.close();
但是这不起作用,我的应用程序挂起,我也试过:
sh free
没有更好。
尝试这样的事情:
const QString command { "free" };
QProcess p {};
p.start( command );
if ( !p.waitForFinished( -1 ) )
{
qWarning() << "Error:" << p.readAllStandardError();
return;
}
const auto& output = p.readAllStandardOutput();
qDebug() << "Output:" << output;
您使用的同步 API 错误。试试异步的:
QProcess p;
QString strMemory;
QObject::connect(&p,&QProcess::readyReadStandardOutput,[&strMemory,&p]()->void{strMemory += QString::fromLatin1(p.readAllStandardOutput());});
QObject::connect(&p,&QProcess::readyReadStandardError,[&strMemory,&p]()->void{strMemory += QString::fromLatin1(p.readAllStandardError());});
QObject::connect(&p,static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),[&strMemory]()->void{qDebug() << strMemory;});
p.start("free");
经过多次尝试后,我发现启动子进程在 IDE 之外有效,但在其中无效。
我正在尝试使用 QProcess 获取内存,我使用的是 RedHat 7.3,如果我打开一个终端并输入 free,这会给我:
total used free shared buff/cache available
Mem: 7865728 1602988 3984928 297040 2277812 5552268
Swap: 8126460 0 8126460
我尝试在 QT 中生成相同的内容:
QProcess p;
p.start("free");
p.waitForFinished();
QString strMemory = p.readAllStandardOutput();
qDebug() << strMemory;
p.close();
但是这不起作用,我的应用程序挂起,我也试过:
sh free
没有更好。
尝试这样的事情:
const QString command { "free" };
QProcess p {};
p.start( command );
if ( !p.waitForFinished( -1 ) )
{
qWarning() << "Error:" << p.readAllStandardError();
return;
}
const auto& output = p.readAllStandardOutput();
qDebug() << "Output:" << output;
您使用的同步 API 错误。试试异步的:
QProcess p;
QString strMemory;
QObject::connect(&p,&QProcess::readyReadStandardOutput,[&strMemory,&p]()->void{strMemory += QString::fromLatin1(p.readAllStandardOutput());});
QObject::connect(&p,&QProcess::readyReadStandardError,[&strMemory,&p]()->void{strMemory += QString::fromLatin1(p.readAllStandardError());});
QObject::connect(&p,static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),[&strMemory]()->void{qDebug() << strMemory;});
p.start("free");
经过多次尝试后,我发现启动子进程在 IDE 之外有效,但在其中无效。