i have a custom qdeclarativeitem subclass type called polygon
i want to drag one polygon to another and want other polygon to change its color for example.
the "other polygon" must know a polygon is dragged enter into it. also when i dragged one polygon it must move, not stay old place.
i found the codes at the bottom of the post and i write
Polygon{
id:poly1
//poly specifications
DragArea{
}
}
Polygon{
id:poly2
//poly specifications
DropArea{
}
}
Polygon{
id:poly1
//poly specifications
DragArea{
}
}
Polygon{
id:poly2
//poly specifications
DropArea{
}
}
To copy to clipboard, switch view to plain text mode
and when i clicked in poly1 and dragged the mouse into poly2, poly2 become notified
BUT poly1 stays at its old place, i want it to move.
so i tried sth like
Polygon{
id:poly1
//poly specifications
DragArea{
//some code
MouseArea{
//some code
}
}
}
Polygon{
id:poly2
//poly specifications
DropArea{
}
}
Polygon{
id:poly1
//poly specifications
DragArea{
//some code
MouseArea{
//some code
}
}
}
Polygon{
id:poly2
//poly specifications
DropArea{
}
}
To copy to clipboard, switch view to plain text mode
and tried sth more but can not succeed.
I do not know if i make a dragging by myself, so i want to extend mousearea class and write drag area code in it.
Thanks for reply
Codes that i found, DragArea DropAea classes:
#include "DeclarativeDragArea.h"
#include "DeclarativeMimeData.h"
#include <QDrag>
#include <QMimeData>
#include <QGraphicsSceneMouseEvent>
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
/*!
A DragArea is used to make an item draggable.
*/
DeclarativeDragArea::DeclarativeDragArea(QDeclarativeItem *parent)
: QDeclarativeItem(parent),
m_delegate(0),
m_source(0),
m_target(0),
m_enabled(true),
m_supportedActions(Qt::MoveAction),
m_defaultAction(Qt::MoveAction),
m_data(new DeclarativeMimeData()) // m_data is owned by us, and we shouldn't pass it to Qt directly as it will automatically delete it after the drag and drop.
{
setAcceptedMouseButtons(Qt::LeftButton);
}
DeclarativeDragArea::~DeclarativeDragArea()
{
if (m_data) {
delete m_data;
}
}
/*!
The delegate is the item that will be displayed next to the mouse cursor during the drag and drop operation.
It usually consists of a large, semi-transparent icon representing the data being dragged.
*/
QDeclarativeComponent* DeclarativeDragArea::delegate() const
{
return m_delegate;
}
void DeclarativeDragArea::setDelegate(QDeclarativeComponent *delegate)
{
if (m_delegate != delegate) {
m_delegate = delegate;
emit delegateChanged();
}
}
void DeclarativeDragArea::resetDelegate()
{
setDelegate(0);
}
/*!
The QML element that is the source of this drag and drop operation. This can be defined to any item, and will
be available to the DropArea as event.data.source
*/
QDeclarativeItem* DeclarativeDragArea::source() const
{
return m_source;
}
void DeclarativeDragArea::setSource(QDeclarativeItem* source)
{
if (m_source != source) {
m_source = source;
emit sourceChanged();
}
}
void DeclarativeDragArea::resetSource()
{
setSource(0);
}
// target
QDeclarativeItem* DeclarativeDragArea::target() const
{
//TODO: implement me
return 0;
}
// data
DeclarativeMimeData* DeclarativeDragArea::data() const
{
return m_data;
}
// enabled
bool DeclarativeDragArea::isEnabled() const
{
return m_enabled;
}
void DeclarativeDragArea::setEnabled(bool enabled)
{
if (enabled != m_enabled) {
m_enabled = enabled;
emit enabledChanged();
}
}
// supported actions
Qt::DropActions DeclarativeDragArea::supportedActions() const
{
return m_supportedActions;
}
void DeclarativeDragArea::setSupportedActions(Qt::DropActions actions)
{
if (actions != m_supportedActions) {
m_supportedActions = actions;
emit supportedActionsChanged();
}
}
// default action
Qt::DropAction DeclarativeDragArea::defaultAction() const
{
return m_defaultAction;
}
void DeclarativeDragArea::setDefaultAction(Qt::DropAction action)
{
if (action != m_defaultAction) {
m_defaultAction = action;
emit defaultActionChanged();
}
}
{
if ( !m_enabled
||
QLineF(event
->screenPos
(), event
->buttonDownScreenPos
(Qt
::LeftButton)).
length() return;
}
DeclarativeMimeData* dataCopy = new DeclarativeMimeData(m_data); //Qt will take ownership of this copy and delete it.
drag->setMimeData(dataCopy);
if (m_delegate) {
// Render the delegate to a Pixmap
QDeclarativeItem* item = qobject_cast<QDeclarativeItem *>(m_delegate->create());
scene.addItem(item);
QPixmap pixmap
(scene.
sceneRect().
width(), scene.
sceneRect().
height());
pixmap.fill(Qt::transparent);
painter.
setRenderHint(QPainter::Antialiasing);
scene.render(&painter);
drag->setPixmap(pixmap);
drag
->setHotSpot
(QPoint(0,
0));
// TODO: Make a property for that }
//setCursor(Qt::OpenHandCursor); //TODO? Make a property for the cursor
Qt::DropAction action = drag->exec(m_supportedActions, m_defaultAction);
emit drop(action);
}
#include "DeclarativeDragArea.h"
#include "DeclarativeMimeData.h"
#include <QDrag>
#include <QMimeData>
#include <QGraphicsSceneMouseEvent>
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
/*!
A DragArea is used to make an item draggable.
*/
DeclarativeDragArea::DeclarativeDragArea(QDeclarativeItem *parent)
: QDeclarativeItem(parent),
m_delegate(0),
m_source(0),
m_target(0),
m_enabled(true),
m_supportedActions(Qt::MoveAction),
m_defaultAction(Qt::MoveAction),
m_data(new DeclarativeMimeData()) // m_data is owned by us, and we shouldn't pass it to Qt directly as it will automatically delete it after the drag and drop.
{
setAcceptedMouseButtons(Qt::LeftButton);
}
DeclarativeDragArea::~DeclarativeDragArea()
{
if (m_data) {
delete m_data;
}
}
/*!
The delegate is the item that will be displayed next to the mouse cursor during the drag and drop operation.
It usually consists of a large, semi-transparent icon representing the data being dragged.
*/
QDeclarativeComponent* DeclarativeDragArea::delegate() const
{
return m_delegate;
}
void DeclarativeDragArea::setDelegate(QDeclarativeComponent *delegate)
{
if (m_delegate != delegate) {
m_delegate = delegate;
emit delegateChanged();
}
}
void DeclarativeDragArea::resetDelegate()
{
setDelegate(0);
}
/*!
The QML element that is the source of this drag and drop operation. This can be defined to any item, and will
be available to the DropArea as event.data.source
*/
QDeclarativeItem* DeclarativeDragArea::source() const
{
return m_source;
}
void DeclarativeDragArea::setSource(QDeclarativeItem* source)
{
if (m_source != source) {
m_source = source;
emit sourceChanged();
}
}
void DeclarativeDragArea::resetSource()
{
setSource(0);
}
// target
QDeclarativeItem* DeclarativeDragArea::target() const
{
//TODO: implement me
return 0;
}
// data
DeclarativeMimeData* DeclarativeDragArea::data() const
{
return m_data;
}
// enabled
bool DeclarativeDragArea::isEnabled() const
{
return m_enabled;
}
void DeclarativeDragArea::setEnabled(bool enabled)
{
if (enabled != m_enabled) {
m_enabled = enabled;
emit enabledChanged();
}
}
// supported actions
Qt::DropActions DeclarativeDragArea::supportedActions() const
{
return m_supportedActions;
}
void DeclarativeDragArea::setSupportedActions(Qt::DropActions actions)
{
if (actions != m_supportedActions) {
m_supportedActions = actions;
emit supportedActionsChanged();
}
}
// default action
Qt::DropAction DeclarativeDragArea::defaultAction() const
{
return m_defaultAction;
}
void DeclarativeDragArea::setDefaultAction(Qt::DropAction action)
{
if (action != m_defaultAction) {
m_defaultAction = action;
emit defaultActionChanged();
}
}
void DeclarativeDragArea::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if ( !m_enabled
|| QLineF(event->screenPos(), event->buttonDownScreenPos(Qt::LeftButton)).length()
< QApplication::startDragDistance()) {
return;
}
QDrag *drag = new QDrag(event->widget());
DeclarativeMimeData* dataCopy = new DeclarativeMimeData(m_data); //Qt will take ownership of this copy and delete it.
drag->setMimeData(dataCopy);
if (m_delegate) {
// Render the delegate to a Pixmap
QDeclarativeItem* item = qobject_cast<QDeclarativeItem *>(m_delegate->create());
QGraphicsScene scene;
scene.addItem(item);
QPixmap pixmap(scene.sceneRect().width(), scene.sceneRect().height());
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
painter.setRenderHint(QPainter::Antialiasing);
scene.render(&painter);
drag->setPixmap(pixmap);
drag->setHotSpot(QPoint(0, 0)); // TODO: Make a property for that
}
//setCursor(Qt::OpenHandCursor); //TODO? Make a property for the cursor
Qt::DropAction action = drag->exec(m_supportedActions, m_defaultAction);
emit drop(action);
}
To copy to clipboard, switch view to plain text mode
#include "DeclarativeDropArea.h"
#include "DeclarativeDragDropEvent.h"
#include <QGraphicsSceneDragDropEvent>
#include <QMimeData>
DeclarativeDropArea::DeclarativeDropArea(QDeclarativeItem *parent)
: QDeclarativeItem(parent),
m_enabled(true)
{
setAcceptDrops(m_enabled);
}
void DeclarativeDropArea::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
DeclarativeDragDropEvent dde(event, this);
emit dragEnter(&dde);
}
void DeclarativeDropArea::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
{
DeclarativeDragDropEvent dde(event, this);
emit dragLeave(&dde);
}
void DeclarativeDropArea::dropEvent(QGraphicsSceneDragDropEvent *event)
{
DeclarativeDragDropEvent dde(event, this);
emit drop(&dde);
}
bool DeclarativeDropArea::isEnabled() const
{
return m_enabled;
}
void DeclarativeDropArea::setEnabled(bool enabled)
{
if (enabled == m_enabled) {
return;
}
m_enabled = enabled;
setAcceptDrops(m_enabled);
emit enabledChanged();
}
#include "DeclarativeDropArea.h"
#include "DeclarativeDragDropEvent.h"
#include <QGraphicsSceneDragDropEvent>
#include <QMimeData>
DeclarativeDropArea::DeclarativeDropArea(QDeclarativeItem *parent)
: QDeclarativeItem(parent),
m_enabled(true)
{
setAcceptDrops(m_enabled);
}
void DeclarativeDropArea::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
DeclarativeDragDropEvent dde(event, this);
emit dragEnter(&dde);
}
void DeclarativeDropArea::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
{
DeclarativeDragDropEvent dde(event, this);
emit dragLeave(&dde);
}
void DeclarativeDropArea::dropEvent(QGraphicsSceneDragDropEvent *event)
{
DeclarativeDragDropEvent dde(event, this);
emit drop(&dde);
}
bool DeclarativeDropArea::isEnabled() const
{
return m_enabled;
}
void DeclarativeDropArea::setEnabled(bool enabled)
{
if (enabled == m_enabled) {
return;
}
m_enabled = enabled;
setAcceptDrops(m_enabled);
emit enabledChanged();
}
To copy to clipboard, switch view to plain text mode
Bookmarks