v1.1: 修复 Windows 跨平台兼容性 & 按钮高对比度配色

【跨平台修复】
- cmakegenerator.cpp: CMAKE_C_COMPILER / CMAKE_MAKE_PROGRAM 提前到 project() 之前设置,避免 CMake 在 Windows 回退 NMake
- cmakegenerator.cpp: MinGW 编译器路径下自动推导 mingw32-make.exe
- mainwindow.cpp: generateCMake() 在 Windows 下添加 -G 'MinGW Makefiles'
- mainwindow.cpp: compileProject() cmake 添加生成器参数 & mingw32-make 替代 make

【UI 改进】
- 全部操作按钮升级为 Material Design 高对比度配色(Darker 系列)
- 按钮添加圆角和内边距,提升可读性和视觉层次

【工程规范】
- 新增 .gitignore,排除编译产物 (.o / moc_*.cpp / Makefile / 二进制)
- README.md 重写,补充 Windows 编译指南和技术要点
- 需求规格说明书更新至 v1.1
This commit is contained in:
虾哥
2026-04-28 18:26:10 +08:00
parent 2ab1924d1d
commit 1f7328174b
17 changed files with 150 additions and 1020 deletions

View File

@@ -1,5 +1,6 @@
#include "cmakegenerator.h"
#include <QFile>
#include <QFileInfo>
#include <QTextStream>
#include <QDir>
#include <QDateTime>
@@ -93,7 +94,36 @@ QString CMakeGenerator::generateProjectConfig(const ProjectConfig *config)
projectName = "UntitledProject";
}
content += "# 项目名称\n";
// ===== 编译器必须在 project() 之前设置,否则 CMake 会默认用 NMake =====
content += "# ========================================\n";
content += "# 编译器配置(必须在 project() 之前)\n";
content += "# ========================================\n\n";
QString compilerPath = config->getCompilerPath();
QString assemblerPath = config->getAssemblerPath();
QString linkerPath = config->getLinkerPath();
if (!compilerPath.isEmpty() && compilerPath != "gcc") {
content += "set(CMAKE_C_COMPILER \"" + compilerPath + "\")\n";
// 如果编译器是 MinGW 路径,自动推导 make 程序目录
// 例如C:/msys2/mingw64/bin/gcc.exe → C:/msys2/mingw64/bin/mingw32-make.exe
if (compilerPath.contains("mingw", Qt::CaseInsensitive)) {
QString makeDir = QFileInfo(compilerPath).path(); // path() 正确保留绝对路径的盘符
content += "set(CMAKE_MAKE_PROGRAM \"" + makeDir + "/mingw32-make.exe\")\n";
}
}
if (!assemblerPath.isEmpty() && assemblerPath != "gcc") {
content += "set(CMAKE_ASM_COMPILER \"" + assemblerPath + "\")\n";
}
if (!linkerPath.isEmpty() && linkerPath != "gcc" && linkerPath != compilerPath) {
content += "set(CMAKE_C_LINK_EXECUTABLE \"" + linkerPath + " <FLAGS> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>\")\n";
}
content += "\n";
content += "# 项目名称(编译器已设置,不再尝试检测)\n";
content += "project(" + projectName + " C)\n\n";
content += "# C 标准\n";
@@ -111,13 +141,6 @@ QString CMakeGenerator::generateProjectConfig(const ProjectConfig *config)
content += "set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/" + outputDir + ")\n";
content += "set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/" + outputDir + ")\n\n";
// 编译器配置
content += "# 编译器配置\n";
if (!config->getCompilerPath().isEmpty() && config->getCompilerPath() != "gcc") {
content += "set(CMAKE_C_COMPILER \"" + config->getCompilerPath() + "\")\n";
}
content += "\n";
return content;
}
@@ -244,7 +267,7 @@ QString CMakeGenerator::generateCompilerOptions(const ProjectConfig *config)
return content;
}
QString CMakeGenerator::generateBuildTargets(const ProjectConfig *config)
QString CMakeGenerator::generateBuildTargets(const ProjectConfig * /*config*/)
{
QString content;

View File

@@ -49,37 +49,37 @@ void MainWindow::setupUI()
QPushButton *openBtn = new QPushButton("📂 打开工程");
openBtn->setMinimumHeight(35);
openBtn->setStyleSheet("QPushButton { background: #2196F3; color: white; font-weight: bold; }");
openBtn->setStyleSheet("QPushButton { background: #1565C0; color: white; font-weight: bold; border: none; border-radius: 4px; padding: 6px 16px; }");
connect(openBtn, &QPushButton::clicked, this, &MainWindow::openProject);
actionLayout->addWidget(openBtn);
QPushButton *newBtn = new QPushButton("📄 新建工程");
newBtn->setMinimumHeight(35);
newBtn->setStyleSheet("QPushButton { background: #4CAF50; color: white; font-weight: bold; }");
newBtn->setStyleSheet("QPushButton { background: #2E7D32; color: white; font-weight: bold; border: none; border-radius: 4px; padding: 6px 16px; }");
connect(newBtn, &QPushButton::clicked, this, &MainWindow::newProject);
actionLayout->addWidget(newBtn);
saveButton = new QPushButton("💾 保存工程 (Ctrl+S)");
saveButton->setMinimumHeight(35);
saveButton->setStyleSheet("QPushButton { background: #FF9800; color: white; font-weight: bold; }");
saveButton->setStyleSheet("QPushButton { background: #E65100; color: white; font-weight: bold; border: none; border-radius: 4px; padding: 6px 16px; }");
connect(saveButton, &QPushButton::clicked, this, &MainWindow::saveProject);
actionLayout->addWidget(saveButton);
generateButton = new QPushButton("🔨 生成 CMake (Ctrl+G)");
generateButton->setMinimumHeight(35);
generateButton->setStyleSheet("QPushButton { background: #9C27B0; color: white; font-weight: bold; }");
generateButton->setStyleSheet("QPushButton { background: #6A1B9A; color: white; font-weight: bold; border: none; border-radius: 4px; padding: 6px 16px; }");
connect(generateButton, &QPushButton::clicked, this, &MainWindow::generateCMake);
actionLayout->addWidget(generateButton);
QPushButton *debugBtn = new QPushButton("🐛 编译 Debug");
debugBtn->setMinimumHeight(35);
debugBtn->setStyleSheet("QPushButton { background: #00BCD4; color: white; font-weight: bold; }");
debugBtn->setStyleSheet("QPushButton { background: #00838F; color: white; font-weight: bold; border: none; border-radius: 4px; padding: 6px 16px; }");
connect(debugBtn, &QPushButton::clicked, this, &MainWindow::compileDebug);
actionLayout->addWidget(debugBtn);
QPushButton *releaseBtn = new QPushButton("🚀 编译 Release");
releaseBtn->setMinimumHeight(35);
releaseBtn->setStyleSheet("QPushButton { background: #E91E63; color: white; font-weight: bold; }");
releaseBtn->setStyleSheet("QPushButton { background: #AD1457; color: white; font-weight: bold; border: none; border-radius: 4px; padding: 6px 16px; }");
connect(releaseBtn, &QPushButton::clicked, this, &MainWindow::compileRelease);
actionLayout->addWidget(releaseBtn);
@@ -553,7 +553,15 @@ void MainWindow::generateCMake()
// 在 build 目录执行 cmake
QProcess process;
process.setWorkingDirectory(buildDir);
process.start("cmake", QStringList() << "..");
// 跨平台Windows 必须指定 MinGW Makefiles 生成器
QStringList cmakeArgs;
#ifdef Q_OS_WIN32
cmakeArgs << "-G" << "MinGW Makefiles" << "..";
#else
cmakeArgs << "..";
#endif
process.start("cmake", cmakeArgs);
process.waitForFinished(10000);
QString output = process.readAllStandardOutput();
@@ -820,7 +828,7 @@ void MainWindow::compileProject(const QString &buildType)
QPushButton *closeBtn = new QPushButton("❌ 关闭");
closeBtn->setMinimumHeight(35);
closeBtn->setStyleSheet("QPushButton { background: #f44336; color: white; font-weight: bold; font-size: 14px; }");
closeBtn->setStyleSheet("QPushButton { background: #C62828; color: white; font-weight: bold; font-size: 14px; border: none; border-radius: 4px; padding: 6px 16px; }");
dialogLayout->addWidget(closeBtn);
compileDialog->show();
@@ -829,7 +837,15 @@ void MainWindow::compileProject(const QString &buildType)
outputEdit->append("📋 正在配置 CMake...\n");
QProcess cmakeProcess;
cmakeProcess.setWorkingDirectory(buildDir);
cmakeProcess.start("cmake", QStringList() << "-DCMAKE_BUILD_TYPE=" + buildType << "..");
// 跨平台Windows 必须指定 MinGW Makefiles 生成器
QStringList cmakeArgs;
#ifdef Q_OS_WIN32
cmakeArgs << "-G" << "MinGW Makefiles" << "-DCMAKE_BUILD_TYPE=" + buildType << "..";
#else
cmakeArgs << "-DCMAKE_BUILD_TYPE=" + buildType << "..";
#endif
cmakeProcess.start("cmake", cmakeArgs);
cmakeProcess.waitForFinished(30000);
outputEdit->append(cmakeProcess.readAllStandardOutput());
@@ -838,11 +854,17 @@ void MainWindow::compileProject(const QString &buildType)
outputEdit->append("⚠️ CMake 警告/错误:\n" + cmakeError);
}
// 执行 make
// 执行构建跨平台Windows 用 mingw32-makeLinux/macOS 用 make
outputEdit->append("\n🔨 正在编译 " + buildType + "...\n");
QProcess makeProcess;
makeProcess.setWorkingDirectory(buildDir);
makeProcess.start("make", QStringList() << buildType.toLower());
#ifdef Q_OS_WIN32
QString makeCmd = "mingw32-make";
#else
QString makeCmd = "make";
#endif
makeProcess.start(makeCmd, QStringList() << buildType.toLower());
// 实时输出
connect(&makeProcess, &QProcess::readyReadStandardOutput, [&outputEdit, &makeProcess]() {