Skip to content

Commit c9e899e

Browse files
添加图片拖拽的实现
1 parent bde77fb commit c9e899e

File tree

8 files changed

+98
-47
lines changed

8 files changed

+98
-47
lines changed

qt01/qt01.pro.user

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3-
<!-- Written by QtCreator 4.9.1, 2023-09-15T00:27:00. -->
3+
<!-- Written by QtCreator 4.9.1, 2023-09-18T00:05:52. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>
@@ -65,7 +65,7 @@
6565
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">桌面</value>
6666
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">桌面</value>
6767
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">{9dfbdac3-1b15-41db-bb48-96d8b0d23b9a}</value>
68-
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">2</value>
68+
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
6969
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
7070
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
7171
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
@@ -315,7 +315,7 @@
315315
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
316316
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
317317
<value type="QString" key="RunConfiguration.WorkingDirectory"></value>
318-
<value type="QString" key="RunConfiguration.WorkingDirectory.default">/work/QT_examples/build-qt01-unknown-Profile</value>
318+
<value type="QString" key="RunConfiguration.WorkingDirectory.default">/work/QT_examples/build-qt01-unknown-Debug</value>
319319
</valuemap>
320320
<value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
321321
</valuemap>

qt02/qt02.pro.user

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3-
<!-- Written by QtCreator 4.9.1, 2023-09-13T00:06:21. -->
3+
<!-- Written by QtCreator 4.9.1, 2023-09-17T21:09:22. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>

qt_clion/CMakeLists.txt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
cmake_minimum_required(VERSION 3.19)
2-
project(test)
1+
cmake_minimum_required(VERSION 3.12)
2+
project(qt_clion)
33

44
set(CMAKE_CXX_STANDARD 11)
55
set(CMAKE_AUTOMOC ON)
@@ -10,10 +10,14 @@ set(QT_VERSION 5)
1010
set(REQUIRED_LIBS Core Gui Widgets)
1111
set(REQUIRED_LIBS_QUALIFIED Qt5::Core Qt5::Gui Qt5::Widgets)
1212

13-
add_executable(${PROJECT_NAME} main.cpp)
13+
add_executable(${PROJECT_NAME}
14+
main.cpp
15+
mainwindow.cpp)
1416

17+
target_include_directories(${PROJECT_NAME}
18+
PUBLIC ./)
1519

16-
set(CMAKE_PREFIX_PATH "D:/software/Qt5.13.0/mingw73_64/lib/cmake/Qt5")
20+
#set(CMAKE_PREFIX_PATH "D:/software/Qt5.13.0/mingw73_64/lib/cmake/Qt5")
1721

1822

1923
if (NOT CMAKE_PREFIX_PATH)

qt_clion/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <QApplication>
22
#include <QPushButton>
33

