mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-02 23:35:28 +08:00
qt 6.5.1 original
This commit is contained in:
8
examples/widgets/draganddrop/CMakeLists.txt
Normal file
8
examples/widgets/draganddrop/CMakeLists.txt
Normal file
@ -0,0 +1,8 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
qt_internal_add_example(draggableicons)
|
||||
qt_internal_add_example(draggabletext)
|
||||
qt_internal_add_example(dropsite)
|
||||
qt_internal_add_example(fridgemagnets)
|
||||
qt_internal_add_example(puzzle)
|
9
examples/widgets/draganddrop/README
Normal file
9
examples/widgets/draganddrop/README
Normal file
@ -0,0 +1,9 @@
|
||||
Qt supports native drag and drop on all platforms via an extensible
|
||||
MIME-based system that enables applications to send data to each other in the
|
||||
most appropriate formats.
|
||||
|
||||
Drag and drop can also be implemented for internal use by applications.
|
||||
|
||||
|
||||
Documentation for these examples can be found via the Examples
|
||||
link in the main Qt documentation.
|
6
examples/widgets/draganddrop/draganddrop.pro
Normal file
6
examples/widgets/draganddrop/draganddrop.pro
Normal file
@ -0,0 +1,6 @@
|
||||
TEMPLATE = subdirs
|
||||
SUBDIRS = draggableicons \
|
||||
draggabletext \
|
||||
dropsite \
|
||||
fridgemagnets \
|
||||
puzzle
|
51
examples/widgets/draganddrop/draggableicons/CMakeLists.txt
Normal file
51
examples/widgets/draganddrop/draggableicons/CMakeLists.txt
Normal file
@ -0,0 +1,51 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(draggableicons LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/draganddrop/draggableicons")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(draggableicons
|
||||
dragwidget.cpp dragwidget.h
|
||||
main.cpp
|
||||
)
|
||||
|
||||
set_target_properties(draggableicons PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(draggableicons PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
# Resources:
|
||||
set(draggableicons_resource_files
|
||||
"images/boat.png"
|
||||
"images/car.png"
|
||||
"images/house.png"
|
||||
)
|
||||
|
||||
qt_add_resources(draggableicons "draggableicons"
|
||||
PREFIX
|
||||
"/"
|
||||
FILES
|
||||
${draggableicons_resource_files}
|
||||
)
|
||||
|
||||
install(TARGETS draggableicons
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
@ -0,0 +1,10 @@
|
||||
QT += widgets
|
||||
|
||||
HEADERS = dragwidget.h
|
||||
RESOURCES = draggableicons.qrc
|
||||
SOURCES = dragwidget.cpp \
|
||||
main.cpp
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/draganddrop/draggableicons
|
||||
INSTALLS += target
|
@ -0,0 +1,7 @@
|
||||
<!DOCTYPE RCC><RCC version="1.0">
|
||||
<qresource prefix="">
|
||||
<file>images/boat.png</file>
|
||||
<file>images/car.png</file>
|
||||
<file>images/house.png</file>
|
||||
</qresource>
|
||||
</RCC>
|
131
examples/widgets/draganddrop/draggableicons/dragwidget.cpp
Normal file
131
examples/widgets/draganddrop/draggableicons/dragwidget.cpp
Normal file
@ -0,0 +1,131 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QtWidgets>
|
||||
|
||||
#include "dragwidget.h"
|
||||
|
||||
//! [0]
|
||||
DragWidget::DragWidget(QWidget *parent)
|
||||
: QFrame(parent)
|
||||
{
|
||||
setMinimumSize(200, 200);
|
||||
setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
|
||||
setAcceptDrops(true);
|
||||
|
||||
QLabel *boatIcon = new QLabel(this);
|
||||
boatIcon->setPixmap(QPixmap(":/images/boat.png"));
|
||||
boatIcon->move(10, 10);
|
||||
boatIcon->show();
|
||||
boatIcon->setAttribute(Qt::WA_DeleteOnClose);
|
||||
|
||||
QLabel *carIcon = new QLabel(this);
|
||||
carIcon->setPixmap(QPixmap(":/images/car.png"));
|
||||
carIcon->move(100, 10);
|
||||
carIcon->show();
|
||||
carIcon->setAttribute(Qt::WA_DeleteOnClose);
|
||||
|
||||
QLabel *houseIcon = new QLabel(this);
|
||||
houseIcon->setPixmap(QPixmap(":/images/house.png"));
|
||||
houseIcon->move(10, 80);
|
||||
houseIcon->show();
|
||||
houseIcon->setAttribute(Qt::WA_DeleteOnClose);
|
||||
}
|
||||
//! [0]
|
||||
|
||||
void DragWidget::dragEnterEvent(QDragEnterEvent *event)
|
||||
{
|
||||
if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
|
||||
if (event->source() == this) {
|
||||
event->setDropAction(Qt::MoveAction);
|
||||
event->accept();
|
||||
} else {
|
||||
event->acceptProposedAction();
|
||||
}
|
||||
} else {
|
||||
event->ignore();
|
||||
}
|
||||
}
|
||||
|
||||
void DragWidget::dragMoveEvent(QDragMoveEvent *event)
|
||||
{
|
||||
if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
|
||||
if (event->source() == this) {
|
||||
event->setDropAction(Qt::MoveAction);
|
||||
event->accept();
|
||||
} else {
|
||||
event->acceptProposedAction();
|
||||
}
|
||||
} else {
|
||||
event->ignore();
|
||||
}
|
||||
}
|
||||
|
||||
void DragWidget::dropEvent(QDropEvent *event)
|
||||
{
|
||||
if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
|
||||
QByteArray itemData = event->mimeData()->data("application/x-dnditemdata");
|
||||
QDataStream dataStream(&itemData, QIODevice::ReadOnly);
|
||||
|
||||
QPixmap pixmap;
|
||||
QPoint offset;
|
||||
dataStream >> pixmap >> offset;
|
||||
|
||||
QLabel *newIcon = new QLabel(this);
|
||||
newIcon->setPixmap(pixmap);
|
||||
newIcon->move(event->position().toPoint() - offset);
|
||||
newIcon->show();
|
||||
newIcon->setAttribute(Qt::WA_DeleteOnClose);
|
||||
|
||||
if (event->source() == this) {
|
||||
event->setDropAction(Qt::MoveAction);
|
||||
event->accept();
|
||||
} else {
|
||||
event->acceptProposedAction();
|
||||
}
|
||||
} else {
|
||||
event->ignore();
|
||||
}
|
||||
}
|
||||
|
||||
//! [1]
|
||||
void DragWidget::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
QLabel *child = static_cast<QLabel*>(childAt(event->position().toPoint()));
|
||||
if (!child)
|
||||
return;
|
||||
|
||||
QPixmap pixmap = child->pixmap();
|
||||
|
||||
QByteArray itemData;
|
||||
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
|
||||
dataStream << pixmap << QPoint(event->position().toPoint() - child->pos());
|
||||
//! [1]
|
||||
|
||||
//! [2]
|
||||
QMimeData *mimeData = new QMimeData;
|
||||
mimeData->setData("application/x-dnditemdata", itemData);
|
||||
//! [2]
|
||||
|
||||
//! [3]
|
||||
QDrag *drag = new QDrag(this);
|
||||
drag->setMimeData(mimeData);
|
||||
drag->setPixmap(pixmap);
|
||||
drag->setHotSpot(event->position().toPoint() - child->pos());
|
||||
//! [3]
|
||||
|
||||
QPixmap tempPixmap = pixmap;
|
||||
QPainter painter;
|
||||
painter.begin(&tempPixmap);
|
||||
painter.fillRect(pixmap.rect(), QColor(127, 127, 127, 127));
|
||||
painter.end();
|
||||
|
||||
child->setPixmap(tempPixmap);
|
||||
|
||||
if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction) {
|
||||
child->close();
|
||||
} else {
|
||||
child->show();
|
||||
child->setPixmap(pixmap);
|
||||
}
|
||||
}
|
28
examples/widgets/draganddrop/draggableicons/dragwidget.h
Normal file
28
examples/widgets/draganddrop/draggableicons/dragwidget.h
Normal file
@ -0,0 +1,28 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef DRAGWIDGET_H
|
||||
#define DRAGWIDGET_H
|
||||
|
||||
#include <QFrame>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QDragEnterEvent;
|
||||
class QDropEvent;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
//! [0]
|
||||
class DragWidget : public QFrame
|
||||
{
|
||||
public:
|
||||
explicit DragWidget(QWidget *parent = nullptr);
|
||||
|
||||
protected:
|
||||
void dragEnterEvent(QDragEnterEvent *event) override;
|
||||
void dragMoveEvent(QDragMoveEvent *event) override;
|
||||
void dropEvent(QDropEvent *event) override;
|
||||
void mousePressEvent(QMouseEvent *event) override;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
#endif // DRAGWIDGET_H
|
BIN
examples/widgets/draganddrop/draggableicons/images/boat.png
Normal file
BIN
examples/widgets/draganddrop/draggableicons/images/boat.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.7 KiB |
BIN
examples/widgets/draganddrop/draggableicons/images/car.png
Normal file
BIN
examples/widgets/draganddrop/draggableicons/images/car.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.9 KiB |
BIN
examples/widgets/draganddrop/draggableicons/images/house.png
Normal file
BIN
examples/widgets/draganddrop/draggableicons/images/house.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.2 KiB |
24
examples/widgets/draganddrop/draggableicons/main.cpp
Normal file
24
examples/widgets/draganddrop/draggableicons/main.cpp
Normal file
@ -0,0 +1,24 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QApplication>
|
||||
#include <QHBoxLayout>
|
||||
|
||||
#include "dragwidget.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
Q_INIT_RESOURCE(draggableicons);
|
||||
|
||||
QApplication app(argc, argv);
|
||||
|
||||
QWidget mainWidget;
|
||||
QHBoxLayout *horizontalLayout = new QHBoxLayout(&mainWidget);
|
||||
horizontalLayout->addWidget(new DragWidget);
|
||||
horizontalLayout->addWidget(new DragWidget);
|
||||
|
||||
mainWidget.setWindowTitle(QObject::tr("Draggable Icons"));
|
||||
mainWidget.show();
|
||||
|
||||
return app.exec();
|
||||
}
|
49
examples/widgets/draganddrop/draggabletext/CMakeLists.txt
Normal file
49
examples/widgets/draganddrop/draggabletext/CMakeLists.txt
Normal file
@ -0,0 +1,49 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(draggabletext LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/draganddrop/draggabletext")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(draggabletext
|
||||
dragwidget.cpp dragwidget.h
|
||||
main.cpp
|
||||
)
|
||||
|
||||
set_target_properties(draggabletext PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(draggabletext PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
# Resources:
|
||||
set(draggabletext_resource_files
|
||||
"words.txt"
|
||||
)
|
||||
|
||||
qt_add_resources(draggabletext "draggabletext"
|
||||
PREFIX
|
||||
"/dictionary"
|
||||
FILES
|
||||
${draggabletext_resource_files}
|
||||
)
|
||||
|
||||
install(TARGETS draggabletext
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
10
examples/widgets/draganddrop/draggabletext/draggabletext.pro
Normal file
10
examples/widgets/draganddrop/draggabletext/draggabletext.pro
Normal file
@ -0,0 +1,10 @@
|
||||
QT += widgets
|
||||
|
||||
HEADERS = dragwidget.h
|
||||
RESOURCES = draggabletext.qrc
|
||||
SOURCES = dragwidget.cpp \
|
||||
main.cpp
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/draganddrop/draggabletext
|
||||
INSTALLS += target
|
@ -0,0 +1,5 @@
|
||||
<!DOCTYPE RCC><RCC version="1.0">
|
||||
<qresource prefix="/dictionary">
|
||||
<file>words.txt</file>
|
||||
</qresource>
|
||||
</RCC>
|
130
examples/widgets/draganddrop/draggabletext/dragwidget.cpp
Normal file
130
examples/widgets/draganddrop/draggabletext/dragwidget.cpp
Normal file
@ -0,0 +1,130 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QtWidgets>
|
||||
|
||||
#include "dragwidget.h"
|
||||
|
||||
static QLabel *createDragLabel(const QString &text, QWidget *parent)
|
||||
{
|
||||
QLabel *label = new QLabel(text, parent);
|
||||
label->setAutoFillBackground(true);
|
||||
label->setFrameShape(QFrame::Panel);
|
||||
label->setFrameShadow(QFrame::Raised);
|
||||
return label;
|
||||
}
|
||||
|
||||
static QString hotSpotMimeDataKey() { return QStringLiteral("application/x-hotspot"); }
|
||||
|
||||
DragWidget::DragWidget(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
{
|
||||
QFile dictionaryFile(QStringLiteral(":/dictionary/words.txt"));
|
||||
dictionaryFile.open(QIODevice::ReadOnly);
|
||||
QTextStream inputStream(&dictionaryFile);
|
||||
|
||||
int x = 5;
|
||||
int y = 5;
|
||||
|
||||
while (!inputStream.atEnd()) {
|
||||
QString word;
|
||||
inputStream >> word;
|
||||
if (!word.isEmpty()) {
|
||||
QLabel *wordLabel = createDragLabel(word, this);
|
||||
wordLabel->move(x, y);
|
||||
wordLabel->show();
|
||||
wordLabel->setAttribute(Qt::WA_DeleteOnClose);
|
||||
x += wordLabel->width() + 2;
|
||||
if (x >= 245) {
|
||||
x = 5;
|
||||
y += wordLabel->height() + 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setAcceptDrops(true);
|
||||
setMinimumSize(400, qMax(200, y));
|
||||
setWindowTitle(tr("Draggable Text"));
|
||||
}
|
||||
|
||||
void DragWidget::dragEnterEvent(QDragEnterEvent *event)
|
||||
{
|
||||
if (event->mimeData()->hasText()) {
|
||||
if (event->source() == this) {
|
||||
event->setDropAction(Qt::MoveAction);
|
||||
event->accept();
|
||||
} else {
|
||||
event->acceptProposedAction();
|
||||
}
|
||||
} else {
|
||||
event->ignore();
|
||||
}
|
||||
}
|
||||
|
||||
void DragWidget::dropEvent(QDropEvent *event)
|
||||
{
|
||||
if (event->mimeData()->hasText()) {
|
||||
const QMimeData *mime = event->mimeData();
|
||||
QStringList pieces = mime->text().split(QRegularExpression(QStringLiteral("\\s+")),
|
||||
Qt::SkipEmptyParts);
|
||||
QPoint position = event->position().toPoint();
|
||||
QPoint hotSpot;
|
||||
|
||||
QByteArrayList hotSpotPos = mime->data(hotSpotMimeDataKey()).split(' ');
|
||||
if (hotSpotPos.size() == 2) {
|
||||
hotSpot.setX(hotSpotPos.first().toInt());
|
||||
hotSpot.setY(hotSpotPos.last().toInt());
|
||||
}
|
||||
|
||||
for (const QString &piece : pieces) {
|
||||
QLabel *newLabel = createDragLabel(piece, this);
|
||||
newLabel->move(position - hotSpot);
|
||||
newLabel->show();
|
||||
newLabel->setAttribute(Qt::WA_DeleteOnClose);
|
||||
|
||||
position += QPoint(newLabel->width(), 0);
|
||||
}
|
||||
|
||||
if (event->source() == this) {
|
||||
event->setDropAction(Qt::MoveAction);
|
||||
event->accept();
|
||||
} else {
|
||||
event->acceptProposedAction();
|
||||
}
|
||||
} else {
|
||||
event->ignore();
|
||||
}
|
||||
for (QWidget *widget : findChildren<QWidget *>()) {
|
||||
if (!widget->isVisible())
|
||||
widget->deleteLater();
|
||||
}
|
||||
}
|
||||
|
||||
void DragWidget::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
QLabel *child = qobject_cast<QLabel*>(childAt(event->position().toPoint()));
|
||||
if (!child)
|
||||
return;
|
||||
|
||||
QPoint hotSpot = event->position().toPoint() - child->pos();
|
||||
|
||||
QMimeData *mimeData = new QMimeData;
|
||||
mimeData->setText(child->text());
|
||||
mimeData->setData(hotSpotMimeDataKey(),
|
||||
QByteArray::number(hotSpot.x()) + ' ' + QByteArray::number(hotSpot.y()));
|
||||
|
||||
qreal dpr = window()->windowHandle()->devicePixelRatio();
|
||||
QPixmap pixmap(child->size() * dpr);
|
||||
pixmap.setDevicePixelRatio(dpr);
|
||||
child->render(&pixmap);
|
||||
|
||||
QDrag *drag = new QDrag(this);
|
||||
drag->setMimeData(mimeData);
|
||||
drag->setPixmap(pixmap);
|
||||
drag->setHotSpot(hotSpot);
|
||||
|
||||
Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);
|
||||
|
||||
if (dropAction == Qt::MoveAction)
|
||||
child->close();
|
||||
}
|
25
examples/widgets/draganddrop/draggabletext/dragwidget.h
Normal file
25
examples/widgets/draganddrop/draggabletext/dragwidget.h
Normal file
@ -0,0 +1,25 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef DRAGWIDGET_H
|
||||
#define DRAGWIDGET_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QDragEnterEvent;
|
||||
class QDropEvent;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
class DragWidget : public QWidget
|
||||
{
|
||||
public:
|
||||
explicit DragWidget(QWidget *parent = nullptr);
|
||||
|
||||
protected:
|
||||
void dragEnterEvent(QDragEnterEvent *event) override;
|
||||
void dropEvent(QDropEvent *event) override;
|
||||
void mousePressEvent(QMouseEvent *event) override;
|
||||
};
|
||||
|
||||
#endif // DRAGWIDGET_H
|
16
examples/widgets/draganddrop/draggabletext/main.cpp
Normal file
16
examples/widgets/draganddrop/draggabletext/main.cpp
Normal file
@ -0,0 +1,16 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
#include "dragwidget.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
Q_INIT_RESOURCE(draggabletext);
|
||||
|
||||
QApplication app(argc, argv);
|
||||
DragWidget window;
|
||||
window.show();
|
||||
return app.exec();
|
||||
}
|
41
examples/widgets/draganddrop/draggabletext/words.txt
Normal file
41
examples/widgets/draganddrop/draggabletext/words.txt
Normal file
@ -0,0 +1,41 @@
|
||||
Qt
|
||||
Quarterly
|
||||
is
|
||||
a
|
||||
paper
|
||||
based
|
||||
newsletter
|
||||
exclusively
|
||||
available
|
||||
to
|
||||
Qt
|
||||
customers
|
||||
Every
|
||||
quarter
|
||||
we
|
||||
mail
|
||||
out
|
||||
an
|
||||
issue
|
||||
that
|
||||
we
|
||||
hope
|
||||
will
|
||||
bring
|
||||
added
|
||||
insight
|
||||
and
|
||||
pleasure
|
||||
to
|
||||
your
|
||||
Qt
|
||||
programming
|
||||
with
|
||||
high
|
||||
quality
|
||||
technical
|
||||
articles
|
||||
written
|
||||
by
|
||||
Qt
|
||||
experts
|
38
examples/widgets/draganddrop/dropsite/CMakeLists.txt
Normal file
38
examples/widgets/draganddrop/dropsite/CMakeLists.txt
Normal file
@ -0,0 +1,38 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(dropsite LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/draganddrop/dropsite")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(dropsite
|
||||
droparea.cpp droparea.h
|
||||
dropsitewindow.cpp dropsitewindow.h
|
||||
main.cpp
|
||||
)
|
||||
|
||||
set_target_properties(dropsite PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(dropsite PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
install(TARGETS dropsite
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
93
examples/widgets/draganddrop/dropsite/droparea.cpp
Normal file
93
examples/widgets/draganddrop/dropsite/droparea.cpp
Normal file
@ -0,0 +1,93 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "droparea.h"
|
||||
|
||||
#include <QDragEnterEvent>
|
||||
#include <QMimeData>
|
||||
|
||||
using namespace Qt::StringLiterals;
|
||||
|
||||
//! [DropArea constructor]
|
||||
DropArea::DropArea(QWidget *parent)
|
||||
: QLabel(parent)
|
||||
{
|
||||
setMinimumSize(200, 200);
|
||||
setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
|
||||
setAlignment(Qt::AlignCenter);
|
||||
setAcceptDrops(true);
|
||||
setAutoFillBackground(true);
|
||||
clear();
|
||||
}
|
||||
//! [DropArea constructor]
|
||||
|
||||
//! [dragEnterEvent() function]
|
||||
void DropArea::dragEnterEvent(QDragEnterEvent *event)
|
||||
{
|
||||
setText(tr("<drop content>"));
|
||||
setBackgroundRole(QPalette::Highlight);
|
||||
|
||||
event->acceptProposedAction();
|
||||
emit changed(event->mimeData());
|
||||
}
|
||||
//! [dragEnterEvent() function]
|
||||
|
||||
//! [dragMoveEvent() function]
|
||||
void DropArea::dragMoveEvent(QDragMoveEvent *event)
|
||||
{
|
||||
event->acceptProposedAction();
|
||||
}
|
||||
//! [dragMoveEvent() function]
|
||||
|
||||
//! [dropEvent() function part1]
|
||||
void DropArea::dropEvent(QDropEvent *event)
|
||||
{
|
||||
const QMimeData *mimeData = event->mimeData();
|
||||
//! [dropEvent() function part1]
|
||||
|
||||
//! [dropEvent() function part2]
|
||||
if (mimeData->hasImage()) {
|
||||
setPixmap(qvariant_cast<QPixmap>(mimeData->imageData()));
|
||||
} else if (mimeData->hasFormat(u"text/markdown"_s)) {
|
||||
setText(QString::fromUtf8(mimeData->data(u"text/markdown"_s)));
|
||||
setTextFormat(Qt::MarkdownText);
|
||||
} else if (mimeData->hasHtml()) {
|
||||
setText(mimeData->html());
|
||||
setTextFormat(Qt::RichText);
|
||||
} else if (mimeData->hasText()) {
|
||||
setText(mimeData->text());
|
||||
setTextFormat(Qt::PlainText);
|
||||
} else if (mimeData->hasUrls()) {
|
||||
QList<QUrl> urlList = mimeData->urls();
|
||||
QString text;
|
||||
for (qsizetype i = 0, count = qMin(urlList.size(), qsizetype(32)); i < count; ++i)
|
||||
text += urlList.at(i).path() + u'\n';
|
||||
setText(text);
|
||||
} else {
|
||||
setText(tr("Cannot display data"));
|
||||
}
|
||||
//! [dropEvent() function part2]
|
||||
|
||||
//! [dropEvent() function part3]
|
||||
setBackgroundRole(QPalette::Dark);
|
||||
event->acceptProposedAction();
|
||||
}
|
||||
//! [dropEvent() function part3]
|
||||
|
||||
//! [dragLeaveEvent() function]
|
||||
void DropArea::dragLeaveEvent(QDragLeaveEvent *event)
|
||||
{
|
||||
clear();
|
||||
event->accept();
|
||||
}
|
||||
//! [dragLeaveEvent() function]
|
||||
|
||||
//! [clear() function]
|
||||
void DropArea::clear()
|
||||
{
|
||||
setText(tr("<drop content>"));
|
||||
setBackgroundRole(QPalette::Dark);
|
||||
|
||||
emit changed();
|
||||
}
|
||||
//! [clear() function]
|
37
examples/widgets/draganddrop/dropsite/droparea.h
Normal file
37
examples/widgets/draganddrop/dropsite/droparea.h
Normal file
@ -0,0 +1,37 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef DROPAREA_H
|
||||
#define DROPAREA_H
|
||||
|
||||
#include <QLabel>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QMimeData;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
//! [DropArea header part1]
|
||||
class DropArea : public QLabel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit DropArea(QWidget *parent = nullptr);
|
||||
|
||||
public slots:
|
||||
void clear();
|
||||
|
||||
signals:
|
||||
void changed(const QMimeData *mimeData = nullptr);
|
||||
//! [DropArea header part1]
|
||||
|
||||
//! [DropArea header part2]
|
||||
protected:
|
||||
void dragEnterEvent(QDragEnterEvent *event) override;
|
||||
void dragMoveEvent(QDragMoveEvent *event) override;
|
||||
void dragLeaveEvent(QDragLeaveEvent *event) override;
|
||||
void dropEvent(QDropEvent *event) override;
|
||||
};
|
||||
//! [DropArea header part2]
|
||||
|
||||
#endif // DROPAREA_H
|
12
examples/widgets/draganddrop/dropsite/dropsite.pro
Normal file
12
examples/widgets/draganddrop/dropsite/dropsite.pro
Normal file
@ -0,0 +1,12 @@
|
||||
QT += widgets
|
||||
requires(qtConfig(tablewidget))
|
||||
|
||||
HEADERS = droparea.h \
|
||||
dropsitewindow.h
|
||||
SOURCES = droparea.cpp \
|
||||
dropsitewindow.cpp \
|
||||
main.cpp
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/draganddrop/dropsite
|
||||
INSTALLS += target
|
135
examples/widgets/draganddrop/dropsite/dropsitewindow.cpp
Normal file
135
examples/widgets/draganddrop/dropsite/dropsitewindow.cpp
Normal file
@ -0,0 +1,135 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QPushButton>
|
||||
#include <QDialogButtonBox>
|
||||
#include <QHeaderView>
|
||||
#include <QTableWidget>
|
||||
#include <QTableWidgetItem>
|
||||
#include <QVBoxLayout>
|
||||
|
||||
#include <QClipboard>
|
||||
#include <QGuiApplication>
|
||||
|
||||
#include <QMimeData>
|
||||
|
||||
#include "droparea.h"
|
||||
#include "dropsitewindow.h"
|
||||
|
||||
using namespace Qt::StringLiterals;
|
||||
|
||||
//! [constructor part1]
|
||||
DropSiteWindow::DropSiteWindow()
|
||||
{
|
||||
abstractLabel = new QLabel(tr("This example accepts drags from other "
|
||||
"applications and displays the MIME types "
|
||||
"provided by the drag object."));
|
||||
abstractLabel->setWordWrap(true);
|
||||
abstractLabel->adjustSize();
|
||||
//! [constructor part1]
|
||||
|
||||
//! [constructor part2]
|
||||
dropArea = new DropArea;
|
||||
connect(dropArea, &DropArea::changed,
|
||||
this, &DropSiteWindow::updateFormatsTable);
|
||||
//! [constructor part2]
|
||||
|
||||
//! [constructor part3]
|
||||
formatsTable = new QTableWidget;
|
||||
formatsTable->setColumnCount(2);
|
||||
formatsTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||||
formatsTable->setHorizontalHeaderLabels({tr("Format"), tr("Content")});
|
||||
formatsTable->horizontalHeader()->setStretchLastSection(true);
|
||||
//! [constructor part3]
|
||||
|
||||
//! [constructor part4]
|
||||
clearButton = new QPushButton(tr("Clear"));
|
||||
copyButton = new QPushButton(tr("Copy"));
|
||||
quitButton = new QPushButton(tr("Quit"));
|
||||
|
||||
buttonBox = new QDialogButtonBox;
|
||||
buttonBox->addButton(clearButton, QDialogButtonBox::ActionRole);
|
||||
buttonBox->addButton(copyButton, QDialogButtonBox::ActionRole);
|
||||
#if !QT_CONFIG(clipboard)
|
||||
copyButton->setVisible(false);
|
||||
#endif
|
||||
|
||||
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
|
||||
|
||||
connect(quitButton, &QAbstractButton::clicked, this, &QWidget::close);
|
||||
connect(clearButton, &QAbstractButton::clicked, dropArea, &DropArea::clear);
|
||||
connect(copyButton, &QAbstractButton::clicked, this, &DropSiteWindow::copy);
|
||||
//! [constructor part4]
|
||||
|
||||
//! [constructor part5]
|
||||
QVBoxLayout *mainLayout = new QVBoxLayout(this);
|
||||
mainLayout->addWidget(abstractLabel);
|
||||
mainLayout->addWidget(dropArea);
|
||||
mainLayout->addWidget(formatsTable);
|
||||
mainLayout->addWidget(buttonBox);
|
||||
|
||||
setWindowTitle(tr("Drop Site"));
|
||||
resize(700, 500);
|
||||
}
|
||||
//! [constructor part5]
|
||||
|
||||
//! [updateFormatsTable() part1]
|
||||
void DropSiteWindow::updateFormatsTable(const QMimeData *mimeData)
|
||||
{
|
||||
formatsTable->setRowCount(0);
|
||||
copyButton->setEnabled(false);
|
||||
if (!mimeData)
|
||||
return;
|
||||
//! [updateFormatsTable() part1]
|
||||
|
||||
//! [updateFormatsTable() part2]
|
||||
const QStringList formats = mimeData->formats();
|
||||
for (const QString &format : formats) {
|
||||
QTableWidgetItem *formatItem = new QTableWidgetItem(format);
|
||||
formatItem->setFlags(Qt::ItemIsEnabled);
|
||||
formatItem->setTextAlignment(Qt::AlignTop | Qt::AlignLeft);
|
||||
//! [updateFormatsTable() part2]
|
||||
|
||||
//! [updateFormatsTable() part3]
|
||||
QString text;
|
||||
if (format == u"text/plain") {
|
||||
text = mimeData->text().simplified();
|
||||
} else if (format == u"text/markdown") {
|
||||
text = QString::fromUtf8(mimeData->data(u"text/markdown"_s));
|
||||
} else if (format == u"text/html") {
|
||||
text = mimeData->html().simplified();
|
||||
} else if (format == u"text/uri-list") {
|
||||
QList<QUrl> urlList = mimeData->urls();
|
||||
for (qsizetype i = 0, count = qMin(urlList.size(), qsizetype(32)); i < count; ++i)
|
||||
text.append(urlList.at(i).toString() + u' ');
|
||||
} else {
|
||||
QByteArray data = mimeData->data(format);
|
||||
if (data.size() > 32)
|
||||
data.truncate(32);
|
||||
text = QString::fromLatin1(data.toHex(' ')).toUpper();
|
||||
}
|
||||
//! [updateFormatsTable() part3]
|
||||
|
||||
//! [updateFormatsTable() part4]
|
||||
int row = formatsTable->rowCount();
|
||||
formatsTable->insertRow(row);
|
||||
formatsTable->setItem(row, 0, new QTableWidgetItem(format));
|
||||
formatsTable->setItem(row, 1, new QTableWidgetItem(text));
|
||||
}
|
||||
|
||||
formatsTable->resizeColumnToContents(0);
|
||||
#if QT_CONFIG(clipboard)
|
||||
copyButton->setEnabled(formatsTable->rowCount() > 0);
|
||||
#endif
|
||||
}
|
||||
//! [updateFormatsTable() part4]
|
||||
|
||||
void DropSiteWindow::copy()
|
||||
{
|
||||
#if QT_CONFIG(clipboard)
|
||||
QString text;
|
||||
for (int row = 0, rowCount = formatsTable->rowCount(); row < rowCount; ++row)
|
||||
text += formatsTable->item(row, 0)->text() + ": " + formatsTable->item(row, 1)->text() + '\n';
|
||||
QGuiApplication::clipboard()->setText(text);
|
||||
#endif
|
||||
}
|
42
examples/widgets/draganddrop/dropsite/dropsitewindow.h
Normal file
42
examples/widgets/draganddrop/dropsite/dropsitewindow.h
Normal file
@ -0,0 +1,42 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef DROPSITEWINDOW_H
|
||||
#define DROPSITEWINDOW_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QDialogButtonBox;
|
||||
class QLabel;
|
||||
class QMimeData;
|
||||
class QPushButton;
|
||||
class QTableWidget;
|
||||
QT_END_NAMESPACE
|
||||
class DropArea;
|
||||
|
||||
//! [DropSiteWindow header]
|
||||
class DropSiteWindow : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
DropSiteWindow();
|
||||
|
||||
public slots:
|
||||
void updateFormatsTable(const QMimeData *mimeData);
|
||||
void copy();
|
||||
|
||||
private:
|
||||
DropArea *dropArea;
|
||||
QLabel *abstractLabel;
|
||||
QTableWidget *formatsTable;
|
||||
|
||||
QPushButton *clearButton;
|
||||
QPushButton *copyButton;
|
||||
QPushButton *quitButton;
|
||||
QDialogButtonBox *buttonBox;
|
||||
};
|
||||
//! [DropSiteWindow header]
|
||||
|
||||
#endif // DROPSITEWINDOW_H
|
16
examples/widgets/draganddrop/dropsite/main.cpp
Normal file
16
examples/widgets/draganddrop/dropsite/main.cpp
Normal file
@ -0,0 +1,16 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
#include "dropsitewindow.h"
|
||||
|
||||
//! [main() function]
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
DropSiteWindow window;
|
||||
window.show();
|
||||
return app.exec();
|
||||
}
|
||||
//! [main() function]
|
50
examples/widgets/draganddrop/fridgemagnets/CMakeLists.txt
Normal file
50
examples/widgets/draganddrop/fridgemagnets/CMakeLists.txt
Normal file
@ -0,0 +1,50 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(fridgemagnets LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/draganddrop/fridgemagnets")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(fridgemagnets
|
||||
draglabel.cpp draglabel.h
|
||||
dragwidget.cpp dragwidget.h
|
||||
main.cpp
|
||||
)
|
||||
|
||||
set_target_properties(fridgemagnets PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(fridgemagnets PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
# Resources:
|
||||
set(fridgemagnets_resource_files
|
||||
"words.txt"
|
||||
)
|
||||
|
||||
qt_add_resources(fridgemagnets "fridgemagnets"
|
||||
PREFIX
|
||||
"/dictionary"
|
||||
FILES
|
||||
${fridgemagnets_resource_files}
|
||||
)
|
||||
|
||||
install(TARGETS fridgemagnets
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
51
examples/widgets/draganddrop/fridgemagnets/draglabel.cpp
Normal file
51
examples/widgets/draganddrop/fridgemagnets/draglabel.cpp
Normal file
@ -0,0 +1,51 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "draglabel.h"
|
||||
|
||||
#include <QtWidgets>
|
||||
|
||||
//! [0]
|
||||
DragLabel::DragLabel(const QString &text, QWidget *parent)
|
||||
: QLabel(parent)
|
||||
{
|
||||
QFontMetrics metric(font());
|
||||
QSize size = metric.size(Qt::TextSingleLine, text);
|
||||
|
||||
QImage image(size.width() + 12, size.height() + 12, QImage::Format_ARGB32_Premultiplied);
|
||||
image.fill(qRgba(0, 0, 0, 0));
|
||||
|
||||
QFont font;
|
||||
font.setStyleStrategy(QFont::ForceOutline);
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
QLinearGradient gradient(0, 0, 0, image.height()-1);
|
||||
gradient.setColorAt(0.0, Qt::white);
|
||||
gradient.setColorAt(0.2, QColor(200, 200, 255));
|
||||
gradient.setColorAt(0.8, QColor(200, 200, 255));
|
||||
gradient.setColorAt(1.0, QColor(127, 127, 200));
|
||||
|
||||
QPainter painter;
|
||||
painter.begin(&image);
|
||||
painter.setRenderHint(QPainter::Antialiasing);
|
||||
painter.setBrush(gradient);
|
||||
painter.drawRoundedRect(QRectF(0.5, 0.5, image.width()-1, image.height()-1),
|
||||
25, 25, Qt::RelativeSize);
|
||||
|
||||
painter.setFont(font);
|
||||
painter.setBrush(Qt::black);
|
||||
painter.drawText(QRect(QPoint(6, 6), size), Qt::AlignCenter, text);
|
||||
painter.end();
|
||||
//! [1]
|
||||
|
||||
//! [2]
|
||||
setPixmap(QPixmap::fromImage(image));
|
||||
m_labelText = text;
|
||||
}
|
||||
//! [2]
|
||||
|
||||
QString DragLabel::labelText() const
|
||||
{
|
||||
return m_labelText;
|
||||
}
|
27
examples/widgets/draganddrop/fridgemagnets/draglabel.h
Normal file
27
examples/widgets/draganddrop/fridgemagnets/draglabel.h
Normal file
@ -0,0 +1,27 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef DRAGLABEL_H
|
||||
#define DRAGLABEL_H
|
||||
|
||||
#include <QLabel>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QDragEnterEvent;
|
||||
class QDragMoveEvent;
|
||||
class QFrame;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
//! [0]
|
||||
class DragLabel : public QLabel
|
||||
{
|
||||
public:
|
||||
DragLabel(const QString &text, QWidget *parent);
|
||||
QString labelText() const;
|
||||
|
||||
private:
|
||||
QString m_labelText;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
#endif // DRAGLABEL_H
|
176
examples/widgets/draganddrop/fridgemagnets/dragwidget.cpp
Normal file
176
examples/widgets/draganddrop/fridgemagnets/dragwidget.cpp
Normal file
@ -0,0 +1,176 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "draglabel.h"
|
||||
#include "dragwidget.h"
|
||||
|
||||
#include <QtWidgets>
|
||||
|
||||
static inline QString fridgetMagnetsMimeType() { return QStringLiteral("application/x-fridgemagnet"); }
|
||||
|
||||
//! [0]
|
||||
DragWidget::DragWidget(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
{
|
||||
QFile dictionaryFile(QStringLiteral(":/dictionary/words.txt"));
|
||||
dictionaryFile.open(QFile::ReadOnly);
|
||||
QTextStream inputStream(&dictionaryFile);
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
int x = 5;
|
||||
int y = 5;
|
||||
|
||||
while (!inputStream.atEnd()) {
|
||||
QString word;
|
||||
inputStream >> word;
|
||||
if (!word.isEmpty()) {
|
||||
DragLabel *wordLabel = new DragLabel(word, this);
|
||||
wordLabel->move(x, y);
|
||||
wordLabel->show();
|
||||
wordLabel->setAttribute(Qt::WA_DeleteOnClose);
|
||||
x += wordLabel->width() + 2;
|
||||
if (x >= 245) {
|
||||
x = 5;
|
||||
y += wordLabel->height() + 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
//! [1]
|
||||
|
||||
//! [2]
|
||||
QPalette newPalette = palette();
|
||||
newPalette.setColor(QPalette::Window, Qt::white);
|
||||
setPalette(newPalette);
|
||||
|
||||
setMinimumSize(400, qMax(200, y));
|
||||
setWindowTitle(tr("Fridge Magnets"));
|
||||
//! [2] //! [3]
|
||||
setAcceptDrops(true);
|
||||
}
|
||||
//! [3]
|
||||
|
||||
//! [4]
|
||||
void DragWidget::dragEnterEvent(QDragEnterEvent *event)
|
||||
{
|
||||
//! [4] //! [5]
|
||||
if (event->mimeData()->hasFormat(fridgetMagnetsMimeType())) {
|
||||
if (children().contains(event->source())) {
|
||||
event->setDropAction(Qt::MoveAction);
|
||||
event->accept();
|
||||
} else {
|
||||
event->acceptProposedAction();
|
||||
//! [5] //! [6]
|
||||
}
|
||||
//! [6] //! [7]
|
||||
} else if (event->mimeData()->hasText()) {
|
||||
event->acceptProposedAction();
|
||||
} else {
|
||||
event->ignore();
|
||||
}
|
||||
}
|
||||
//! [7]
|
||||
|
||||
//! [8]
|
||||
void DragWidget::dragMoveEvent(QDragMoveEvent *event)
|
||||
{
|
||||
if (event->mimeData()->hasFormat(fridgetMagnetsMimeType())) {
|
||||
if (children().contains(event->source())) {
|
||||
event->setDropAction(Qt::MoveAction);
|
||||
event->accept();
|
||||
} else {
|
||||
event->acceptProposedAction();
|
||||
}
|
||||
} else if (event->mimeData()->hasText()) {
|
||||
event->acceptProposedAction();
|
||||
} else {
|
||||
event->ignore();
|
||||
}
|
||||
}
|
||||
//! [8]
|
||||
|
||||
//! [9]
|
||||
void DragWidget::dropEvent(QDropEvent *event)
|
||||
{
|
||||
if (event->mimeData()->hasFormat(fridgetMagnetsMimeType())) {
|
||||
const QMimeData *mime = event->mimeData();
|
||||
//! [9] //! [10]
|
||||
QByteArray itemData = mime->data(fridgetMagnetsMimeType());
|
||||
QDataStream dataStream(&itemData, QIODevice::ReadOnly);
|
||||
|
||||
QString text;
|
||||
QPoint offset;
|
||||
dataStream >> text >> offset;
|
||||
//! [10]
|
||||
//! [11]
|
||||
DragLabel *newLabel = new DragLabel(text, this);
|
||||
newLabel->move(event->position().toPoint() - offset);
|
||||
newLabel->show();
|
||||
newLabel->setAttribute(Qt::WA_DeleteOnClose);
|
||||
|
||||
if (event->source() == this) {
|
||||
event->setDropAction(Qt::MoveAction);
|
||||
event->accept();
|
||||
} else {
|
||||
event->acceptProposedAction();
|
||||
}
|
||||
//! [11] //! [12]
|
||||
} else if (event->mimeData()->hasText()) {
|
||||
QStringList pieces = event->mimeData()->text().split(
|
||||
QRegularExpression(QStringLiteral("\\s+")), Qt::SkipEmptyParts);
|
||||
QPoint position = event->position().toPoint();
|
||||
|
||||
for (const QString &piece : pieces) {
|
||||
DragLabel *newLabel = new DragLabel(piece, this);
|
||||
newLabel->move(position);
|
||||
newLabel->show();
|
||||
newLabel->setAttribute(Qt::WA_DeleteOnClose);
|
||||
|
||||
position += QPoint(newLabel->width(), 0);
|
||||
}
|
||||
|
||||
event->acceptProposedAction();
|
||||
} else {
|
||||
event->ignore();
|
||||
}
|
||||
}
|
||||
//! [12]
|
||||
|
||||
//! [13]
|
||||
void DragWidget::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
//! [13]
|
||||
//! [14]
|
||||
DragLabel *child = static_cast<DragLabel*>(childAt(event->position().toPoint()));
|
||||
if (!child)
|
||||
return;
|
||||
|
||||
QPoint hotSpot = event->position().toPoint() - child->pos();
|
||||
|
||||
QByteArray itemData;
|
||||
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
|
||||
dataStream << child->labelText() << QPoint(hotSpot);
|
||||
//! [14]
|
||||
|
||||
//! [15]
|
||||
QMimeData *mimeData = new QMimeData;
|
||||
mimeData->setData(fridgetMagnetsMimeType(), itemData);
|
||||
mimeData->setText(child->labelText());
|
||||
//! [15]
|
||||
|
||||
//! [16]
|
||||
QDrag *drag = new QDrag(this);
|
||||
drag->setMimeData(mimeData);
|
||||
drag->setPixmap(child->pixmap());
|
||||
drag->setHotSpot(hotSpot);
|
||||
|
||||
child->hide();
|
||||
//! [16]
|
||||
|
||||
//! [17]
|
||||
if (drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction) == Qt::MoveAction)
|
||||
child->close();
|
||||
else
|
||||
child->show();
|
||||
}
|
||||
//! [17]
|
28
examples/widgets/draganddrop/fridgemagnets/dragwidget.h
Normal file
28
examples/widgets/draganddrop/fridgemagnets/dragwidget.h
Normal file
@ -0,0 +1,28 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef DRAGWIDGET_H
|
||||
#define DRAGWIDGET_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QDragEnterEvent;
|
||||
class QDropEvent;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
//! [0]
|
||||
class DragWidget : public QWidget
|
||||
{
|
||||
public:
|
||||
explicit DragWidget(QWidget *parent = nullptr);
|
||||
|
||||
protected:
|
||||
void dragEnterEvent(QDragEnterEvent *event) override;
|
||||
void dragMoveEvent(QDragMoveEvent *event) override;
|
||||
void dropEvent(QDropEvent *event) override;
|
||||
void mousePressEvent(QMouseEvent *event) override;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
#endif // DRAGWIDGET_H
|
12
examples/widgets/draganddrop/fridgemagnets/fridgemagnets.pro
Normal file
12
examples/widgets/draganddrop/fridgemagnets/fridgemagnets.pro
Normal file
@ -0,0 +1,12 @@
|
||||
QT += widgets
|
||||
|
||||
HEADERS = draglabel.h \
|
||||
dragwidget.h
|
||||
RESOURCES = fridgemagnets.qrc
|
||||
SOURCES = draglabel.cpp \
|
||||
dragwidget.cpp \
|
||||
main.cpp
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/draganddrop/fridgemagnets
|
||||
INSTALLS += target
|
@ -0,0 +1,5 @@
|
||||
<!DOCTYPE RCC><RCC version="1.0">
|
||||
<qresource prefix="/dictionary">
|
||||
<file>words.txt</file>
|
||||
</qresource>
|
||||
</RCC>
|
25
examples/widgets/draganddrop/fridgemagnets/main.cpp
Normal file
25
examples/widgets/draganddrop/fridgemagnets/main.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
#include "dragwidget.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
Q_INIT_RESOURCE(fridgemagnets);
|
||||
|
||||
QApplication app(argc, argv);
|
||||
#ifdef QT_KEYPAD_NAVIGATION
|
||||
QApplication::setNavigationMode(Qt::NavigationModeCursorAuto);
|
||||
#endif
|
||||
DragWidget window;
|
||||
|
||||
bool smallScreen = QApplication::arguments().contains(QStringLiteral("-small-screen"));
|
||||
if (smallScreen)
|
||||
window.showFullScreen();
|
||||
else
|
||||
window.show();
|
||||
|
||||
return app.exec();
|
||||
}
|
48
examples/widgets/draganddrop/fridgemagnets/words.txt
Normal file
48
examples/widgets/draganddrop/fridgemagnets/words.txt
Normal file
@ -0,0 +1,48 @@
|
||||
Colorless
|
||||
green
|
||||
ideas
|
||||
sleep
|
||||
furiously
|
||||
A
|
||||
colorless
|
||||
green
|
||||
idea
|
||||
is
|
||||
a
|
||||
new
|
||||
untried
|
||||
idea
|
||||
that
|
||||
is
|
||||
without
|
||||
vividness
|
||||
dull
|
||||
and
|
||||
unexciting
|
||||
To
|
||||
sleep
|
||||
furiously
|
||||
may
|
||||
seem
|
||||
a
|
||||
puzzling
|
||||
turn
|
||||
of
|
||||
phrase
|
||||
but
|
||||
the
|
||||
mind
|
||||
in
|
||||
sleep
|
||||
often
|
||||
indeed
|
||||
moves
|
||||
furiously
|
||||
with
|
||||
ideas
|
||||
and
|
||||
images
|
||||
flickering
|
||||
in
|
||||
and
|
||||
out
|
51
examples/widgets/draganddrop/puzzle/CMakeLists.txt
Normal file
51
examples/widgets/draganddrop/puzzle/CMakeLists.txt
Normal file
@ -0,0 +1,51 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(puzzle LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/draganddrop_puzzle")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(draganddrop_puzzle
|
||||
main.cpp
|
||||
mainwindow.cpp mainwindow.h
|
||||
pieceslist.cpp pieceslist.h
|
||||
puzzlewidget.cpp puzzlewidget.h
|
||||
)
|
||||
|
||||
set_target_properties(draganddrop_puzzle PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(draganddrop_puzzle PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
# Resources:
|
||||
set(puzzle_resource_files
|
||||
"example.jpg"
|
||||
)
|
||||
|
||||
qt_add_resources(draganddrop_puzzle "puzzle"
|
||||
PREFIX
|
||||
"/images"
|
||||
FILES
|
||||
${puzzle_resource_files}
|
||||
)
|
||||
|
||||
install(TARGETS draganddrop_puzzle
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
BIN
examples/widgets/draganddrop/puzzle/example.jpg
Normal file
BIN
examples/widgets/draganddrop/puzzle/example.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 42 KiB |
17
examples/widgets/draganddrop/puzzle/main.cpp
Normal file
17
examples/widgets/draganddrop/puzzle/main.cpp
Normal file
@ -0,0 +1,17 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "mainwindow.h"
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
Q_INIT_RESOURCE(puzzle);
|
||||
|
||||
QApplication app(argc, argv);
|
||||
MainWindow window;
|
||||
window.loadImage(QStringLiteral(":/images/example.jpg"));
|
||||
window.show();
|
||||
return app.exec();
|
||||
}
|
118
examples/widgets/draganddrop/puzzle/mainwindow.cpp
Normal file
118
examples/widgets/draganddrop/puzzle/mainwindow.cpp
Normal file
@ -0,0 +1,118 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "mainwindow.h"
|
||||
#include "pieceslist.h"
|
||||
#include "puzzlewidget.h"
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <stdlib.h>
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
: QMainWindow(parent)
|
||||
{
|
||||
setupMenus();
|
||||
setupWidgets();
|
||||
|
||||
setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
|
||||
setWindowTitle(tr("Puzzle"));
|
||||
}
|
||||
|
||||
void MainWindow::openImage()
|
||||
{
|
||||
const QString directory =
|
||||
QStandardPaths::standardLocations(QStandardPaths::PicturesLocation).value(0, QDir::homePath());
|
||||
QFileDialog dialog(this, tr("Open Image"), directory);
|
||||
dialog.setAcceptMode(QFileDialog::AcceptOpen);
|
||||
dialog.setFileMode(QFileDialog::ExistingFile);
|
||||
QStringList mimeTypeFilters;
|
||||
for (const QByteArray &mimeTypeName : QImageReader::supportedMimeTypes())
|
||||
mimeTypeFilters.append(mimeTypeName);
|
||||
mimeTypeFilters.sort();
|
||||
dialog.setMimeTypeFilters(mimeTypeFilters);
|
||||
dialog.selectMimeTypeFilter("image/jpeg");
|
||||
if (dialog.exec() == QDialog::Accepted)
|
||||
loadImage(dialog.selectedFiles().constFirst());
|
||||
}
|
||||
|
||||
void MainWindow::loadImage(const QString &fileName)
|
||||
{
|
||||
QPixmap newImage;
|
||||
if (!newImage.load(fileName)) {
|
||||
QMessageBox::warning(this, tr("Open Image"),
|
||||
tr("The image file could not be loaded."),
|
||||
QMessageBox::Close);
|
||||
return;
|
||||
}
|
||||
puzzleImage = newImage;
|
||||
setupPuzzle();
|
||||
}
|
||||
|
||||
void MainWindow::setCompleted()
|
||||
{
|
||||
QMessageBox::information(this, tr("Puzzle Completed"),
|
||||
tr("Congratulations! You have completed the puzzle!\n"
|
||||
"Click OK to start again."),
|
||||
QMessageBox::Ok);
|
||||
|
||||
setupPuzzle();
|
||||
}
|
||||
|
||||
void MainWindow::setupPuzzle()
|
||||
{
|
||||
int size = qMin(puzzleImage.width(), puzzleImage.height());
|
||||
puzzleImage = puzzleImage.copy((puzzleImage.width() - size) / 2,
|
||||
(puzzleImage.height() - size) / 2, size, size).scaled(puzzleWidget->width(),
|
||||
puzzleWidget->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
||||
|
||||
piecesList->clear();
|
||||
|
||||
for (int y = 0; y < 5; ++y) {
|
||||
for (int x = 0; x < 5; ++x) {
|
||||
int pieceSize = puzzleWidget->pieceSize();
|
||||
QPixmap pieceImage = puzzleImage.copy(x * pieceSize, y * pieceSize, pieceSize, pieceSize);
|
||||
piecesList->addPiece(pieceImage, QPoint(x, y));
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < piecesList->count(); ++i) {
|
||||
if (QRandomGenerator::global()->bounded(2) == 1) {
|
||||
QListWidgetItem *item = piecesList->takeItem(i);
|
||||
piecesList->insertItem(0, item);
|
||||
}
|
||||
}
|
||||
|
||||
puzzleWidget->clear();
|
||||
}
|
||||
|
||||
void MainWindow::setupMenus()
|
||||
{
|
||||
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
|
||||
|
||||
QAction *openAction = fileMenu->addAction(tr("&Open..."), this, &MainWindow::openImage);
|
||||
openAction->setShortcuts(QKeySequence::Open);
|
||||
|
||||
QAction *exitAction = fileMenu->addAction(tr("E&xit"), qApp, &QCoreApplication::quit);
|
||||
exitAction->setShortcuts(QKeySequence::Quit);
|
||||
|
||||
QMenu *gameMenu = menuBar()->addMenu(tr("&Game"));
|
||||
|
||||
gameMenu->addAction(tr("&Restart"), this, &MainWindow::setupPuzzle);
|
||||
}
|
||||
|
||||
void MainWindow::setupWidgets()
|
||||
{
|
||||
QFrame *frame = new QFrame;
|
||||
QHBoxLayout *frameLayout = new QHBoxLayout(frame);
|
||||
puzzleWidget = new PuzzleWidget(400);
|
||||
|
||||
piecesList = new PiecesList(puzzleWidget->pieceSize(), this);
|
||||
|
||||
|
||||
connect(puzzleWidget, &PuzzleWidget::puzzleCompleted,
|
||||
this, &MainWindow::setCompleted, Qt::QueuedConnection);
|
||||
|
||||
frameLayout->addWidget(piecesList);
|
||||
frameLayout->addWidget(puzzleWidget);
|
||||
setCentralWidget(frame);
|
||||
}
|
40
examples/widgets/draganddrop/puzzle/mainwindow.h
Normal file
40
examples/widgets/draganddrop/puzzle/mainwindow.h
Normal file
@ -0,0 +1,40 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef MAINWINDOW_H
|
||||
#define MAINWINDOW_H
|
||||
|
||||
#include <QMainWindow>
|
||||
#include <QPixmap>
|
||||
|
||||
class PiecesList;
|
||||
class PuzzleWidget;
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QListWidgetItem;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit MainWindow(QWidget *parent = nullptr);
|
||||
void loadImage(const QString &path);
|
||||
|
||||
public slots:
|
||||
void openImage();
|
||||
void setupPuzzle();
|
||||
|
||||
private slots:
|
||||
void setCompleted();
|
||||
|
||||
private:
|
||||
void setupMenus();
|
||||
void setupWidgets();
|
||||
|
||||
QPixmap puzzleImage;
|
||||
PiecesList *piecesList;
|
||||
PuzzleWidget *puzzleWidget;
|
||||
};
|
||||
|
||||
#endif // MAINWINDOW_H
|
87
examples/widgets/draganddrop/puzzle/pieceslist.cpp
Normal file
87
examples/widgets/draganddrop/puzzle/pieceslist.cpp
Normal file
@ -0,0 +1,87 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "pieceslist.h"
|
||||
|
||||
#include <QDrag>
|
||||
#include <QDragEnterEvent>
|
||||
#include <QMimeData>
|
||||
|
||||
PiecesList::PiecesList(int pieceSize, QWidget *parent)
|
||||
: QListWidget(parent), m_PieceSize(pieceSize)
|
||||
{
|
||||
setDragEnabled(true);
|
||||
setViewMode(QListView::IconMode);
|
||||
setIconSize(QSize(m_PieceSize, m_PieceSize));
|
||||
setSpacing(10);
|
||||
setAcceptDrops(true);
|
||||
setDropIndicatorShown(true);
|
||||
}
|
||||
|
||||
void PiecesList::dragEnterEvent(QDragEnterEvent *event)
|
||||
{
|
||||
if (event->mimeData()->hasFormat(PiecesList::puzzleMimeType()))
|
||||
event->accept();
|
||||
else
|
||||
event->ignore();
|
||||
}
|
||||
|
||||
void PiecesList::dragMoveEvent(QDragMoveEvent *event)
|
||||
{
|
||||
if (event->mimeData()->hasFormat(PiecesList::puzzleMimeType())) {
|
||||
event->setDropAction(Qt::MoveAction);
|
||||
event->accept();
|
||||
} else {
|
||||
event->ignore();
|
||||
}
|
||||
}
|
||||
|
||||
void PiecesList::dropEvent(QDropEvent *event)
|
||||
{
|
||||
if (event->mimeData()->hasFormat(PiecesList::puzzleMimeType())) {
|
||||
QByteArray pieceData = event->mimeData()->data(PiecesList::puzzleMimeType());
|
||||
QDataStream dataStream(&pieceData, QIODevice::ReadOnly);
|
||||
QPixmap pixmap;
|
||||
QPoint location;
|
||||
dataStream >> pixmap >> location;
|
||||
|
||||
addPiece(pixmap, location);
|
||||
|
||||
event->setDropAction(Qt::MoveAction);
|
||||
event->accept();
|
||||
} else {
|
||||
event->ignore();
|
||||
}
|
||||
}
|
||||
|
||||
void PiecesList::addPiece(const QPixmap &pixmap, const QPoint &location)
|
||||
{
|
||||
QListWidgetItem *pieceItem = new QListWidgetItem(this);
|
||||
pieceItem->setIcon(QIcon(pixmap));
|
||||
pieceItem->setData(Qt::UserRole, QVariant(pixmap));
|
||||
pieceItem->setData(Qt::UserRole+1, location);
|
||||
pieceItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);
|
||||
}
|
||||
|
||||
void PiecesList::startDrag(Qt::DropActions /*supportedActions*/)
|
||||
{
|
||||
QListWidgetItem *item = currentItem();
|
||||
|
||||
QByteArray itemData;
|
||||
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
|
||||
QPixmap pixmap = qvariant_cast<QPixmap>(item->data(Qt::UserRole));
|
||||
QPoint location = item->data(Qt::UserRole+1).toPoint();
|
||||
|
||||
dataStream << pixmap << location;
|
||||
|
||||
QMimeData *mimeData = new QMimeData;
|
||||
mimeData->setData(PiecesList::puzzleMimeType(), itemData);
|
||||
|
||||
QDrag *drag = new QDrag(this);
|
||||
drag->setMimeData(mimeData);
|
||||
drag->setHotSpot(QPoint(pixmap.width()/2, pixmap.height()/2));
|
||||
drag->setPixmap(pixmap);
|
||||
|
||||
if (drag->exec(Qt::MoveAction) == Qt::MoveAction)
|
||||
delete takeItem(row(item));
|
||||
}
|
28
examples/widgets/draganddrop/puzzle/pieceslist.h
Normal file
28
examples/widgets/draganddrop/puzzle/pieceslist.h
Normal file
@ -0,0 +1,28 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef PIECESLIST_H
|
||||
#define PIECESLIST_H
|
||||
|
||||
#include <QListWidget>
|
||||
|
||||
class PiecesList : public QListWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit PiecesList(int pieceSize, QWidget *parent = nullptr);
|
||||
void addPiece(const QPixmap &pixmap, const QPoint &location);
|
||||
|
||||
static QString puzzleMimeType() { return QStringLiteral("image/x-puzzle-piece"); }
|
||||
|
||||
protected:
|
||||
void dragEnterEvent(QDragEnterEvent *event) override;
|
||||
void dragMoveEvent(QDragMoveEvent *event) override;
|
||||
void dropEvent(QDropEvent *event) override;
|
||||
void startDrag(Qt::DropActions supportedActions) override;
|
||||
|
||||
int m_PieceSize;
|
||||
};
|
||||
|
||||
#endif // PIECESLIST_H
|
17
examples/widgets/draganddrop/puzzle/puzzle.pro
Normal file
17
examples/widgets/draganddrop/puzzle/puzzle.pro
Normal file
@ -0,0 +1,17 @@
|
||||
QT += widgets
|
||||
requires(qtConfig(filedialog))
|
||||
|
||||
HEADERS = mainwindow.h \
|
||||
pieceslist.h \
|
||||
puzzlewidget.h
|
||||
RESOURCES = puzzle.qrc
|
||||
SOURCES = main.cpp \
|
||||
mainwindow.cpp \
|
||||
pieceslist.cpp \
|
||||
puzzlewidget.cpp
|
||||
|
||||
QMAKE_PROJECT_NAME = dndpuzzle
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/draganddrop/puzzle
|
||||
INSTALLS += target
|
5
examples/widgets/draganddrop/puzzle/puzzle.qrc
Normal file
5
examples/widgets/draganddrop/puzzle/puzzle.qrc
Normal file
@ -0,0 +1,5 @@
|
||||
<!DOCTYPE RCC><RCC version="1.0">
|
||||
<qresource prefix="/images">
|
||||
<file>example.jpg</file>
|
||||
</qresource>
|
||||
</RCC>
|
167
examples/widgets/draganddrop/puzzle/puzzlewidget.cpp
Normal file
167
examples/widgets/draganddrop/puzzle/puzzlewidget.cpp
Normal file
@ -0,0 +1,167 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "puzzlewidget.h"
|
||||
#include "pieceslist.h"
|
||||
|
||||
#include <QDrag>
|
||||
#include <QDragEnterEvent>
|
||||
#include <QMimeData>
|
||||
#include <QPainter>
|
||||
|
||||
PuzzleWidget::PuzzleWidget(int imageSize, QWidget *parent)
|
||||
: QWidget(parent), m_ImageSize(imageSize)
|
||||
{
|
||||
setAcceptDrops(true);
|
||||
setMinimumSize(m_ImageSize, m_ImageSize);
|
||||
setMaximumSize(m_ImageSize, m_ImageSize);
|
||||
}
|
||||
|
||||
void PuzzleWidget::clear()
|
||||
{
|
||||
pieces.clear();
|
||||
highlightedRect = QRect();
|
||||
inPlace = 0;
|
||||
update();
|
||||
}
|
||||
|
||||
void PuzzleWidget::dragEnterEvent(QDragEnterEvent *event)
|
||||
{
|
||||
if (event->mimeData()->hasFormat(PiecesList::puzzleMimeType()))
|
||||
event->accept();
|
||||
else
|
||||
event->ignore();
|
||||
}
|
||||
|
||||
void PuzzleWidget::dragLeaveEvent(QDragLeaveEvent *event)
|
||||
{
|
||||
QRect updateRect = highlightedRect;
|
||||
highlightedRect = QRect();
|
||||
update(updateRect);
|
||||
event->accept();
|
||||
}
|
||||
|
||||
void PuzzleWidget::dragMoveEvent(QDragMoveEvent *event)
|
||||
{
|
||||
QRect updateRect = highlightedRect.united(targetSquare(event->position().toPoint()));
|
||||
|
||||
if (event->mimeData()->hasFormat(PiecesList::puzzleMimeType())
|
||||
&& findPiece(targetSquare(event->position().toPoint())) == -1) {
|
||||
|
||||
highlightedRect = targetSquare(event->position().toPoint());
|
||||
event->setDropAction(Qt::MoveAction);
|
||||
event->accept();
|
||||
} else {
|
||||
highlightedRect = QRect();
|
||||
event->ignore();
|
||||
}
|
||||
|
||||
update(updateRect);
|
||||
}
|
||||
|
||||
void PuzzleWidget::dropEvent(QDropEvent *event)
|
||||
{
|
||||
if (event->mimeData()->hasFormat(PiecesList::puzzleMimeType())
|
||||
&& findPiece(targetSquare(event->position().toPoint())) == -1) {
|
||||
|
||||
QByteArray pieceData = event->mimeData()->data(PiecesList::puzzleMimeType());
|
||||
QDataStream dataStream(&pieceData, QIODevice::ReadOnly);
|
||||
Piece piece;
|
||||
piece.rect = targetSquare(event->position().toPoint());
|
||||
dataStream >> piece.pixmap >> piece.location;
|
||||
|
||||
pieces.append(piece);
|
||||
|
||||
highlightedRect = QRect();
|
||||
update(piece.rect);
|
||||
|
||||
event->setDropAction(Qt::MoveAction);
|
||||
event->accept();
|
||||
|
||||
if (piece.location == piece.rect.topLeft() / pieceSize()) {
|
||||
inPlace++;
|
||||
if (inPlace == 25)
|
||||
emit puzzleCompleted();
|
||||
}
|
||||
} else {
|
||||
highlightedRect = QRect();
|
||||
event->ignore();
|
||||
}
|
||||
}
|
||||
|
||||
int PuzzleWidget::findPiece(const QRect &pieceRect) const
|
||||
{
|
||||
for (int i = 0, size = pieces.size(); i < size; ++i) {
|
||||
if (pieces.at(i).rect == pieceRect)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void PuzzleWidget::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
QRect square = targetSquare(event->position().toPoint());
|
||||
const int found = findPiece(square);
|
||||
|
||||
if (found == -1)
|
||||
return;
|
||||
|
||||
Piece piece = pieces.takeAt(found);
|
||||
|
||||
if (piece.location == square.topLeft() / pieceSize())
|
||||
inPlace--;
|
||||
|
||||
update(square);
|
||||
|
||||
QByteArray itemData;
|
||||
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
|
||||
|
||||
dataStream << piece.pixmap << piece.location;
|
||||
|
||||
QMimeData *mimeData = new QMimeData;
|
||||
mimeData->setData(PiecesList::puzzleMimeType(), itemData);
|
||||
|
||||
QDrag *drag = new QDrag(this);
|
||||
drag->setMimeData(mimeData);
|
||||
drag->setHotSpot(event->position().toPoint() - square.topLeft());
|
||||
drag->setPixmap(piece.pixmap);
|
||||
|
||||
if (drag->exec(Qt::MoveAction) != Qt::MoveAction) {
|
||||
pieces.insert(found, piece);
|
||||
update(targetSquare(event->position().toPoint()));
|
||||
|
||||
if (piece.location == square.topLeft() / pieceSize())
|
||||
inPlace++;
|
||||
}
|
||||
}
|
||||
|
||||
void PuzzleWidget::paintEvent(QPaintEvent *event)
|
||||
{
|
||||
QPainter painter(this);
|
||||
painter.fillRect(event->rect(), Qt::white);
|
||||
|
||||
if (highlightedRect.isValid()) {
|
||||
painter.setBrush(QColor("#ffcccc"));
|
||||
painter.setPen(Qt::NoPen);
|
||||
painter.drawRect(highlightedRect.adjusted(0, 0, -1, -1));
|
||||
}
|
||||
|
||||
for (const Piece &piece : pieces)
|
||||
painter.drawPixmap(piece.rect, piece.pixmap);
|
||||
}
|
||||
|
||||
const QRect PuzzleWidget::targetSquare(const QPoint &position) const
|
||||
{
|
||||
QPoint topLeft = QPoint(position.x() / pieceSize(), position.y() / pieceSize()) * pieceSize();
|
||||
return QRect(topLeft, QSize(pieceSize(), pieceSize()));
|
||||
}
|
||||
|
||||
int PuzzleWidget::pieceSize() const
|
||||
{
|
||||
return m_ImageSize / 5;
|
||||
}
|
||||
|
||||
int PuzzleWidget::imageSize() const
|
||||
{
|
||||
return m_ImageSize;
|
||||
}
|
56
examples/widgets/draganddrop/puzzle/puzzlewidget.h
Normal file
56
examples/widgets/draganddrop/puzzle/puzzlewidget.h
Normal file
@ -0,0 +1,56 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef PUZZLEWIDGET_H
|
||||
#define PUZZLEWIDGET_H
|
||||
|
||||
#include <QPoint>
|
||||
#include <QPixmap>
|
||||
#include <QList>
|
||||
#include <QWidget>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QDragEnterEvent;
|
||||
class QDropEvent;
|
||||
class QMouseEvent;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
class PuzzleWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit PuzzleWidget(int imageSize, QWidget *parent = nullptr);
|
||||
void clear();
|
||||
|
||||
int pieceSize() const;
|
||||
int imageSize() const;
|
||||
|
||||
signals:
|
||||
void puzzleCompleted();
|
||||
|
||||
protected:
|
||||
void dragEnterEvent(QDragEnterEvent *event) override;
|
||||
void dragLeaveEvent(QDragLeaveEvent *event) override;
|
||||
void dragMoveEvent(QDragMoveEvent *event) override;
|
||||
void dropEvent(QDropEvent *event) override;
|
||||
void mousePressEvent(QMouseEvent *event) override;
|
||||
void paintEvent(QPaintEvent *event) override;
|
||||
|
||||
private:
|
||||
struct Piece {
|
||||
QPixmap pixmap;
|
||||
QRect rect;
|
||||
QPoint location;
|
||||
};
|
||||
|
||||
int findPiece(const QRect &pieceRect) const;
|
||||
const QRect targetSquare(const QPoint &position) const;
|
||||
|
||||
QList<Piece> pieces;
|
||||
QRect highlightedRect;
|
||||
int inPlace;
|
||||
int m_ImageSize;
|
||||
};
|
||||
|
||||
#endif // PUZZLEWIDGET_H
|
Reference in New Issue
Block a user