From 8616776a2467c9a340e37413be8dd51f32f5cc17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=99=BE=E5=93=A5?= Date: Tue, 28 Apr 2026 19:03:49 +0800 Subject: [PATCH] =?UTF-8?q?v1.1.4:=20=E4=BF=AE=E5=A4=8D=E6=8B=96=E6=8B=BD?= =?UTF-8?q?=20JSON=20=E6=96=87=E4=BB=B6=E6=89=93=E5=BC=80=E5=B7=A5?= =?UTF-8?q?=E7=A8=8B=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 【Bug 修复】 - setAcceptDrops(true) —— 之前从未设置,导致所有拖拽事件直接被 Qt 忽略 - 新增 dragMoveEvent 覆写 —— 拖拽移动过程也要 accept,否则 dropEvent 不会触发 - 禁用所有子控件的拖拽接收 —— QTreeWidget/QListWidget/QLineEdit 等默认接受拖拽,会先拦截事件导致 MainWindow 收不到 - 同时对 viewport() 禁用拖拽,防止内部滚动区域拦截 --- src/mainwindow.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/mainwindow.h | 1 + 2 files changed, 39 insertions(+) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 63914be..feb52dc 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -30,6 +31,9 @@ MainWindow::MainWindow(QWidget *parent) config = new ProjectConfig(this); cmakeGenerator = new CMakeGenerator(this); + // ★ 启用拖拽 + setAcceptDrops(true); + setupUI(); setupConnections(); updateWindowTitle(); @@ -276,6 +280,22 @@ void MainWindow::setupUI() // 状态栏 statusBar()->showMessage("就绪"); + + // ★ 禁用子控件的拖拽,让所有拖拽事件由 MainWindow 统一处理 + sourceTree->setAcceptDrops(false); + sourceTree->viewport()->setAcceptDrops(false); + includeDirList->setAcceptDrops(false); + includeDirList->viewport()->setAcceptDrops(false); + libraryList->setAcceptDrops(false); + libraryList->viewport()->setAcceptDrops(false); + defineList->setAcceptDrops(false); + defineList->viewport()->setAcceptDrops(false); + optionList->setAcceptDrops(false); + optionList->viewport()->setAcceptDrops(false); + projectNameEdit->setAcceptDrops(false); + compilerPathEdit->setAcceptDrops(false); + assemblerPathEdit->setAcceptDrops(false); + linkerPathEdit->setAcceptDrops(false); } void MainWindow::setupConnections() @@ -350,9 +370,27 @@ void MainWindow::dragEnterEvent(QDragEnterEvent *event) if (file.endsWith(".json", Qt::CaseInsensitive)) { event->acceptProposedAction(); statusBar()->showMessage("📂 释放鼠标打开工程:" + file, 3000); + return; } } } + event->ignore(); +} + +void MainWindow::dragMoveEvent(QDragMoveEvent *event) +{ + // 拖拽移动时也要接受,否则 Qt 不会触发 dropEvent + if (event->mimeData()->hasUrls()) { + QList urls = event->mimeData()->urls(); + if (!urls.isEmpty()) { + QString file = urls.first().toLocalFile(); + if (file.endsWith(".json", Qt::CaseInsensitive)) { + event->acceptProposedAction(); + return; + } + } + } + event->ignore(); } void MainWindow::dropEvent(QDropEvent *event) diff --git a/src/mainwindow.h b/src/mainwindow.h index c0a6e78..fc4b8cd 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -69,6 +69,7 @@ private slots: protected: void dragEnterEvent(QDragEnterEvent *event) override; + void dragMoveEvent(QDragMoveEvent *event) override; void dropEvent(QDropEvent *event) override; private: