Qt中如何获取CPU使用率?

发布于 2024-09-15 13:55:25 字数 77 浏览 3 评论 0原文

我有一个主服务器和 4 台连接的计算机。我怎样才能知道哪台计算机使用了多少CPU和磁盘。

我使用 Qt 和 C++ 编写代码。

I have a main server and 4 computers connected. How can I learn which computer use how much CPU and disk.

I write code using Qt and C++.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蹲墙角沉默 2024-09-22 13:55:25

Qt 没有这方面的 API。您需要使用平台本机 API。

Qt does not have an API for this. You'll need to use the platforms native API.

生来就爱笑 2024-09-22 13:55:25

也许这可以帮助你:

#include <QDebug>
#include <QProcess>

int main(void){
  setenv("LC_NUMERIC", "C",1);
  QProcess process;
  process.start("/usr/bin/uptime",{},QIODevice::ReadWrite);
  process.waitForFinished();

//Get results
  QString output = process.readAllStandardOutput();
  QString err = process.readAllStandardError();

//Get load portion
  QString loads=output.section("load average: ",1,1);
  QString load_lastmin=loads.section(" ",0,0);
  load_lastmin.remove(load_lastmin.size()-1,1);

//For debug
  qDebug() << "Output:" << output;
  qDebug() << "Error:" << err;
  qDebug() << "Loads:" << loads;
  qDebug() << "Load last min:" << load_lastmin;

  return 0;
}

结果示例:

Output: " 10:04:33 up 3 days, 21:39,  1 user,  load average: 0.55, 0.71, 0.78\n"
Error: ""
Loads: "0.55, 0.71, 0.78\n"
Load last min: "0.55"

Maybe this can help you:

#include <QDebug>
#include <QProcess>

int main(void){
  setenv("LC_NUMERIC", "C",1);
  QProcess process;
  process.start("/usr/bin/uptime",{},QIODevice::ReadWrite);
  process.waitForFinished();

//Get results
  QString output = process.readAllStandardOutput();
  QString err = process.readAllStandardError();

//Get load portion
  QString loads=output.section("load average: ",1,1);
  QString load_lastmin=loads.section(" ",0,0);
  load_lastmin.remove(load_lastmin.size()-1,1);

//For debug
  qDebug() << "Output:" << output;
  qDebug() << "Error:" << err;
  qDebug() << "Loads:" << loads;
  qDebug() << "Load last min:" << load_lastmin;

  return 0;
}

Result example:

Output: " 10:04:33 up 3 days, 21:39,  1 user,  load average: 0.55, 0.71, 0.78\n"
Error: ""
Loads: "0.55, 0.71, 0.78\n"
Load last min: "0.55"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文