初始源码,完成基本界面布局

This commit is contained in:
YuXifeng 2025-03-22 17:22:53 +08:00
commit 182542a65b
6 changed files with 520 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/build/
CMakeLists.txt.*
.DS*

72
CMakeLists.txt Normal file
View File

@ -0,0 +1,72 @@
cmake_minimum_required(VERSION 3.16)
project(Off-line-Tools VERSION 0.1 LANGUAGES CXX)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS SerialPort)
set(PROJECT_SOURCES
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(Off-line-Tools
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
# Define target properties for Android with Qt 6 as:
# set_property(TARGET Off-line-Tools APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
# ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
if(ANDROID)
add_library(Off-line-Tools SHARED
${PROJECT_SOURCES}
)
# Define properties for Android with Qt 5 after find_package() calls as:
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
else()
add_executable(Off-line-Tools
${PROJECT_SOURCES}
)
endif()
endif()
target_link_libraries(Off-line-Tools PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
target_link_libraries(Off-line-Tools PRIVATE Qt${QT_VERSION_MAJOR}::SerialPort)
# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
if(${QT_VERSION} VERSION_LESS 6.1.0)
set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.Off-line-Tools)
endif()
set_target_properties(Off-line-Tools PROPERTIES
${BUNDLE_ID_OPTION}
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)
include(GNUInstallDirs)
install(TARGETS Off-line-Tools
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(Off-line-Tools)
endif()

11
main.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

131
mainwindow.cpp Normal file
View File

@ -0,0 +1,131 @@
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QMessageBox>
#include <QDebug>
#include <QStandardItemModel>
#include <QStandardItem>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, serial(new QSerialPort(this)) // 在构造函数中初始化 serial
{
ui->setupUi(this);
// 连接串口的 readyRead 信号到槽函数
connect(serial, &QSerialPort::readyRead, this, &MainWindow::onReadyRead);
#ifdef Q_OS_WIN
qDebug() << "当前是 Windows 平台";
#elif defined(Q_OS_MAC)
qDebug() << "当前是 macOS 平台";
#elif defined(Q_OS_LINUX)
qDebug() << "当前是 Linux 平台";
#else
qDebug() << "未知平台";
#endif
QStringList portList;
const auto serialPortList = QSerialPortInfo::availablePorts();
for (const QSerialPortInfo &info : serialPortList) {
qDebug() << "Port Name:" << info.portName();
qDebug() << "Description:" << info.description();
qDebug() << "Manufacturer:" << info.manufacturer();
qDebug() << "Serial Number:" << info.serialNumber();
qDebug() << "System Location:" << info.systemLocation();
qDebug() << "-----------------------------";
if (info.manufacturer().compare("GenesysLogic") == 0 ||info.manufacturer().compare("Espressif") == 0) {
// 完全匹配
if(info.systemLocation().startsWith("/dev/tty."))
{
portList << info.systemLocation();
}
}
}
ui->comboBox->addItems(portList);
// 创建 QStandardItemModel(表格数据模型)
QStandardItemModel *model = new QStandardItemModel(3, 2);
// 设置表头标签
model->setHorizontalHeaderLabels({"Name", "value"});
// 插入数据
model->setItem(0, 0, new QStandardItem("SN"));
model->setItem(0, 1, new QStandardItem("123456789"));
model->setItem(1, 0, new QStandardItem("BLE_NAME"));
model->setItem(1, 1, new QStandardItem("G01-06"));
model->setItem(2, 0, new QStandardItem(""));
model->setItem(2, 1, new QStandardItem(""));
// 将模型与视图关联
ui->tableView->setModel(model);
//设置列宽
ui->tableView->setColumnWidth(0,100);
ui->tableView->setColumnWidth(1,160);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
// 获取用户在 comboBox 中选择的串口设备
QString selectedPort = ui->comboBox->currentText();
if (selectedPort.isEmpty()) {
QMessageBox::warning(this, "警告", "请选择一个串口设备");
return;
}
// 创建串口对象,如果已创建则先关闭
if (!serial) {
serial = new QSerialPort(this);
} else {
// 确保串口已打开才调用 close
if (serial->isOpen()) {
serial->close();
}
}
// 设置串口信息
serial->setPortName(selectedPort);
serial->setBaudRate(QSerialPort::Baud9600); // 设置波特率
serial->setDataBits(QSerialPort::Data8); // 数据位
serial->setParity(QSerialPort::NoParity); // 校验位
serial->setStopBits(QSerialPort::OneStop); // 停止位
serial->setFlowControl(QSerialPort::NoFlowControl); // 无流控
// 打开串口
if (serial->open(QIODevice::ReadWrite)) {
QMessageBox::information(this, "成功", "串口已成功打开");
// 这里可以执行更多的操作,比如发送数据、读取数据等
// 连接 readyRead 信号到槽函数
// connect(serial, &QSerialPort::readyRead, this, &MainWindow::onReadyRead);
// 示例:发送简单数据
QByteArray dataToSend = "Hello, Serial Port!\r\n";
serial->write(dataToSend);
} else {
QMessageBox::critical(this, "错误", "无法打开串口 " + selectedPort);
}
}
void MainWindow::onReadyRead()
{
while (serial->canReadLine()) {
QByteArray line = serial->readLine(); // 读取一行数据
qDebug() << "接收到一行数据: " << line;
// 处理数据
}
}

31
mainwindow.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSerialPort>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
void onReadyRead(); // 串口数据接收的槽
private:
Ui::MainWindow *ui;
QSerialPort *serial; // 将 serial 定义为成员对象
};
#endif // MAINWINDOW_H

272
mainwindow.ui Normal file
View File

@ -0,0 +1,272 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>838</width>
<height>648</height>
</rect>
</property>
<property name="windowTitle">
<string>ESP32系列脱机烧录</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>540</x>
<y>10</y>
<width>292</width>
<height>576</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>烧录器</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="sizeConstraint">
<enum>QLayout::SizeConstraint::SetDefaultConstraint</enum>
</property>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>COM:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<item>
<property name="text">
<string>/dev/tty0</string>
</property>
</item>
<item>
<property name="text">
<string>/dev/tty1</string>
</property>
</item>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>连接</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="sizeConstraint">
<enum>QLayout::SizeConstraint::SetMinAndMaxSize</enum>
</property>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="sizeConstraint">
<enum>QLayout::SizeConstraint::SetDefaultConstraint</enum>
</property>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>烧录器信息:</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButton_2">
<property name="text">
<string>一键复制</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QTextBrowser" name="textBrowser">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>30</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>设备日志:</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QTextBrowser" name="textBrowser_2"/>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QGroupBox" name="groupBox_3">
<property name="geometry">
<rect>
<x>10</x>
<y>430</y>
<width>511</width>
<height>151</height>
</rect>
</property>
<property name="title">
<string>镜像生成</string>
</property>
<widget class="QPushButton" name="pushButton_3">
<property name="geometry">
<rect>
<x>220</x>
<y>30</y>
<width>61</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>加载</string>
</property>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>10</x>
<y>30</y>
<width>61</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>文件:</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit">
<property name="geometry">
<rect>
<x>70</x>
<y>30</y>
<width>121</width>
<height>31</height>
</rect>
</property>
</widget>
</widget>
<widget class="QGroupBox" name="groupBox_4">
<property name="geometry">
<rect>
<x>0</x>
<y>10</y>
<width>521</width>
<height>411</height>
</rect>
</property>
<property name="title">
<string>固件选择</string>
</property>
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>20</y>
<width>501</width>
<height>41</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButton_4">
<property name="text">
<string>添加固件</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_5">
<property name="text">
<string>删除固件</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QTableView" name="tableView">
<property name="geometry">
<rect>
<x>10</x>
<y>80</y>
<width>511</width>
<height>311</height>
</rect>
</property>
</widget>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>838</width>
<height>24</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>