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: