说明:此示例包含了基本的常使用的系统操作
效果如下:
mainwindos.cpp
#pragma execution_character_set("utf-8")
#include "mainwindow.h"#include <QDesktopWidget>
#include <QApplication>
#include <QHostInfo>
#include <QNetworkInterface>#include <QListView>
#include <QStringListModel>
#include <QProcess>
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent)
{QDesktopWidget *desktopWidget = QApplication::desktop();//1.1 获取屏幕分辨率QRect screenRect = desktopWidget->screenGeometry();int sWidth = screenRect.width();//屏幕宽度int sHeight = screenRect.height();QLabel *m_label00 = new QLabel("屏幕分辨率:");QString ip = (QString::number(sWidth,10)+"X"+QString::number(sHeight,10));QLabel *m_label01 = new QLabel(ip);//1.2 获取计算机名称QString localHostName = QHostInfo::localHostName();QLabel *m_label10 = new QLabel("本机名:");QLabel *m_label11 = new QLabel(localHostName); //1.3 获取计算机IPQLabel *m_label20 = new QLabel("IP:");QLabel *m_label21 = new QLabel();QHostInfo hostInfo = QHostInfo::fromName(localHostName);QList<QHostAddress> listAddress = hostInfo.addresses();// 创建一个 QList,类型 QHostAddressif(!listAddress.isEmpty()){m_label21->setText(listAddress.first().toString());}//1.5 默认取应用程序根目录QString strdirpath;strdirpath = qApp->applicationDirPath();//QLabel *m_label40 = new QLabel("applicationDirPath:");QLabel *m_label41 = new QLabel(strdirpath);//1.6 默认取应用程序可执行文件名称QString strapplicationFilePath;strapplicationFilePath = qApp->applicationFilePath();QLabel *m_label50 = new QLabel("applicationFilePath:");QLabel *m_label51 = new QLabel(strapplicationFilePath);//1.7 获取系统环境变量//标题QLabel *m_label30 = new QLabel("获取 Path 环境变量:");QListView *listView = new QListView();//实例 QListViewlistView->setGeometry(QRect(10,10,380,280));//QListView 位置QStringList strList = QProcess::systemEnvironment();//实例 QStringList 接收 path 系统变量QStringListModel *model = new QStringListModel(strList);//装载数据模型listView->setModel(model);//绑定数据//1.8执行系统命令//实例命令输入对话框comm = new QLineEdit();comm->setText("ipconfig");comm->setGeometry(QRect(20,20,260,25));//实例执行按钮btClick = new QPushButton(this);btClick->setText("执行");btClick->setGeometry(QRect(290,20,80,25));connect(btClick,SIGNAL(clicked()),this,SLOT(clickExecution()));//实例输出对话框outEdit = new QPlainTextEdit(this);outEdit->setGeometry(QRect(20,60,350,200));//实例 QProcessprocess = new QProcess;connect(process, SIGNAL(readyRead()), this, SLOT(readOutput()));//dos 命令查阅label = new QLabel(this);label->setGeometry(QRect(30,265,200,25));label->setText(tr("<a href=\"http://www.baidu.com/s?wd=dos 命令大全\">命令 DOS查阅</a>"));//开启超链接label->setOpenExternalLinks(true);QFrameshow1 = new QFrame();QFrameshow1->setFrameStyle(QFrame::Panel|QFrame::Sunken);//设置框架样式QGridLayout *childlayout1 = new QGridLayout();childlayout1->addWidget(m_label00,0,0,1,1);childlayout1->addWidget(m_label01,0,1,1,2);childlayout1->addWidget(m_label10,1,0);childlayout1->addWidget(m_label11,1,1);childlayout1->addWidget(m_label20,2,0);childlayout1->addWidget(m_label21,2,1);childlayout1->addWidget(m_label40,3,0);childlayout1->addWidget(m_label41,3,1);childlayout1->addWidget(m_label50,4,0);childlayout1->addWidget(m_label51,4,1);QVBoxLayout *childlayout2 = new QVBoxLayout;childlayout2->addWidget(m_label30);childlayout2->addWidget(listView);QGridLayout *childlayout3 = new QGridLayout();childlayout3->addWidget(comm);childlayout3->addWidget(btClick);childlayout3->addWidget(outEdit);//childlayout3->addWidget(process);childlayout3->addWidget(label);QVBoxLayout *mainlayout = new QVBoxLayout(QFrameshow1);mainlayout->addLayout(childlayout1);mainlayout->addLayout(childlayout2);mainlayout->addLayout(childlayout3);//显示布局控件QWidget *m_widget = new QWidget();m_widget->setLayout(mainlayout);this->setCentralWidget(m_widget);
}MainWindow::~MainWindow()
{}//执行 DOS 命令
void MainWindow::clickExecution()
{//定义变量接收 dos 命令QString info = comm->text();//执行命令process->start(info);//绑定反馈值outEdit->setPlainText(output);
}
//QProcess 事件
void MainWindow::readOutput()
{//接收反馈信息output +=process->readAll();//将返回值绑定控件outEdit->setPlainText(output);
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QFrame>
#include <QLabel>
#include <QWidget>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QPlainTextEdit>
#include <QProcess>
#include <QLabel>
class MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = 0);~MainWindow();QFrame *QFrameshow1;private:QLineEdit *comm; //dos 命令输入框QPlainTextEdit *outEdit; //命令执行反馈框QPushButton *btClick; //执行按钮QProcess *process; //QProcessQString output; //接收反馈信息QLabel *label;private slots:void clickExecution(); //点击按钮事件void readOutput(); //QProcess 事件};#endif // MAINWINDOW_H