4+
#include "mainwindow.h"
5+
46
int main(int argc, char *argv[]) {
57
QApplication a(argc, argv);
68
QPushButton button("Hello world!", nullptr);

qt_clion/mainwindow.ui

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<ui version="4.0">
3-
<author/>
4-
<comment/>
5-
<exportmacro/>
6-
<class>MainWindow</class>
7-
<widget class="QMainWindow" name="MainWindow">
8-
<property name="geometry">
9-
<rect>
10-
<x>0</x>
11-
<y>0</y>
12-
<width>400</width>
13-
<height>300</height>
14-
</rect>
15-
</property>
16-
<property name="windowTitle">
17-
<string>MainWindow</string>
18-
</property>
19-
</widget>
20-
<pixmapfunction/>
21-
<connections/>
22-
</ui>
3+
<class>MainWindow</class>
4+
<widget class="QMainWindow" name="MainWindow">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>373</width>
10+
<height>314</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>MainWindow</string>
15+
</property>
16+
</widget>
17+
<resources/>
18+
<connections/>
19+
</ui>

qt_dragdrop/qt_dragdrop.pro.user

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3-
<!-- Written by QtCreator 4.9.1, 2023-09-16T23:55:39. -->
3+
<!-- Written by QtCreator 4.9.1, 2023-09-17T21:09:22. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>

qt_imagedragdrop/mainwindow.cpp

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ MainWindow::~MainWindow()
4242

4343
void MainWindow::mousePressEvent(QMouseEvent * event)
4444
{
45+
4546
// 首先进行图片的获取
4647
// 将鼠标指针所在的位置强制转换为QLabel类型
4748
QLabel *child = static_cast<QLabel*>(childAt(event->pos()));
@@ -63,29 +64,78 @@ void MainWindow::mousePressEvent(QMouseEvent * event)
6364
// 移动过程中显示图片,若是不进行设置则会显示一个小矩形框
6465
drag->setPixmap(pixmap);
6566
// 移动过程中鼠标的位置在图片的位置上保持不变,通过设置鼠标抓手实现鼠标在drag移动过程中相对位置一直不改变
67+
// 如果不设置,拖动过程鼠标将位于左上角位置
6668
drag->setHotSpot(event->pos() - child->pos());
6769
// 给原始图片添加阴影
6870
QPixmap tempPixmap = pixmap; // 使用原始图片
6971
QPainter painter; // 创建画板
7072
painter.begin(&tempPixmap);
7173
// 给图片蒙上一层透明但是淡淡的黑色效果
7274
painter.fillRect(pixmap.rect(), QColor(121, 127, 127, 127));
73-
74-
75-
75+
painter.end();
76+
child->setPixmap(tempPixmap);// 在移动过程中让图片添加一层黑色的阴影
77+
// 执行拖拽操作
78+
// 设置拖拽支持复制和移动操作,默认是复制操作,如果是移动操作,那么在拖拽完成之后关闭原有的标签
79+
// 需要注意的是,drag在点击之后,执行exec会一直卡在这里,直到鼠标松开之后exec会返回一个执行的事件
80+
// 这里的返回值只受到 dropEvent接口的影响
81+
if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction) {
82+
qDebug() << "1=========";
83+
child->close();
84+
} else {
85+
qDebug() << "2=========";
86+
// 如果是复制那么拖拽完成之后显示标签
87+
child->show();
88+
// 现实原来的图片而不是阴影
89+
child->setPixmap(pixmap);
90+
}
7691
}
7792

93+
// window drag event
7894
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
7995
{
96+
// 判断是否是我们自定义的mime类型数据,如果是就进行移动操作
97+
if (event->mimeData()->hasFormat("myimage/png")) {
98+
// 这里设置不设置不影响最终结果,但是会影响点击是鼠标的默认小标签,
99+
event->setDropAction(Qt::MoveAction);
100+
event->accept();
101+
} else {
102+
event->ignore();
103+
}
80104

81105
}
82106

83107
void MainWindow::dragMoveEvent(QDragMoveEvent *event)
84108
{
85-
109+
if (event->mimeData()->hasFormat("myimage/png")) {
110+
// 这里设置不设置不影响最终结果,但是会影响点击是鼠标的默认小标签,
111+
event->setDropAction(Qt::MoveAction);
112+
event->accept();
113+
} else {
114+
event->ignore();
115+
}
86116
}
87117

88118
void MainWindow::dropEvent(QDropEvent *event)
89119
{
90-
120+
if (event->mimeData()->hasFormat("myimage/png")) {
121+
QByteArray itemData = event->mimeData()->data("myimage/png");
122+
QDataStream dataStream(&itemData, QIODevice::ReadOnly);
123+
QPixmap pixmap;
124+
QPoint offset;
125+
// accord the in stream order
126+
dataStream >> pixmap >> offset;
127+
// create new label add pixpng
128+
QLabel *newLabel = new QLabel(this);
129+
newLabel->setPixmap(pixmap);
130+
newLabel->resize(pixmap.size());
131+
newLabel->move(event->pos() - offset);
132+
newLabel->show();
133+
// win close clear the pixmap
134+
newLabel->setAttribute(Qt::WA_DeleteOnClose);
135+
event->setDropAction(Qt::MoveAction);
136+
// event->setDropAction(Qt::CopyAction);
137+
event->accept();
138+
} else {
139+
event->ignore();
140+
}
91141
}

