在Qt中调用Windows API GetSystemInfo可以获取CPU的相关信息,使用Qt的界面控件显示在界面上。在实现的过程中遇到了一个问题,就是显示地址信息在界面控件上。
试了好几种方法,都不能显示,最后想到了格式化函数sprintf,将地址转为char类型的数组或者指针,再转为字符串就可以正常显示了。
如果有人有更好的方法来显示地址,欢迎交流。
完整代码:
#pragma execution_character_set("utf-8") #ifndef QCPUINFO_H #define QCPUINFO_H #include <QtWidgets/QWidget> #include <QLabel> class QCPUInfo : public QWidget { Q_OBJECT public: QCPUInfo(QWidget *parent = 0); ~QCPUInfo(); private: void getCPUInfo(); QLabel *m_pageSize; QLabel *m_minAddress; QLabel *m_maxAddress; QLabel *m_mask; QLabel *m_processorNum; QLabel *m_processorType; QLabel *m_processorLevel; QLabel *m_processorVersion; }; #endif // QCPUINFO_H
#include "qcpuinfo.h" #include <windows.h> #include <QVBoxLayout> QCPUInfo::QCPUInfo(QWidget *parent) : QWidget(parent) { m_pageSize = new QLabel(this); m_minAddress = new QLabel(this); m_maxAddress = new QLabel(this); m_mask = new QLabel(this); m_processorNum = new QLabel(this); m_processorType = new QLabel(this); m_processorLevel = new QLabel(this); m_processorVersion = new QLabel(this); QVBoxLayout *pVBox = new QVBoxLayout(this); pVBox->addWidget(m_pageSize); pVBox->addWidget(m_minAddress); pVBox->addWidget(m_maxAddress); pVBox->addWidget(m_mask); pVBox->addWidget(m_processorNum); pVBox->addWidget(m_processorType); pVBox->addWidget(m_processorLevel); pVBox->addWidget(m_processorVersion); getCPUInfo(); } QCPUInfo::~QCPUInfo() { } void QCPUInfo::getCPUInfo() { SYSTEM_INFO sysInfo; GetSystemInfo(&sysInfo); m_pageSize->setText(QString("分页大小:\t%1").arg(sysInfo.dwPageSize)); char buff[32]; sprintf(buff, "%p", sysInfo.lpMinimumApplicationAddress); m_minAddress->setText(QString("最小寻址:\t%1").arg(buff)); sprintf(buff, "%p", sysInfo.lpMaximumApplicationAddress); m_maxAddress->setText(QString("最大寻址:\t%1").arg(buff)); m_mask->setText(QString("掩码:\t\t%1").arg(sysInfo.dwActiveProcessorMask)); m_processorNum->setText(QString("处理器个数:\t%1").arg(sysInfo.dwNumberOfProcessors)); m_processorType->setText(QString("类型:\t\t%1").arg(sysInfo.dwProcessorType)); m_processorLevel->setText(QString("等级:\t\t%1").arg(sysInfo.wProcessorLevel)); m_processorVersion->setText(QString("版本:\t\t%1").arg(sysInfo.wProcessorRevision)); }
交流qq:1245178753
本文地址:http://blog.csdn.net/u011417605/article/details/51567716
时间: 2024-10-12 09:17:03