添加项目文件。

This commit is contained in:
ldh
2026-11-06 14:45:09 +08:00
parent fb76f2ed7d
commit 24cbecc2cc
3437 changed files with 895900 additions and 0 deletions
+1605
View File
File diff suppressed because it is too large Load Diff
+28
View File
@@ -0,0 +1,28 @@
//
// Created by Administrator on 2026/1/21.
//
#ifndef CLICKABLE_LINE_EDIT_H
#define CLICKABLE_LINE_EDIT_H
#include <QLineEdit>
#include <QMouseEvent>
class ClickableLineEdit : public QLineEdit {
Q_OBJECT
public:
ClickableLineEdit(QWidget *parent = nullptr) : QLineEdit(parent) {}
signals:
void clicked();
protected:
void mousePressEvent(QMouseEvent *event) override {
QLineEdit::mousePressEvent(event);
if (event->button() == Qt::LeftButton) {
emit clicked(); // 发射自定义信号
}
}
};
#endif //CLICKABLE_LINE_EDIT_H
+135
View File
@@ -0,0 +1,135 @@
#include "crc16util.h"
#include <iostream>
#include <cstdint>
#include <string>
#include <sstream>
#include <vector>
#include <iomanip>
#include <cctype>
Crc16Util::Crc16Util()
{
}
// 计算Modbus CRC16校验值
uint16_t calculateCRC16(const uint8_t* data, size_t length) {
uint16_t crc = 0xFFFF;
for (size_t i = 0; i < length; ++i) {
crc ^= static_cast<uint16_t>(data[i]);
for (size_t j = 0; j < 8; ++j) {
if ((crc & 0x0001) != 0) {
crc >>= 1;
crc ^= 0xA001;
}
else {
crc >>= 1;
}
}
}
return crc;
}
uint8_t hexToUint8(const std::string& hexStr) {
// 检查字符串长度
if (hexStr.length() < 2) {
return 0;
}
// 检查字符是否有效
char c1 = hexStr[0];
char c2 = hexStr[1];
// 转换为大写并验证是否为有效的十六进制字符
c1 = toupper(c1);
c2 = toupper(c2);
if (!((c1 >= '0' && c1 <= '9') || (c1 >= 'A' && c1 <= 'F')) ||
!((c2 >= '0' && c2 <= '9') || (c2 >= 'A' && c2 <= 'F'))) {
return 0;
}
uint8_t highNibble = (c1 >= 'A') ? (c1 - 'A' + 10) : (c1 - '0');
uint8_t lowNibble = (c2 >= 'A') ? (c2 - 'A' + 10) : (c2 - '0');
return (highNibble << 4) | lowNibble;
}
std::vector<uint8_t> splitAndConvertToHex(const std::string& input) {
std::vector<uint8_t> result;
// 检查输入字符串是否为空
if (input.empty()) {
return result;
}
std::stringstream ss(input);
std::string token;
while (ss >> token) {
// 检查token是否有效(至少2个字符)
if (token.length() >= 2) {
try {
uint8_t value = hexToUint8(token);
result.push_back(value);
} catch (...) {
// 如果转换失败,跳过这个token
continue;
}
}
}
return result;
}
std::string Crc16Util::getCRC16(std::string cmdHex){
// 添加空字符串检查
if (cmdHex.empty()) {
return "0000";
}
std::vector<uint8_t> hexArray = splitAndConvertToHex(cmdHex);
// 检查转换后的数组是否为空
if (hexArray.empty()) {
return "0000";
}
uint8_t* data = hexArray.data();
size_t size = hexArray.size();
// 计算CRC16校验值
uint16_t crc = calculateCRC16(data, size);
// 交换高低字节
uint16_t swappedCRC = ((crc & 0xFF) << 8) | ((crc & 0xFF00) >> 8);
std::stringstream ss;
ss << std::hex << std::setw(4) << std::setfill('0') << swappedCRC;
// 确保返回的字符串不为空
std::string result = ss.str();
if (result.empty()) {
return "0000";
}
return result;
// uint8_t data[] = { 0x5A, 0x01, 0x01, 0xA1, 0x01 };
// // 计算CRC16校验值
// uint16_t crc = calculateCRC16(data, sizeof(data));
// // 交换高低字节
// uint16_t swappedCRC = ((crc & 0xFF) << 8) | ((crc & 0xFF00) >> 8);
// // 输出校验值
// std::cout << "CRC16: 0x" << std::hex << swappedCRC << std::endl;
// return "";
}
+13
View File
@@ -0,0 +1,13 @@
#ifndef CRC16UTIL_H
#define CRC16UTIL_H
#include <string>
using namespace std;
class Crc16Util
{
public:
Crc16Util();
static string getCRC16(string cmdHex);
};
#endif // CRC16UTIL_H
+439
View File
@@ -0,0 +1,439 @@
#include <QDebug>
#include "sqliteutil.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QList>
#include <QVariant>
#include <QSqlQueryModel>
#include <QSqlRecord>
#include <QSqlError>
#include <QApplication>
#include <QDir>
QSqlDatabase SqliteUtil::m_db ;
QString SqliteUtil::m_strDatabase = "SQL/1208_02_jixiebi.db";
SqliteUtil::SqliteUtil()
{
}
bool SqliteUtil::createConnection(const QString &database)
{
//与数据库建立连接
if (QSqlDatabase::contains("qt_sql_default_connection"))
{
m_db = QSqlDatabase::database("qt_sql_default_connection");
}
else
{
// 修复:使用一致的连接名称
m_db = QSqlDatabase::addDatabase("QSQLITE", "qt_sql_default_connection");
}
//设置数据库名
m_db.setDatabaseName(database);
//打开数据库
if (!m_db.open())
{
qDebug() << "打开数据库失败!" << m_db.lastError().text();
return false;
}
qDebug() << "数据库连接成功:" << database;
return true;
}
bool SqliteUtil::ExecuteRecord(const QString& strQuery)
{
if(!m_db.isOpen())
{
if(!createConnection(m_strDatabase))
{
qDebug() << "在ExecuteRecord中创建数据库连接失败!" << m_db.lastError().text();
return false;
}
}
QSqlQuery query(m_db);
query.prepare(strQuery);
if(query.exec()){
return true;
}
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
return false;
}
QSqlRecord SqliteUtil::ExecuteRecord(const QString& strQuery, QList<QVariant> lstParameter)
{
if(!m_db.isOpen())
{
if(!createConnection(m_strDatabase))
{
qDebug() << "在ExecuteRecord中创建数据库连接失败!" << m_db.lastError().text();
return QSqlRecord();
}
}
QSqlQuery query(m_db);
query.prepare(strQuery);
for(int i = 0; i < lstParameter.size(); i++)
{
query.bindValue(i, lstParameter.at(i));
}
if(query.exec())
{
if(query.next())
{
return query.record();
}
qDebug() << "查询执行失败!未找到记录!" << strQuery;
return QSqlRecord();
}
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
return QSqlRecord();
}
QSqlRecord SqliteUtil::ExecuteRecord(const QString& strQuery, QVariant Parameter)
{
if(!m_db.isOpen())
{
if(!createConnection(m_strDatabase))
{
qDebug() << "在ExecuteRecord中创建数据库连接失败!" << m_db.lastError().text();
return QSqlRecord();
}
}
QSqlQuery query(m_db);
query.prepare(strQuery);
query.bindValue(0, Parameter);
if(query.exec())
{
if(query.next())
{
return query.record();
}
qDebug() << "查询执行失败!未找到记录!" << strQuery;
return QSqlRecord();
}
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
return QSqlRecord();
}
QSqlQuery SqliteUtil::ExecuteSqlQuery(const QString& strQuery, QList<QVariant> lstParameter)
{
if(!m_db.isOpen())
{
if(!createConnection(m_strDatabase))
{
qDebug() << "在ExecuteSqlQuery中创建数据库连接失败!" << m_db.lastError().text();
return QSqlQuery();
}
}
QSqlQuery query(m_db);
query.prepare(strQuery);
query.setForwardOnly(true);
for(int i = 0; i < lstParameter.size(); i++)
{
query.bindValue(i, lstParameter.at(i));
}
if(!query.exec())
{
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
}
return query;
}
QSqlQuery SqliteUtil::ExecuteSqlQuery(const QString& strQuery, QVariant Parameter)
{
if(!m_db.isOpen())
{
if(!createConnection(m_strDatabase))
{
qDebug() << "在ExecuteSqlQuery中创建数据库连接失败!" << m_db.lastError().text();
return QSqlQuery();
}
}
QSqlQuery query(m_db);
query.setForwardOnly(true);
query.prepare(strQuery);
query.bindValue(0, Parameter);
if(!query.exec())
{
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
}
return query;
}
QSqlQuery SqliteUtil::ExecuteSqlQuery(const QString& strQuery)
{
if(!m_db.isOpen())
{
if(!createConnection(m_strDatabase))
{
qDebug() << "在ExecuteSqlQuery中创建数据库连接失败!" << m_db.lastError().text();
return QSqlQuery();
}
}
QSqlQuery query(m_db);
query.prepare(strQuery);
query.setForwardOnly(true);
if(!query.exec())
{
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
}
return query;
}
int SqliteUtil::ExecuteInt(const QString& strQuery)
{
if(!m_db.isOpen())
{
if(!createConnection(m_strDatabase))
{
qDebug() << "在ExecuteInt中创建数据库连接失败!" << m_db.lastError().text();
return -1;
}
}
QSqlQuery query(m_db);
query.prepare(strQuery);
if(query.exec())
{
if(query.next())
{
return query.value(0).toInt();
}
qDebug() << "查询执行失败!未找到记录!" << strQuery;
return -1;
}
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
return -1;
}
int SqliteUtil::ExecuteInt(const QString& strQuery, QList<QVariant> lstParameter)
{
if(!m_db.isOpen())
{
if(!createConnection(m_strDatabase))
{
qDebug() << "在ExecuteInt中创建数据库连接失败!" << m_db.lastError().text();
return -1;
}
}
QSqlQuery query(m_db);
query.prepare(strQuery);
for(int i = 0; i < lstParameter.size(); i++)
{
query.bindValue(i, lstParameter.at(i));
}
if(query.exec())
{
if(query.next())
{
return query.value(0).toInt();
}
qDebug() << "查询执行失败!未找到记录!" << strQuery;
return -1;
}
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
return -1;
}
int SqliteUtil::ExecuteInt(const QString& strQuery, QVariant Parameter)
{
if(!m_db.isOpen())
{
if(!createConnection(m_strDatabase))
{
qDebug() << "在ExecuteInt中创建数据库连接失败!" << m_db.lastError().text();
return -1;
}
}
QSqlQuery query(m_db);
query.prepare(strQuery);
query.bindValue(0, Parameter);
if(query.exec())
{
if(query.next())
{
return query.value(0).toInt();
}
qDebug() << "查询执行失败!未找到记录!" << strQuery;
return -1;
}
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
return -1;
}
bool SqliteUtil::Execute(const QString& strQuery)
{
if(!m_db.isOpen())
{
if(!createConnection(m_strDatabase))
{
qDebug() << "Failed to create database connection in Execute";
return false;
}
}
QSqlQuery query(m_db);
if(query.exec(strQuery))
{
return true;
}
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
return false;
}
bool SqliteUtil::Execute(const QString& strQuery, QVariant Parameter)
{
if(!m_db.isOpen())
{
if(!createConnection(m_strDatabase))
{
qDebug() << "在Execute中创建数据库连接失败!" << m_db.lastError().text();
return false;
}
}
QSqlQuery query(m_db);
query.prepare(strQuery);
query.bindValue(0, Parameter);
if(query.exec())
{
return true;
}
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
return false;
}
bool SqliteUtil::Execute(const QString& strQuery, QList<QVariant> lstParameter)
{
if(!m_db.isOpen())
{
if(!createConnection(m_strDatabase))
{
qDebug() << "在Execute中创建数据库连接失败!" << m_db.lastError().text();
return false;
}
}
QSqlQuery query(m_db);
query.prepare(strQuery);
for(int i = 0; i < lstParameter.size(); i++)
{
query.bindValue(i, lstParameter.at(i));
}
if(query.exec())
{
return true;
}
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
return false;
}
QString SqliteUtil::ExecuteString(const QString& strQuery)
{
if(!m_db.isOpen())
{
if(!createConnection(m_strDatabase))
{
qDebug() << "在ExecuteString中创建数据库连接失败!" << m_db.lastError().text();
return QString();
}
}
QSqlQuery query(m_db);
query.prepare(strQuery);
if(query.exec())
{
if(query.next())
{
return query.value(0).toString();
}
qDebug() << "查询执行失败!未找到记录!" << strQuery;
return QString();
}
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
return QString();
}
void SqliteUtil::ExecuteQueryModel(QSqlQueryModel *p_queryModel, const QString& strQuery)
{
if(!m_db.isOpen())
{
if(!createConnection(m_strDatabase))
{
qDebug() << "在ExecuteQueryModel中创建数据库连接失败!" << m_db.lastError().text();
return;
}
}
if(p_queryModel == nullptr)
{
qDebug() << "在ExecuteQueryModel中查询模型指针为空!" << strQuery;
return;
}
p_queryModel->setQuery(strQuery, m_db);
if(p_queryModel->lastError().isValid())
{
qDebug() << "在ExecuteQueryModel中查询模型执行失败!" << p_queryModel->lastError().text() << "SQL:" << strQuery;
}
}
void SqliteUtil::ExecuteQueryModel(QSqlQueryModel *p_queryModel, const QString& strQuery,
QList<QVariant> lstParameter)
{
if(!m_db.isOpen())
{
if(!createConnection(m_strDatabase))
{
qDebug() << "在ExecuteQueryModel中创建数据库连接失败!" << m_db.lastError().text();
return;
}
}
if(p_queryModel == nullptr)
{
qDebug() << "在ExecuteQueryModel中查询模型指针为空!" << strQuery;
return;
}
QSqlQuery query(m_db);
query.prepare(strQuery);
for(int i = 0; i < lstParameter.size(); i++)
{
query.bindValue(i, lstParameter.at(i));
}
if(query.exec())
{
p_queryModel->setQuery(query);
if(p_queryModel->lastError().isValid())
{
qDebug() << "在ExecuteQueryModel中查询模型执行失败!" << p_queryModel->lastError().text() << "SQL:" << strQuery;
}
}
else
{
qDebug() << "在ExecuteQueryModel中查询模型执行失败!" << query.lastError().text() << "SQL:" << strQuery;
}
}
void SqliteUtil::ExecuteQueryModel(QSqlQueryModel *p_queryModel, const QString& strQuery, QVariant Parameter)
{
if(!m_db.isOpen())
{
if(!createConnection(m_strDatabase))
{
qDebug() << "在ExecuteQueryModel中创建数据库连接失败!" << m_db.lastError().text();
return;
}
}
if(p_queryModel == nullptr)
{
qDebug() << "在ExecuteQueryModel中查询模型指针为空!" << strQuery;
return;
}
QSqlQuery query(m_db);
query.prepare(strQuery);
query.bindValue(0, Parameter);
if(query.exec())
{
p_queryModel->setQuery(query);
if(p_queryModel->lastError().isValid())
{
qDebug() << "在ExecuteQueryModel中查询模型执行失败!" << p_queryModel->lastError().text() << "SQL:" << strQuery;
}
} else {
qDebug() << "在ExecuteQueryModel中查询模型执行失败!" << query.lastError().text() << "SQL:" << strQuery;
}
}
+52
View File
@@ -0,0 +1,52 @@
#ifndef SQLITEUTIL_H
#define SQLITEUTIL_H
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QList>
#include <QVariant>
#include <QSqlQueryModel>
class SqliteUtil
{
public:
SqliteUtil();
static bool createConnection(const QString& strConn);
static bool ExecuteRecord(const QString& strQuery);
static QSqlRecord ExecuteRecord(const QString& strQuery, QList<QVariant> lstParameter);
static QSqlRecord ExecuteRecord(const QString& strQuery, QVariant Parameter);
static QSqlQuery ExecuteSqlQuery(const QString& strQuery, QList<QVariant> lstParameter);
static QSqlQuery ExecuteSqlQuery(const QString& strQuery, QVariant Parameter);
static QSqlQuery ExecuteSqlQuery(const QString& strQuery);
static int ExecuteInt(const QString& strQuery);
static int ExecuteInt(const QString& strQuery, QList<QVariant> lstParameter);
static int ExecuteInt(const QString& strQuery, QVariant Parameter);
static bool Execute(const QString& strQuery);
static bool Execute(const QString& strQuery, QVariant Parameter);
static bool Execute(const QString& strQuery, QList<QVariant> lstParameter);
static QString ExecuteString(const QString& strQuery);
static void ExecuteQueryModel(QSqlQueryModel *p_queryModel, const QString& strQuery);
static void ExecuteQueryModel(QSqlQueryModel *p_queryModel, const QString& strQuery,
QList<QVariant> lstParameter);
static void ExecuteQueryModel(QSqlQueryModel *p_queryModel, const QString& strQuery, QVariant Parameter);
private:
static QSqlDatabase m_db;
static QString m_strDatabase;
};
#endif // SQLITEUTIL_H
+21
View File
@@ -0,0 +1,21 @@
#ifndef TYPE_H_
#define TYPE_H_
typedef signed char int8;
typedef unsigned char uint8;
typedef signed short int16;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef signed int int32;
typedef struct {
double start;
double end;
}Interval;
typedef struct {
int iIndex;
int iStart;
}HandleIndex;
#define MAX_NR_DEVICES 2
#define MAX_DATA_LEN 2048
#endif
+124
View File
@@ -0,0 +1,124 @@
#include "util.h"
#include <QMessageBox>
#include <QWidget>
#include <QTime>
#include <QEventLoop>
#include <QCoreApplication>
#include <sstream>
#include <cmath>
#include <QBuffer>
#include <QFile>
#include <qrandom.h>
#include <QTextStream>
using namespace std;
Util::Util()
{
}
void Util::WriteLog(string logInfo)
{
Util::Assert(false);
}
void Util::MessageBox(QWidget *parentWnd, std::string msg, std::string title /*= "信息"*/)
{
QMessageBox::information(parentWnd, ToQstr(title), ToQstr(msg), QMessageBox::Ok, QMessageBox::NoButton);
}
QString Util::ToQstr(std::string str)
{
return QString::fromStdString(str);
}
string Util::ToStdStr(QString str /*= "默认的参数:你好2 helloworld *&……%¥#@!"*/)
{
return string(str.toLocal8Bit());
}
bool Util::IsEqual(double a, double b) //比较两个浮点数使否相等
{
return fabs(a - b) <= EPSLON;
}
void Util::delay(int ms) //延迟指定的ms毫秒数
{
QTime dieTime = QTime::currentTime().addMSecs(ms);
while (QTime::currentTime() < dieTime)
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
QString Util::GetId()
{
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
QDateTime currentTime = QDateTime::currentDateTime();
QString currentTimeStr = currentTime.toString("yyyyMMddhhmmss");
int n = qrand() % 10;
//QString random = QString("%1").arg(n,6,10,QLatin1Char('0'));
return currentTimeStr + QString::number(n);
}
//i要转化的十进制整数,width转化后的宽度,位数不足则补0
std::string Util::Dec2Hex(int i, int width)
{
std::stringstream ioss; //定义字符串流
std::string s_temp; //存放转化后字符
ioss << std::hex << i; //以十六制形式输出
ioss >> s_temp;
if(width > s_temp.size())
{
std::string s_0(width - s_temp.size(), '0'); //位数不够则补0
s_temp = s_0 + s_temp; //合并
}
std::string s = s_temp.substr(s_temp.length() - width, s_temp.length()); //取右width位
transform(s.begin(), s.end(), s.begin(), ::toupper);
return s;
}
QString Util::Img2base64(QImage& image){
// 将 QImage 写入 QByteArray
QByteArray byteArray;
QBuffer buffer(&byteArray);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "JPEG"); // 保存为 PNG 格式,您也可以选择其他格式
buffer.close();
// 将 QByteArray 转换为 Base64 编码的字符串
QString base64Str = byteArray.toBase64();
return base64Str;
}
std::string Util::Byte2Hex(unsigned char* bytes, int bytelength)
{
string str("");
string str2("0123456789abcdef");
for (int i=0;i<bytelength;i++) {
int b;
b = 0x0f&(bytes[i]>>4);
char s1 = str2.at(b);
str.append(1,str2.at(b));
b = 0x0f & bytes[i];
str.append(1,str2.at(b));
char s2 = str2.at(b);
}
return str;
}
// 生成11位随机数
qint64 Util::generate11DigitRandomNumber() {
qint64 min = 10000000000LL; // 11位最小值
qint64 max = 99999999999LL; // 11位最大值
qint64 range = max - min + 1;
qint64 randNum = QRandomGenerator::global()->generate64() % range + min;
return randNum;
}
+56
View File
@@ -0,0 +1,56 @@
#ifndef UTIL_H
#define UTIL_H
#include <string>
#include <QString>
#include <vector>
#include <QImage>
#define ARM_RUN true /* shi fou zai ARM shang yun xing*/
#if (ARM_RUN)
#include <assert.h>
#define ASSERT(x) assert(x)
#else
#define ASSERT(x) _ASSERT(x)
#endif
#define EPSLON 1E-6
#define COUNT_ARRAY(Array) (sizeof(Array)/sizeof(Array[0])) /*计算数组长度*/
#define DEL_PTR_S(pData) { try { if (pData) { delete (pData); (pData) = NULL; } } catch (...) { ASSERT(false); } ASSERT(NULL ==(pData)); } /* 安全删除指针 */
#define DEL_ARRAY_S(pData) { try { if (pData) { delete[] (pData); (pData) = NULL;} } catch (...) { ASSERT(false); } ASSERT(NULL ==(pData)); } /* 安全删除数组[] */
#define STR2SZ(str) ((LPSTR)(LPCTSTR)(str)) /* CStringתΪchar* */
typedef bool (*OnReceiveDataCallBackFunc)(int nDeviceIndex, double* pSpectrum, int nLen);
using namespace std;
class QWidget;
class Util
{
public:
Util();
static void Assert(bool bExprState) { ASSERT(bExprState); } //Assert断言
static void WriteLog(std::string logInfo); //写入日志
static void MessageBox(QWidget *parentWnd, std::string msg, std::string title = "信息"); //弹出模态消息框
static QString ToQstr(std::string str); //std::string 转 QString
static std::string ToStdStr(QString str = "默认的参数:你好2 helloworld *&……%¥#@!"); //QString 转 std::string
static bool IsEqual(double a, double b); //比较两个浮点数使否相等
static void delay(int ms); //延迟指定的ms毫秒数
static QString GetId();
static std::string Dec2Hex(int num, int width);
static std::string Byte2Hex(unsigned char* bytes, int byteLength);
static QString Img2base64(QImage& image);
static qint64 generate11DigitRandomNumber();
};
#endif // UTIL_H