add:添加文件路径支持拖入。

This commit is contained in:
taynpg 2024-09-14 09:35:13 +08:00
parent 2bfddf1ef7
commit e1ef813032
5 changed files with 85 additions and 14 deletions

View File

@ -30,6 +30,7 @@ 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 Widgets)
include_directories(3rd) include_directories(3rd)
include_directories(src)
set(PROJECT_SOURCES set(PROJECT_SOURCES
main.cpp main.cpp
MainWidget.cpp MainWidget.cpp
@ -40,6 +41,7 @@ set(PROJECT_SOURCES
resource/ico.rc src/attribute_edit.ui src/attribute_edit.h resource/ico.rc src/attribute_edit.ui src/attribute_edit.h
src/attribute_edit.cpp flatgray.qrc src/attribute_edit.cpp flatgray.qrc
src/data_edit.h src/data_edit.cpp src/data_edit.ui src/data_edit.h src/data_edit.cpp src/data_edit.ui
src/QCustomQLineEdit.h src/QCustomQLineEdit.cpp
) )
if (MSVC) if (MSVC)

View File

@ -961,11 +961,16 @@ bool MainWidget::format_xml()
if (xml_path.empty()) { if (xml_path.empty()) {
return false; return false;
} }
if (!xml_.handle_save(xml_path)) {
CUtil::msg(this, u8"格式化失败"); if (!CUtil::affirm(this, u8"确认", u8"重排版内容将会覆盖源文件,请确认是否需要备份,继续?")) {
return false; return false;
} }
CUtil::msg(this, u8"格式化结束");
if (!xml_.handle_save(xml_path)) {
CUtil::msg(this, u8"重排版内容失败");
return false;
}
CUtil::msg(this, u8"重排版内容结束");
return true; return true;
} }

View File

@ -6,7 +6,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>1056</width> <width>1102</width>
<height>682</height> <height>682</height>
</rect> </rect>
</property> </property>
@ -35,7 +35,7 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QLineEdit" name="edStatus"/> <widget class="QCustomQLineEdit" name="edStatus"/>
</item> </item>
<item> <item>
<widget class="QComboBox" name="cbConfig"/> <widget class="QComboBox" name="cbConfig"/>
@ -57,7 +57,7 @@
<item> <item>
<widget class="QPushButton" name="btnFormat"> <widget class="QPushButton" name="btnFormat">
<property name="text"> <property name="text">
<string></string> <string>重排版</string>
</property> </property>
</widget> </widget>
</item> </item>
@ -87,7 +87,7 @@
<item> <item>
<widget class="Line" name="line"> <widget class="Line" name="line">
<property name="orientation"> <property name="orientation">
<enum>Qt::Orientation::Vertical</enum> <enum>Qt::Vertical</enum>
</property> </property>
</widget> </widget>
</item> </item>
@ -142,7 +142,7 @@
<item> <item>
<widget class="Line" name="line_2"> <widget class="Line" name="line_2">
<property name="orientation"> <property name="orientation">
<enum>Qt::Orientation::Vertical</enum> <enum>Qt::Vertical</enum>
</property> </property>
</widget> </widget>
</item> </item>
@ -227,9 +227,6 @@
</item> </item>
<item> <item>
<spacer name="horizontalSpacer_2"> <spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0"> <property name="sizeHint" stdset="0">
<size> <size>
<width>40</width> <width>40</width>
@ -247,9 +244,6 @@
</item> </item>
<item> <item>
<spacer name="horizontalSpacer"> <spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0"> <property name="sizeHint" stdset="0">
<size> <size>
<width>40</width> <width>40</width>
@ -329,6 +323,13 @@
</item> </item>
</layout> </layout>
</widget> </widget>
<customwidgets>
<customwidget>
<class>QCustomQLineEdit</class>
<extends>QLineEdit</extends>
<header>QCustomQLineEdit.h</header>
</customwidget>
</customwidgets>
<tabstops> <tabstops>
<tabstop>btnSelectFile</tabstop> <tabstop>btnSelectFile</tabstop>
<tabstop>edStatus</tabstop> <tabstop>edStatus</tabstop>

36
src/QCustomQLineEdit.cpp Normal file
View File

@ -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()
{
}

27
src/QCustomQLineEdit.h Normal file
View File

@ -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