PDA

View Full Version : Drag and Drop from QListWidget to QGraphicsView (Scene)



fritzone
16th June 2010, 10:41
Hi all,

I have the following question: I want to drag an item from a list widget to a graphics view and want to show something on the graphics view when the drop ended. For this I have done the regular work, It works, please see the code below. However I have the following question: I would like to show the thingie on the graphics view exactly where the drop ended... and for this I need your help. Seems the dropEvent (and the dragMove event) are not getting the required coordinates ... Can you hint me towards a solution: I want to place the "itm" object exactly where the mouse drag drop ended. (Don't mind the Version class, has nothing to do with this)

Thanks.
f

Code and out below:



class ERGraphicsScene : public QGraphicsScene
{
public:
ERGraphicsScene(QWidget* parent, Version* v) : QGraphicsScene(parent), itm(0), m_version(v), lastX(-1), lastY(-1)
{
// to mark the centre of the view
addLine(0, -10, 0, 10);
addLine(-10, 0, 10, 0);
}

void setLastCoords(int x, int y)
{
if(x != 0) lastX = x;
if(y != 0) lastY = y;


if(lastX != 0 & &lastY != 0 && lastX != -1 && lastY != -1 && itm != 0)
{
qDebug() << "setLastCoords: X=" << lastX << " Y=" << lastY;

itm->setX(lastX);
itm->setY(lastY);
addItem(itm);
itm = 0;
}
}

protected:
virtual void dropEvent ( QGraphicsSceneDragDropEvent * event )
{
QString tabName = event->mimeData()->text();
event->acceptProposedAction();

itm = m_version->getTable(tabName)->prepareDiagramEntity(event->pos().x(), event->pos().y());
setLastCoords(event->pos().x(), event->pos().y());
}

void dragEnterEvent ( QGraphicsSceneDragDropEvent * event )
{
qDebug() << "GraphicsScene::dragEnter : X=" << event->pos().x() << " Y=" << event->pos().y();
event->acceptProposedAction();
}

void dragMoveEvent ( QGraphicsSceneDragDropEvent * event )
{
qDebug() << "GraphicsScene::dragMove : X=" << event->pos().x() << " Y=" << event->pos().y();
event->acceptProposedAction();
int x = event->pos().x();
int y = event->pos().y();
setLastCoords(x,y);
}

virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "ERGraphicsScene::mouseMove : X=" << event->pos().x() << " Y=" << event->pos().y();
setLastCoords(event->pos().x(), event->pos().y());
}

virtual void mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
{
qDebug() << "GraphicsScene::mouseRelease: X=" << event->pos().x() << " Y=" << event->pos().y();
}

private:
QGraphicsItemGroup* itm;
Version* m_version;
int lastX;
int lastY;

};

class ERGraphicsView : public QGraphicsView
{
public:

ERGraphicsView(QWidget* parent, Version* v) : QGraphicsView(parent)
{
setAcceptDrops(true);
scene = new ERGraphicsScene(this, v);
setScene(scene);
}


void setLastCoords(int x, int y)
{
scene->setLastCoords(x, y);
}

protected:

virtual void mouseMoveEvent(QMouseEvent *event)
{
qDebug() << "GraphicsView::mouseMove : X=" << event->pos().x() << " Y=" << event->pos().y();
scene->setLastCoords(event->pos().x(), event->pos().y());
}

private:

ERGraphicsScene* scene;

};

class TableListWidget : public QListWidget
{
public:

TableListWidget(QWidget* parent, ERGraphicsView* associatedGrView) : QListWidget(parent), grv(associatedGrView)
{
setDragEnabled(true);
}

protected:
virtual void dragEnterEvent(QDragEnterEvent *e)
{
e->accept();
}

virtual void dragMoveEvent(QDragMoveEvent *event)
{
qDebug() << "TabListWidget::dragMove : X=" << event->pos().x() << " Y=" << event->pos().y();
event->accept();
}

virtual void dropEvent(QDropEvent *event)
{
event->accept();
}

virtual void mouseMoveEvent(QMouseEvent *event)
{
if (!(event->buttons() & Qt::LeftButton)) return;
if (currentItem() == NULL) return;

QDrag *drag = new QDrag(this);
QMimeData *mimeData = new QMimeData;

// mime stuff
mimeData->setText(currentItem()->text());
drag->setMimeData(mimeData);
drag->setPixmap(IconFactory::getTablesIcon().pixmap(16,1 6));

// start drag
drag->start(Qt::CopyAction | Qt::MoveAction);
}
private:
ERGraphicsView* grv;
};

