QT C++ 自学积累 『非技术文』

ops/2024/10/20 16:14:45/

QT C++ 自学积累 『非技术文』

最近一段时间参与了一个 QT 项目的开发,使用的是 C++ 语法,很遗憾的是我之前从来没有接触过 C++ ,大学没有开过这堂课,也没用自己学习过,所有说上手贼慢,到现在为止其实也不是很清楚具体的开发技巧,毕竟是参与,东一复制西一粘贴的,就拉倒了。里面用到了很多东西,尽管很简单很简单,但是对于没有接触过的人来说还是很值得记录一下的,这篇博文只是自己学习记录,没啥营养,浅看则以,切勿尽信!对了,用的 QT5 哈,其他版本的不知道嗷!

在这里插入图片描述

QDebug 打印

在项目开发过程中难免遇到打印调试数据,打印数据很简单,引入 QDebug,然后就可以使用了:

#include <QDebug>qDebug() << "hello, I'm + V";

看一下效果:

在这里插入图片描述

开启弹窗 Dialog

这玩意儿,首先得有弹窗文件,有弹窗文件就好说了,直接调用一下让他弹出来就行,比如做了一个叫做 GPUDialog 的弹窗:

#include "GPUDialog.h"  // 引入弹窗文件GPUDialog gpuDialog;  // 实例化一个弹窗
gpuDialog.exec();   // 打开弹窗

写入配置文件

在QT里面嘞,有些配置数据可能需要写入配置文件,然后怎么写呢,用 QSetting:

#include <QSettings>// 创建 QSettings
QSettings* setting = new QSettings("./config.ini", QSettings::IniFormat);
setting->setValue("ed/name", "+V");  // 写入配置
setting->setValue("ed/age", 18);  // 写入配置QString name = setting->value("ed/name").toString(); // 读取配置
QString age = setting->value("ed/age").toString(); // 读取配置
qDebug() << name << age;

看一下打印结果:

在这里插入图片描述

当然配置文件内容也可以看一下,是这个样子的:

在这里插入图片描述

弹出警示框

比如说,我们点击一个表单的提交按钮,如果没有输入表单数据,就需要提示用户清闲输入内容。

#include <QMessageBox>QString warningTitle(tr("Data is empty"));
QMessageBox::warning(this, warningTitle, tr("Please enter the data first and try again!"), QMessageBox::Ok);

在这里插入图片描述

弹出确认框

这个和上面的是差不多的,比如我们需要点击一个 “运行” 按钮,需要二次确认的时候,经常用到这种弹窗:

#include <QMessageBox>QMessageBox::StandardButton response = QMessageBox::question(nullptr, "Kill Exe Confirm", "Secondary Confirmation Dialogue Box Demonstration", QMessageBox::Yes | QMessageBox::No);
if (response == QMessageBox::Yes)
{qDebug() << "Yes";
} else {qDebug() << "No";
}

看一下效果:

在这里插入图片描述

然后看一下控制台打印的数据:

在这里插入图片描述

关闭第三方 exe 程序

比如说我们写一个程序,在程序需要执行的时候,需要关闭掉其他应用程序,就像是某些付费视频,配套专用的播放器,启动播放器的时候,他会把你电脑启动的截图、录屏插件全部强制杀死,就是一样的功能,不如下面案例,杀死 PixPin.exe 程序:

#include <QProcess>QString progress = "taskkill /im PixPin.exe /f";
QProcess::execute(progress);

创建文件夹

创建文件夹就肯简单了,两行命令完成:

#include <QDir>  // 引入库QDir dir;
dir.mkpath("./wjw");  // 创建文件夹

看一下,在当前exe同级目录下就会出现我们创建的文件夹:

在这里插入图片描述

创建文件

创建文件和创建文件夹功能类似,但是代码有些区别:

#include <QDir>  // 引入库QDir dirPath("./");
QString filePathStr = dirPath.filePath("wjw.txt");
QFile file(filePathStr);
if (!file.open(QIODevice::WriteOnly)) { return; }
file.close();

嘿嘿,再看一下结果,这个文件出来啦 :

在这里插入图片描述

输入框输入格式校验

// 实现文本框只允许输入float类型
#include <QDoubleValidator>QDoubleValidator* validator = new QDoubleValidator(this);
validator->setNotation(QDoubleValidator::StandardNotation);
this->ui->lineEdit->setValidator(validator);// 实现文本框只允许输入int类型
#include <QIntValidator>QIntValidator* validatorInt = new QIntValidator(this);
this->ui->lineEdit_2->setValidator(validatorInt);

获取网络日期

我们有的时候需要获取网络时间,这个时候就用下面的方法:

#include <QDateTime>
#include <QTcpSocket>
#include <QDate>// 获取网络时间,如果没有获取到,则获取系统时间
QString Widget::getNetTime() {QStringList urls;urls << "time-b-g.nist.gov"<< "time-c-g.nist.gov"<< "time-d-g.nist.gov"<< "time-e-g.nist.gov"<< "time-a-wwv.nist.gov"<< "time-b-wwv.nist.gov"<< "time-c-wwv.nist.gov"<< "time-d-wwv.nist.gov"<< "time-e-wwv.nist.gov"<< "time-a-b.nist.gov"<< "time-b-b.nist.gov"<< "time-c-b.nist.gov"<< "time-d-b.nist.gov"<< "time-e-b.nist.gov"<< "time.nist.gov"<< "utcnist.colorado.edu"<< "utcnist2.colorado.edu";bool isFind = false;QString netTime    = "";QTcpSocket *socket = new QTcpSocket();for (int i = 0; i < urls.size(); i++){socket->connectToHost(urls.at(i), 13);if (socket->waitForConnected()){if (socket->waitForReadyRead()){QString str(socket->readAll());netTime = str.trimmed();netTime = str.section(" ", 1, 2);isFind = true;break;}}socket->close();}if(isFind){QDateTime utcDateTime = QDateTime::fromString(netTime, "yy-MM-dd HH:mm:ss");utcDateTime.setTimeSpec(Qt::UTC);netTime = utcDateTime.toLocalTime().toString("yy-MM-dd");} else {QDateTime currentDateTime = QDateTime::currentDateTime();netTime = currentDateTime.toString("yy-MM-dd");}delete socket;return netTime;
}