qt_imagedragdrop/qt_imagedragdrop.pro.user

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3-
<!-- Written by QtCreator 4.9.1, 2023-09-17T18:12:44. -->
3+
<!-- Written by QtCreator 4.9.1, 2023-09-18T00:05:52. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>
7-
<value type="QByteArray">{5f89e11e-2c41-419f-96f7-18ebd964d82f}</value>
7+
<value type="QByteArray">{a33e5fbf-84cc-47c5-88b8-4639e8732808}</value>
88
</data>
99
<data>
1010
<variable>ProjectExplorer.Project.ActiveTarget</variable>
@@ -55,23 +55,21 @@
5555
<data>
5656
<variable>ProjectExplorer.Project.PluginSettings</variable>
5757
<valuemap type="QVariantMap">
58-
<valuelist type="QVariantList" key="ClangCodeModel.CustomCommandLineKey">
59-
<value type="QString">-fno-delayed-template-parsing</value>
60-
</valuelist>
58+
<valuelist type="QVariantList" key="ClangCodeModel.CustomCommandLineKey"/>
6159
<value type="bool" key="ClangCodeModel.UseGlobalConfig">true</value>
6260
</valuemap>
6361
</data>
6462
<data>
6563
<variable>ProjectExplorer.Project.Target.0</variable>
6664
<valuemap type="QVariantMap">
67-
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.13.0 MinGW 64-bit</value>
68-
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.13.0 MinGW 64-bit</value>
69-
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt5.5130.win64_mingw73_kit</value>
65+
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">桌面</value>
66+
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">桌面</value>
67+
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">{9dfbdac3-1b15-41db-bb48-96d8b0d23b9a}</value>
7068
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
7169
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
7270
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
7371
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
74-
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">F:/work/QT_examples/build-qt_imagedragdrop-Desktop_Qt_5_13_0_MinGW_64_bit-Debug</value>
72+
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/work/QT_examples/build-qt_imagedragdrop-unknown-Debug</value>
7573
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
7674
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
7775
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
@@ -127,7 +125,7 @@
127125
<value type="bool" key="Qt4ProjectManager.Qt4BuildConfiguration.UseShadowBuild">true</value>
128126
</valuemap>
129127
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.1">
130-
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">F:/work/QT_examples/build-qt_imagedragdrop-Desktop_Qt_5_13_0_MinGW_64_bit-Release</value>
128+
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/work/QT_examples/build-qt_imagedragdrop-unknown-Release</value>
131129
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
132130
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
133131
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
@@ -183,7 +181,7 @@
183181
<value type="bool" key="Qt4ProjectManager.Qt4BuildConfiguration.UseShadowBuild">true</value>
184182
</valuemap>
185183
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.2">
186-
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">F:/work/QT_examples/build-qt_imagedragdrop-Desktop_Qt_5_13_0_MinGW_64_bit-Profile</value>
184+
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/work/QT_examples/build-qt_imagedragdrop-unknown-Profile</value>
187185
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
188186
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
189187
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
@@ -307,7 +305,7 @@
307305
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
308306
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">qt_imagedragdrop</value>
309307
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
310-
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:F:/work/QT_examples/qt_imagedragdrop/qt_imagedragdrop.pro</value>
308+
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:/work/QT_examples/qt_imagedragdrop/qt_imagedragdrop.pro</value>
311309
<value type="QString" key="RunConfiguration.Arguments"></value>
312310
<value type="uint" key="RunConfiguration.QmlDebugServerPort">3768</value>
313311
<value type="bool" key="RunConfiguration.UseCppDebugger">false</value>
@@ -317,7 +315,7 @@
317315
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
318316
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
319317
<value type="QString" key="RunConfiguration.WorkingDirectory"></value>
320-
<value type="QString" key="RunConfiguration.WorkingDirectory.default">F:/work/QT_examples/build-qt_imagedragdrop-Desktop_Qt_5_13_0_MinGW_64_bit-Debug</value>
318+
<value type="QString" key="RunConfiguration.WorkingDirectory.default">/work/QT_examples/build-qt_imagedragdrop-unknown-Debug</value>
321319
</valuemap>
322320
<value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
323321
</valuemap>

0 commit comments

Comments
 (0)