欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 美景 > 7、Qt5开发及实列(笔记3-系统操作)

7、Qt5开发及实列(笔记3-系统操作)

2024/10/24 1:51:41 来源:https://blog.csdn.net/oDuanYanGuHong/article/details/140631013  浏览:    关键词:7、Qt5开发及实列(笔记3-系统操作)

说明:此示例包含了基本的常使用的系统操作

效果如下:
在这里插入图片描述

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com