获取 CPU 序列号

#include <QProcess>
#include <windows.h>
#include <QStringList>// 获取CPU序列号
QString Widget::getCpuId()
{QProcess p;QString cmd = "wmic cpu get processorid";p.start(cmd);p.waitForFinished();QString result = QString::fromLocal8Bit(p.readAllStandardOutput());QStringList list = cmd.split(" ");result = result.remove(list.last(), Qt::CaseInsensitive);result = result.replace("\r", "");result = result.replace("\n", "");result = result.simplified();return result ;
}

获取系统硬盘信息

#include <QProcess>
#include <windows.h>
#include <QStringList>// 获取硬盘信息
QString Widget::getDiskID()
{QProcess p;QString cmd = "wmic diskdrive get model";p.start(cmd);p.waitForFinished();QString result = QString::fromLocal8Bit(p.readAllStandardOutput());QStringList list = cmd.split(" ");result = result.remove(list.last(), Qt::CaseInsensitive);result = result.replace("\r", "");result = result.replace("\n", "");result = result.simplified();return result ;
}

获取 MAC 编码

#include <QNetworkInterface>// 获取MAC编码
QString Widget::GetMacByNetworkInterface() {QList<QNetworkInterface> NetList;//网卡链表int NetCount = 0;//网卡个数int Neti = 0;QNetworkInterface thisNet;//所要使用的网卡NetList = QNetworkInterface::allInterfaces();//获取所有网卡信息NetCount = NetList.count();//统计网卡个数for (Neti = 0; Neti < NetCount; Neti++) {//遍历所有网卡if (NetList[Neti].isValid()) {//判断该网卡是否是合法thisNet = NetList[Neti];//将该网卡置为当前网卡break;}}return thisNet.hardwareAddress().replace(":", "-"); //获取该网卡的MAC
}

暂时没有了~


http://www.ppmy.cn/ops/119801.html

相关文章

使用FFmpeg压缩MP3格式音频

FFmpeg简介 FFmpeg 是一个开源的多媒体框架&#xff0c;能够录制、转换数字音频和视频&#xff0c;并将其转码到流行的格式。它被广泛应用于音视频处理领域&#xff0c;支持几乎所有的音视频格式和编解码器。以下是 FFmpeg 的一些关键特点和功能&#xff1a; 主要特点 跨平台…

【Android 14源码分析】WMS-窗口显示-流程概览与应用端流程分析

忽然有一天&#xff0c;我想要做一件事&#xff1a;去代码中去验证那些曾经被“灌输”的理论。                                                                                  – 服装…

“图像识别技术:重塑生活与工作的未来”

“图像识别技术&#xff1a;重塑生活与工作的未来”这个标题非常具有吸引力和前瞻性。它突出了图像识别技术在现代社会中的重要地位&#xff0c;以及它如何深刻地影响我们的日常生活和工作方式。以下是对这个标题的一些解读&#xff1a; 重塑生活&#xff1a; 图像识别技术正在…

计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-09-30

计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-09-30 目录 文章目录 计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-09-30目录1. Proof Automation with Large Language Models概览&#xff1a;论文研究背景&#xff1a;技术挑战&#xff1a;如何破局…

★ C++进阶篇 ★ map和set

Ciallo&#xff5e;(∠・ω< )⌒☆ ~ 今天&#xff0c;我将继续和大家一起学习C进阶篇第四章----map和set ~ ❄️❄️❄️❄️❄️❄️❄️❄️❄️❄️❄️❄️❄️❄️ 澄岚主页&#xff1a;椎名澄嵐-CSDN博客 C基础篇专栏&#xff1a;★ C基础篇 ★_椎名澄嵐的博客-CSDN博…

ireport 5.1 中文生辟字显示不出来,生成PDF报字体找不到

ireport生成pdf里文字不显示。本文以宋体中文字不显示为例。 问题&#xff1a;由浅入深一步一步分析 问题1、预览正常&#xff0c;但生成pdf中文不显示 报告模板编辑后&#xff0c;预览正常&#xff0c;但生成pdf中文不显示。以下是试验过程&#xff1a; 先编辑好一个报告单模…

基于微信小程序的宿舍报修系统的设计与实现(lw+演示+源码+运行)

摘 要 互联网发展至今&#xff0c;无论是其理论还是技术都已经成熟&#xff0c;而且它广泛参与在社会中的方方面面。它让信息都可以通过网络传播&#xff0c;搭配信息管理工具可以很好地为人们提供服务。针对成果信息管理混乱&#xff0c;出错率高&#xff0c;信息安全性差&am…

10款好用的开源 HarmonyOS 工具库

大家好&#xff0c;我是 V 哥&#xff0c;今天给大家分享10款好用的 HarmonyOS的工具库&#xff0c;在开发鸿蒙应用时可以用下&#xff0c;好用的工具可以简化代码&#xff0c;让你写出优雅的应用来。废话不多说&#xff0c;马上开整。 1. efTool efTool是一个功能丰富且易用…