DiagramForm::DiagramForm(Version* v, QWidget *parent) : QWidget(parent), ui(new Ui::DiagramForm), ver(v)
{

ui->setupUi(this);

ERGraphicsView *graphicsView;
graphicsView = new ERGraphicsView(this, v);
graphicsView->setObjectName(QString::fromUtf8("graphicsView"));
graphicsView->setDragMode(QGraphicsView::RubberBandDrag);
graphicsView->setAcceptDrops(true);

TableListWidget *lstTables;
lstTables = new TableListWidget(ui->groupBox, graphicsView);
lstTables->setObjectName(QString::fromUtf8("lstTables"));
QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(lstTables->sizePolicy().hasHeightForWidth());
lstTables->setSizePolicy(sizePolicy);
lstTables->setDragDropMode(QAbstractItemView::DragDrop);

ui->verticalLayout_2->addWidget(lstTables);

ui->horizontalLayout->addWidget(graphicsView);

for(int i=0; i<v->getTables().size(); i++)
{
QListWidgetItem* qlwi = new QListWidgetItem(v->getTables()[i]->getName(), lstTables);
qlwi->setIcon(IconFactory::getTablesIcon());
}
}

DiagramForm::~DiagramForm()
{
delete ui;
}

void DiagramForm::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}


and the output:

TabListWidget::dragMove : X= 197 Y= 46
TabListWidget::dragMove : X= 199 Y= 46
TabListWidget::dragMove : X= 201 Y= 45
TabListWidget::dragMove : X= 203 Y= 45
TabListWidget::dragMove : X= 206 Y= 45
TabListWidget::dragMove : X= 208 Y= 45
TabListWidget::dragMove : X= 210 Y= 45
TabListWidget::dragMove : X= 213 Y= 45
TabListWidget::dragMove : X= 216 Y= 45
TabListWidget::dragMove : X= 218 Y= 45
TabListWidget::dragMove : X= 220 Y= 45
TabListWidget::dragMove : X= 222 Y= 45
TabListWidget::dragMove : X= 224 Y= 45
TabListWidget::dragMove : X= 227 Y= 45
TabListWidget::dragMove : X= 229 Y= 44
TabListWidget::dragMove : X= 231 Y= 44
TabListWidget::dragMove : X= 232 Y= 44
TabListWidget::dragMove : X= 234 Y= 44
TabListWidget::dragMove : X= 236 Y= 44
TabListWidget::dragMove : X= 237 Y= 44
TabListWidget::dragMove : X= 238 Y= 44
TabListWidget::dragMove : X= 239 Y= 44
TabListWidget::dragMove : X= 240 Y= 44
TabListWidget::dragMove : X= 241 Y= 44
TabListWidget::dragMove : X= 243 Y= 44
TabListWidget::dragMove : X= 244 Y= 44
TabListWidget::dragMove : X= 245 Y= 44
TabListWidget::dragMove : X= 246 Y= 44
TabListWidget::dragMove : X= 248 Y= 44
TabListWidget::dragMove : X= 250 Y= 44
TabListWidget::dragMove : X= 252 Y= 44
TabListWidget::dragMove : X= 253 Y= 44
GraphicsScene::dragEnter : X= 0 Y= 0
GraphicsScene::dragMove : X= 0 Y= 0
GraphicsScene::dragMove : X= 0 Y= 0
GraphicsScene::dragMove : X= 0 Y= 0
GraphicsScene::dragMove : X= 0 Y= 0
GraphicsScene::dragMove : X= 0 Y= 0


Here I dropped the object and the dropEvent didn't got any usable coordinates ...