qt 获取系统信息 跨平台支持
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
只回答业务咨询
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
//QtSystemInfo.h /* * File: QtSystemInfo.h * Author: Adrian Łubik (adrian5632@gmail.com) * License: GNU LGPL v2 or newer * Copyright: (c) 2010 Adrian Łubik */ #ifndef QTSYSTEMINFO_H #define QTSYSTEMINFO_H #define QTSYSTEMINFO_VERSION 0x009000 #include <QObject> #include <QString> class QtSystemInfoPrivate; class QtSystemInfo: public QObject { public: enum SystemType { UnknownType = -1, UnknownUnix, MacOSX, Windows, WindowsCE, Linux, UnknownBSD, FreeBSD, OpenBSD, NetBSD }; enum ArchitectureType { UnknownArchitectureType = -1, I386, I486, I586, I686, X86_64, PPC, PPC64 }; QtSystemInfo(QObject *parent = 0); ~QtSystemInfo(); /** * @return Name of currently running system's kernel. */ QString kernelName() const; /** * @return Version of currently running system's kernel. */ QString kernelVersion() const; /** * @return Architecture of the running system. */ QString architectureName() const; /** * @return Type of currently running system's architecture. */ ArchitectureType architecture() const; /** * @return Type of currently running system. */ SystemType systemType() const; /** * @return Name of currently running system. */ QString systemName() const; /** * @return Version of currently running system. */ QString systemVersion() const; /** * @param format A format of the information to be returned. * The following strings will be replaced: * %SYS_NAME - replaced with system name. * %SYS_VERSION - replaced with system version. * %KERN_NAME - replaced with kernel name. * %KERN_VERSION - replaced with kernel version. * %ARCH - replaced with architecture. * If no format given, the method will default to: %SYS_NAME %SYS_VERSION (%KERN_NAME %KERN_VERSION %ARCH). * * @return Formatted information about the running system. */ QString getSystemInformation(QString format = QString()); private: QtSystemInfoPrivate *d; }; #endif |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 |
//QtSystemInfo.cpp /* * File: QtSystemInfo.cpp * Author: Adrian Łubik (adrian5632@gmail.com) * License: GNU LGPL v2 or newer * Copyright: (c) 2010 Adrian Łubik */ #include "QtSystemInfo.h" #include <QtGlobal> static const char *UNKNOWN = "UNKNOWN"; class QtSystemInfoPrivate { public: QtSystemInfoPrivate() {} virtual ~QtSystemInfoPrivate() {} virtual QtSystemInfo::ArchitectureType architecture() = 0; virtual QtSystemInfo::SystemType systemType() = 0; virtual QString architectureName() = 0; virtual QString kernelName() = 0; virtual QString kernelVersion() = 0; virtual QString systemName() = 0; virtual QString systemVersion() = 0; }; // -------------------------------------------------- // // Any Unix // // -------------------------------------------------- // #ifdef Q_OS_UNIX #include <sys/utsname.h> class QtSystemInfoUnix: public QtSystemInfoPrivate { public: QtSystemInfoUnix():m_kernelName(UNKNOWN), m_kernelVersion(UNKNOWN), m_architectureName(UNKNOWN), m_architecture(QtSystemInfo::UnknownArchitectureType) { struct utsname buffer; if (uname(&buffer) == 0) { m_kernelVersion = buffer.release; m_kernelName = buffer.sysname; m_architectureName = buffer.machine; if (m_architectureName == "i386") m_architecture = QtSystemInfo::I386; else if (m_architectureName == "i486") m_architecture = QtSystemInfo::I486; else if (m_architectureName == "i586") m_architecture = QtSystemInfo::I586; else if (m_architectureName == "i686") m_architecture = QtSystemInfo::I686; else if (m_architectureName == "x86_64") m_architecture = QtSystemInfo::X86_64; else if (m_architectureName == "ppc") m_architecture = QtSystemInfo::PPC; else if (m_architectureName == "ppc64") m_architecture = QtSystemInfo::PPC64; } } virtual QString architectureName() { return m_architectureName; } virtual QtSystemInfo::ArchitectureType architecture() { return m_architecture; } virtual QString kernelName() { return m_kernelName; } virtual QString kernelVersion() { return m_kernelVersion; } virtual QtSystemInfo::SystemType systemType() { return QtSystemInfo::UnknownUnix; } virtual QString systemName() { return "Unix"; } virtual QString systemVersion() { return UNKNOWN; } private: QString m_kernelName; QString m_kernelVersion; QString m_architectureName; QtSystemInfo::ArchitectureType m_architecture; }; #endif // -------------------------------------------------- // // Mac OS X // // -------------------------------------------------- // #ifdef Q_OS_MAC #include <QSysInfo> class QtSystemInfoMac: public QtSystemInfoUnix { public: QtSystemInfoMac() {} virtual QString systemName() { return "Mac OS X"; } virtual QString systemVersion() { switch (QSysInfo::MacintoshVersion) { case QSysInfo::MV_10_0: return "10.0"; case QSysInfo::MV_10_1: return "10.1"; case QSysInfo::MV_10_2: return "10.2"; case QSysInfo::MV_10_3: return "10.3"; case QSysInfo::MV_10_4: return "10.4"; case QSysInfo::MV_10_5: return "10.5"; case QSysInfo::MV_10_6: return "10.6"; default: return UNKNOWN; } } virtual QtSystemInfo::SystemType systemType() { return QtSystemInfo::MacOSX; } }; #endif // -------------------------------------------------- // // Linux // // -------------------------------------------------- // #ifdef Q_OS_LINUX #include <QFile> #include <QRegExp> static bool scanSuSEReleaseFile(QString &name, QString &version) { name.clear(); version.clear(); QFile releaseFile("/etc/SuSE-release"); if (releaseFile.exists()) { name = "SuSE"; if (releaseFile.open(QFile::ReadOnly)) { while (!releaseFile.atEnd()) { QString line = releaseFile.readLine(500).trimmed(); if (line.startsWith("VERSION = ")) { version = line.remove(0, 10); releaseFile.close(); return true; } } releaseFile.close(); } } return false; } static bool scanRedHatReleaseFile(QString &name, QString &version) { name.clear(); version.clear(); QFile releaseFile("/etc/redhat-release"); if (releaseFile.exists() && releaseFile.open(QFile::ReadOnly)) { QRegExp fedoraRegExp("(^Fedora)\s+[release]*\s+([0-9]{1,2})\s+" ); QRegExp pclinuxosRegExp("(^PCLinuxOS)\s+release\s+([0-9]{4})\s+\(PCLinuxOS\)\s+for"); QString line = releaseFile.readLine(500); releaseFile.close(); if (line.contains(fedoraRegExp)) { name = fedoraRegExp.cap(1); version = fedoraRegExp.cap(2); return true; } else if (line.contains(pclinuxosRegExp)) { name = pclinuxosRegExp.cap(1); version = pclinuxosRegExp.cap(2); return true; } } return false; } static bool scanLSBReleaseFile(QString &name, QString &version) { name.clear(); version.clear(); QFile releaseFile("/etc/lsb-release"); if (releaseFile.exists() && releaseFile.open(QFile::ReadOnly)) { while (!releaseFile.atEnd()) { QString line = releaseFile.readLine(500).trimmed(); if (line.startsWith("DISTRIB_ID=") && line.count() > 11) { name = line.mid(11); if (!name.isEmpty() && !version.isEmpty()) { releaseFile.close(); return true; } } else if (line.startsWith("DISTRIB_RELEASE=") && line.count() > 16) { version = line.mid(16); if (!name.isEmpty() && !version.isEmpty()) { releaseFile.close(); return true; } } } } return false; } static bool scanDebianReleaseFile(QString &name, QString &version) { name.clear(); version.clear(); QFile releaseFile("/etc/debian_version"); if (releaseFile.exists() && releaseFile.open(QFile::ReadOnly)) { QRegExp debianRegExp("(^[0-9]{1}\.[0-9]\s*$)"); QString line = releaseFile.readLine(100); releaseFile.close(); if (line.startsWith("lenny/sid")) { name = "Debian"; version = "4.0"; return true; } else if (line.contains(debianRegExp)) { name = "Debian"; version = debianRegExp.cap(1); return true; } } return false; } class QtSystemInfoLinux: public QtSystemInfoUnix { public: QtSystemInfoLinux() { if (scanSuSEReleaseFile(m_distributionName, m_distributionVersion)) return; if (scanRedHatReleaseFile(m_distributionName, m_distributionVersion)) return; if (scanLSBReleaseFile(m_distributionName, m_distributionVersion)) return; if (scanDebianReleaseFile(m_distributionName, m_distributionVersion)) return; m_distributionName = "Linux"; m_distributionVersion = UNKNOWN; } virtual QtSystemInfo::SystemType systemType() { return QtSystemInfo::Linux; } virtual QString systemName() { return m_distributionName; } virtual QString systemVersion() { return m_distributionVersion; } private: QString m_distributionName; QString m_distributionVersion; }; #endif // -------------------------------------------------- // // BSD // // -------------------------------------------------- // #if defined(Q_OS_BSD4) && !defined(Q_OS_DARWIN) #include <QFile> #include <QRegExp> static QString getFreeBSDDistro() { if (QFile::exists("/usr/PCBSD")) return "PCBSD"; return "FreeBSD"; } class QtSystemInfoBSD: public QtSystemInfoUnix { public: QtSystemInfoBSD() {} virtual QtSystemInfo::SystemType systemType() { # if defined(Q_OS_OPENBSD) return QtSystemInfo::OpenBSD; # elif defined(Q_OS_NETBSD) return QtSystemInfo::NetBSD; # elif defined(Q_OS_FREEBSD) return QtSystemInfo::FreeBSD; # else return QtSystemInfo::UnknownBSD; # endif } virtual QString systemName() { # if defined(Q_OS_OPENBSD) return "OpenBSD"; # elif defined(Q_OS_NETBSD) return "NetBSD"; # elif defined(Q_OS_FREEBSD) return getFreeBSDDistro(); # else return "BSD4"; # endif } virtual QString systemVersion() { QString version = kernelVersion(); QRegExp versionRx("^(\d+\.\d+(\.\d+)?)"); if (version.contains(versionRx)) version = versionRx.cap(1); return version; } }; #endif // -------------------------------------------------- // // Windows // // -------------------------------------------------- // #ifdef Q_OS_WIN #include <QSysInfo> class QtSystemInfoWindows: public QtSystemInfoPrivate { public: QtSystemInfoWindows() {} virtual QtSystemInfo::SystemType systemType() { # ifdef Q_OS_WINCE return QtSystemInfo::WindowsCE; # else return QtSystemInfo::Windows; # endif } virtual QtSystemInfo::ArchitectureType architecture() { # if defined(Q_OS_WINCE) return QtSystemInfo::UnknownArchitectureType; # elif defined(Q_OS_WIN64) return QtSystemInfo::X86_64; # else return QtSystemInfo::I386; # endif } virtual QString architectureName() { switch (architecture()) { case QtSystemInfo::X86_64: return "x86_64"; case QtSystemInfo::I386: return "i386"; default: return UNKNOWN; } } virtual QString kernelName() { if (QSysInfo::WindowsVersion & QSysInfo::WV_NT_based) return "NT"; else if (QSysInfo::WindowsVersion & QSysInfo::WV_CE_based) return "CE"; return UNKNOWN; } virtual QString kernelVersion() { switch (QSysInfo::WindowsVersion) { case QSysInfo::WV_4_0: return "4.0"; case QSysInfo::WV_5_0: return "5.0"; case QSysInfo::WV_5_1: return "5.1"; case QSysInfo::WV_5_2: return "5.2"; case QSysInfo::WV_6_0: return "6.0"; case QSysInfo::WV_6_1: return "6.1"; case QSysInfo::WV_CE: return "1.0"; case QSysInfo::WV_CENET: return ".NET"; case QSysInfo::WV_CE_5: return "5.x"; case QSysInfo::WV_CE_6: return "6.x"; default: return UNKNOWN; } } virtual QString systemName() { # ifdef Q_OS_WINCE return "Windows CE"; # else return "Windows"; # endif } virtual QString systemVersion() { switch (QSysInfo::WindowsVersion) { case QSysInfo::WV_NT: return "NT"; case QSysInfo::WV_2000: return "2000"; case QSysInfo::WV_XP: return "XP"; case QSysInfo::WV_2003: # ifdef Q_OS_WIN64 return "XP Proffessional 64"; # else return "2003 Server"; # endif case QSysInfo::WV_VISTA: return "Vista / Server 2008"; case QSysInfo::WV_WINDOWS7: return "7 / Server 2008 R2"; default: return UNKNOWN; } } }; #endif // -------------------------------------------------- // // Main class // // -------------------------------------------------- // QtSystemInfo::QtSystemInfo(QObject *parent): QObject(parent), #if defined(Q_OS_UNIX) # if defined(Q_OS_MAC) d(new QtSystemInfoMac()) # elif defined(Q_OS_LINUX) d(new QtSystemInfoLinux()) # elif defined(Q_OS_BSD4) d(new QtSystemInfoBSD) # else d(new QtSystemInfoUnix()) # endif #elif defined(Q_OS_WIN) d(new QtSystemInfoWindows()) #endif { } QtSystemInfo::~QtSystemInfo() { delete d; } QString QtSystemInfo::architectureName() const { return d->architectureName(); } QtSystemInfo::ArchitectureType QtSystemInfo::architecture() const { return d->architecture(); } QString QtSystemInfo::kernelVersion() const { return d->kernelVersion(); } QString QtSystemInfo::kernelName() const { return d->kernelName(); } QString QtSystemInfo::systemName() const { return d->systemName(); } QString QtSystemInfo::systemVersion() const { return d->systemVersion(); } QtSystemInfo::SystemType QtSystemInfo::systemType() const { return d->systemType(); } QString QtSystemInfo::getSystemInformation(QString format) { if (format.isEmpty()) format = "%SYS_NAME %SYS_VERSION (%KERN_NAME %KERN_VERSION %ARCH)."; format.replace("%SYS_NAME", systemName(), Qt::CaseInsensitive); format.replace("%SYS_VERSION", systemVersion(), Qt::CaseInsensitive); format.replace("%SYS_RELEASE", systemVersion(), Qt::CaseInsensitive); format.replace("%KERN_NAME", kernelName(), Qt::CaseInsensitive); format.replace("%KERN_RELEASE", kernelVersion(), Qt::CaseInsensitive); format.replace("%KERN_VERSION", kernelVersion(), Qt::CaseInsensitive); format.replace("%ARCH", architectureName(), Qt::CaseInsensitive); return format; } |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=135