From e1ef813032b1f83073fb0b0134b247079649941f Mon Sep 17 00:00:00 2001 From: taynpg <taynpg@163.com> Date: Sat, 14 Sep 2024 09:35:13 +0800 Subject: [PATCH] =?UTF-8?q?add=EF=BC=9A=E6=B7=BB=E5=8A=A0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E8=B7=AF=E5=BE=84=E6=94=AF=E6=8C=81=E6=8B=96=E5=85=A5?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 ++ MainWidget.cpp | 11 ++++++++--- MainWidget.ui | 23 ++++++++++++----------- src/QCustomQLineEdit.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/QCustomQLineEdit.h | 27 +++++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 14 deletions(-) create mode 100644 src/QCustomQLineEdit.cpp create mode 100644 src/QCustomQLineEdit.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 36cfc69..57f605a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,7 @@ find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets) include_directories(3rd) +include_directories(src) set(PROJECT_SOURCES main.cpp MainWidget.cpp @@ -40,6 +41,7 @@ set(PROJECT_SOURCES resource/ico.rc src/attribute_edit.ui src/attribute_edit.h src/attribute_edit.cpp flatgray.qrc src/data_edit.h src/data_edit.cpp src/data_edit.ui + src/QCustomQLineEdit.h src/QCustomQLineEdit.cpp ) if (MSVC) diff --git a/MainWidget.cpp b/MainWidget.cpp index 4418d1f..ebb896f 100644 --- a/MainWidget.cpp +++ b/MainWidget.cpp @@ -961,11 +961,16 @@ bool MainWidget::format_xml() if (xml_path.empty()) { return false; } - if (!xml_.handle_save(xml_path)) { - CUtil::msg(this, u8"格式化失败"); + + if (!CUtil::affirm(this, u8"确认", u8"重排版内容将会覆盖源文件,请确认是否需要备份,继续?")) { return false; } - CUtil::msg(this, u8"格式化结束"); + + if (!xml_.handle_save(xml_path)) { + CUtil::msg(this, u8"重排版内容失败"); + return false; + } + CUtil::msg(this, u8"重排版内容结束"); return true; } diff --git a/MainWidget.ui b/MainWidget.ui index 03fae1c..e9a62e6 100644 --- a/MainWidget.ui +++ b/MainWidget.ui @@ -6,7 +6,7 @@ <rect> <x>0</x> <y>0</y> - <width>1056</width> + <width>1102</width> <height>682</height> </rect> </property> @@ -35,7 +35,7 @@ </widget> </item> <item> - <widget class="QLineEdit" name="edStatus"/> + <widget class="QCustomQLineEdit" name="edStatus"/> </item> <item> <widget class="QComboBox" name="cbConfig"/> @@ -57,7 +57,7 @@ <item> <widget class="QPushButton" name="btnFormat"> <property name="text"> - <string>格</string> + <string>重排版</string> </property> </widget> </item> @@ -87,7 +87,7 @@ <item> <widget class="Line" name="line"> <property name="orientation"> - <enum>Qt::Orientation::Vertical</enum> + <enum>Qt::Vertical</enum> </property> </widget> </item> @@ -142,7 +142,7 @@ <item> <widget class="Line" name="line_2"> <property name="orientation"> - <enum>Qt::Orientation::Vertical</enum> + <enum>Qt::Vertical</enum> </property> </widget> </item> @@ -227,9 +227,6 @@ </item> <item> <spacer name="horizontalSpacer_2"> - <property name="orientation"> - <enum>Qt::Orientation::Horizontal</enum> - </property> <property name="sizeHint" stdset="0"> <size> <width>40</width> @@ -247,9 +244,6 @@ </item> <item> <spacer name="horizontalSpacer"> - <property name="orientation"> - <enum>Qt::Orientation::Horizontal</enum> - </property> <property name="sizeHint" stdset="0"> <size> <width>40</width> @@ -329,6 +323,13 @@ </item> </layout> </widget> + <customwidgets> + <customwidget> + <class>QCustomQLineEdit</class> + <extends>QLineEdit</extends> + <header>QCustomQLineEdit.h</header> + </customwidget> + </customwidgets> <tabstops> <tabstop>btnSelectFile</tabstop> <tabstop>edStatus</tabstop> diff --git a/src/QCustomQLineEdit.cpp b/src/QCustomQLineEdit.cpp new file mode 100644 index 0000000..7b4cab4 --- /dev/null +++ b/src/QCustomQLineEdit.cpp @@ -0,0 +1,36 @@ +#include "QCustomQLineEdit.h" + +QCustomQLineEdit::QCustomQLineEdit(QWidget* parent) : QLineEdit(parent) +{ + setAcceptDrops(true); +} + +void QCustomQLineEdit::dragEnterEvent(QDragEnterEvent* event) +{ + if (event->mimeData()->hasUrls()) { + event->acceptProposedAction(); + } + QLineEdit::dragEnterEvent(event); +} + +void QCustomQLineEdit::dragMoveEvent(QDragMoveEvent* event) +{ +} + +void QCustomQLineEdit::dropEvent(QDropEvent* event) +{ + const QMimeData* mimeData = event->mimeData(); + if (mimeData->hasUrls()) { + QList<QUrl> urls = mimeData->urls(); + if (urls.size() > 0) { + QString file_name = urls.at(0).toLocalFile(); + setText(file_name); + event->accept(); + } + } + QLineEdit::dropEvent(event); +} + +QCustomQLineEdit::~QCustomQLineEdit() +{ +} diff --git a/src/QCustomQLineEdit.h b/src/QCustomQLineEdit.h new file mode 100644 index 0000000..b1bef6f --- /dev/null +++ b/src/QCustomQLineEdit.h @@ -0,0 +1,27 @@ +#ifndef QCUSTOMQLINE +#define QCUSTOMQLINE + +#include <QLineEdit> + +#include <QDropEvent> +#include <QMimeData> +#include <QDragEnterEvent> + +class QCustomQLineEdit : public QLineEdit +{ + Q_OBJECT + +public: + QCustomQLineEdit(QWidget* parent = nullptr); + ~QCustomQLineEdit() override; + +protected: + // 拖动文件到窗口 触发 + void dragEnterEvent(QDragEnterEvent* event); + // 拖动文件到窗口移动文件 触发 + void dragMoveEvent(QDragMoveEvent* event); + // 拖动文件到窗口释放文件触发 + void dropEvent(QDropEvent* event); +}; + +#endif \ No newline at end of file