PDA

View Full Version : Drag from QtreeWidget and drop to QGraphicsView



syjgin
3rd July 2010, 14:17
I'm trying to give a user the ability to drag items from the QTreeWidget and move it to the QGraphicsView - the icon of current item must be transformated to the QGraphicsItem's pixmap, the text of current item must become an remark to that graphics item. But currently I coudn't catch the drop event - in the following code I had reimplemented "dragEnter", "drop" and "mousePressed" event, but the Drop event's listener invokes only one time and mousePressed event isn't tracked. Maybe someone know how to correctly handle drag and drop between different widgets on the same form?
Here are sample of my code:


void addcondition::dragEnterEvent ( QDragEnterEvent * event )
{
event->acceptProposedAction();
}

void addcondition::dropEvent ( QDropEvent * event )
{
if(m_ui->toolBox->currentIndex()==0)
//just current page of toolbox item - this isn't important
{
//QGraphicsScene scene has declared in the header file
scene.addText(event->mimeData()->text());
m_ui->gcond->setScene(&scene);
m_ui->gcond->show();
}
event->acceptProposedAction();
}
void addcondition::mousePressEvent( QMouseEvent * event)
{
if (event->button() == Qt::LeftButton)
{
QDrag *drag = new QDrag(this);
QMimeData *mimeData = new QMimeData;
if(m_ui->toolBox->currentIndex()==0)
mimeData->setText(m_ui->cond_view->currentItem()->text(0));
drag->setMimeData(mimeData);
Qt::DropAction dropAction = drag->exec();
qDebug()<<"mouse event detected";
//this event will never be tracked. I don't know, why
}
}