×
Namespaces

Variants
Actions
(Difference between revisions)

How to drag & drop QAbstractGraphicsShapeItem in QGraphicsView

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update - Add ArticleMetaData)
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
Line 32: Line 32:
 
Sublass QGraphicsScene and reimplement mousePressEvent, mouseMoveEvent and mouseReleaseEvent
 
Sublass QGraphicsScene and reimplement mousePressEvent, mouseMoveEvent and mouseReleaseEvent
 
to capture all mouse events.
 
to capture all mouse events.
<code cpp>
+
<code cpp-qt>
 
#ifndef MAIN_H
 
#ifndef MAIN_H
 
#define MAIN_H
 
#define MAIN_H
Line 64: Line 64:
 
item over the scene.
 
item over the scene.
  
<code cpp>
+
<code cpp-qt>
 
#include <QtGui>
 
#include <QtGui>
  
Line 110: Line 110:
  
 
In main function we put all things together
 
In main function we put all things together
<code cpp>
+
<code cpp-qt>
 
int main(int argc, char *argv[])
 
int main(int argc, char *argv[])
 
{
 
{

Latest revision as of 04:17, 11 October 2012

Article Metadata
Compatibility
Platform(s): S60, Maemo 5
Symbian
Article
Keywords: QGraphicsView, drag and drop
Created: divanov (21 Mar 2011)
Last edited: hamishwillee (11 Oct 2012)

[edit] Problem

Implement drag and drop in QGraphicsView for QAbstractGraphicsShapeItem items.

[edit] Solution

Sublass QGraphicsScene and reimplement mousePressEvent, mouseMoveEvent and mouseReleaseEvent to capture all mouse events.

#ifndef MAIN_H
#define MAIN_H
 
#include <QtGui>
 
class DnDGraphicsScene : public QGraphicsScene
{
Q_OBJECT
public:
DnDGraphicsScene(QWidget *parent = 0);
 
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);
 
private:
QAbstractGraphicsShapeItem *dragged;
QPen selected;
QBrush selectedBrush;
QPen normal;
QBrush normalBrush;
QPointF offset;
};
 
#endif // MAIN_H

Press event detects beginning of a drag and release end of a drag. In between we move item over the scene.

#include <QtGui>
 
#include "main.h"
 
DnDGraphicsScene::DnDGraphicsScene(QWidget *parent)
: QGraphicsScene(parent), dragged(0)
{
selected = QPen(Qt::red, 3);
selectedBrush = QBrush(Qt::yellow);
}
 
void DnDGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
dragged = qgraphicsitem_cast<QAbstractGraphicsShapeItem*>(
itemAt(mouseEvent->scenePos(), QTransform()));
if (dragged) {
offset = dragged->pos() - mouseEvent->scenePos();
normal = dragged->pen();
normalBrush = dragged->brush();
dragged->setPen(selected);
dragged->setBrush(selectedBrush);
} else
QGraphicsScene::mousePressEvent(mouseEvent);
}
 
void DnDGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (dragged) {
dragged->setPos(offset + mouseEvent->scenePos());
} else
QGraphicsScene::mouseMoveEvent(mouseEvent);
}
 
void DnDGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (dragged) {
dragged->setPen(normal);
dragged->setBrush(normalBrush);
dragged = 0;
} else
QGraphicsScene::mouseReleaseEvent(mouseEvent);
}

In main function we put all things together

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
 
QPolygonF polygon1;
polygon1 << QPointF(0.0, 0.0) << QPointF(25.0, 0.0) << QPointF(12.5, 25.0);
QGraphicsPolygonItem *item1 = new QGraphicsPolygonItem(polygon1);
item1->setBrush(Qt::green);
item1->setTransformOriginPoint(12.5, 12.5);
item1->setRotation(45.0);
QPolygonF polygon2;
polygon2 << QPointF(50.0, 50.0) << QPointF(75.0, 50.0) << QPointF(62.5, 75.0);
QGraphicsPolygonItem *item2 = new QGraphicsPolygonItem(polygon2);
item2->setBrush(Qt::blue);
item2->setTransformOriginPoint(12.5, 12.5);
item2->setRotation(-45.0);
DnDGraphicsScene scene;
scene.addItem(item1);
scene.addItem(item2);
QGraphicsView view(&scene);
view.show();
 
return a.exec();
}
This page was last modified on 11 October 2012, at 04:17.
75 page views in the last 30 days.
×