PDA

View Full Version : Drag and Drop QGraphicsTextItem



airglide
17th July 2012, 00:44
Hello everyone,

I'm quiet stuck and would really appreciate if someone could help me :)

I have a derived class of QGraphicsScene and /view there are some Items which are derived from QGraphicsTextItem.
If a mousePressEvent happens, I want to be able to move these Items to another position on the scene/view. But if these TextItems are set to editable, I want to set the cursor to the position, where the click happened.

So I overwrote the mousePressEvent function of these TextItems to see if they are set editable or not. But now I don't really know how to do this, I tried to setFlag(QGraphicsItem::ItemIsMovable) but had no success... , I asked me if I should write an own function for Drag and Drop but I didn't found an example simple enough for my problem.

Any Ideas of doing it otherwise are welcome :)

thanks

airglide

wysota
17th July 2012, 01:06
Set the ItemIsMovable when you create the item and don't override mousePressEvent handler for the item.

airglide
17th July 2012, 01:46
allright, but how can I set the cursor with a mouseclick?

thanks

airglide

wysota
17th July 2012, 02:09
It should work out of the box if you set proper flags for the item (Movable and Focusable).

cocophotos
22nd August 2012, 15:35
Hi everyone,

As this thread is not so old, I try to post a question regarding the drag'n'drop with a QGraphicsTextItem (on Qt 4.7).

I followed the Drag'n'drop Robot example to implement the drag'n'drop. But it seems that it does not work on QGraphicsTextItem. I subclassed the TextItem and implemented every useful method.

If I change my parent class to another GraphicsItem such as EllipseItem, the drag'n'drop works, if it's derived from QGraphicsObject (as QGraphicsTextItem does), it's no working. Actually it's just the dropEvent part which is not working at all. The rest is perfectly working. But Qt won't let me to drop (I always have an icon forbidding me to do so) and the program never enters dropEvent in my subclass.

Does someone have an idea regarding that problem?


NodeTreeItem::NodeTreeItem(QGraphicsItem *parent, QGraphicsScene *scene)
:QGraphicsTextItem(parent, scene)
{
//setFlag(QGraphicsItem::ItemIsSelectable);
//setFlag(QGraphicsItem::ItemSendsGeometryChanges);
setAcceptedMouseButtons(Qt::LeftButton);
setAcceptDrops(true);

setCursor(Qt::OpenHandCursor);
}

void NodeTreeItem::dragEnterEvent(QGraphicsSceneDragDro pEvent *event)
{
if(event->mimeData()->hasText())
{
event->setAccepted(true);
update();
dragOver = true;
}
else
event->setAccepted(false);
}

void NodeTreeItem::dragLeaveEvent(QGraphicsSceneDragDro pEvent *event)
{
Q_UNUSED(event);
dragOver = false;
update();
}

void NodeTreeItem::dropEvent(QGraphicsSceneDragDropEven t *event)
{
event->setAccepted(true);
qDebug() << "I dropped it";
dragOver = false;
update();
}

void NodeTreeItem::mousePressEvent(QGraphicsSceneMouseE vent *event)
{
Q_UNUSED(event);
setCursor(Qt::ClosedHandCursor);
}

void NodeTreeItem::mouseMoveEvent(QGraphicsSceneMouseEv ent *event)
{
if (QLineF(event->screenPos(), event->buttonDownScreenPos(Qt::LeftButton))
.length() < QApplication::startDragDistance()) {
return;
}

QDrag *drag = new QDrag(event->widget());
QMimeData *mime = new QMimeData;
mime->setText(this->toPlainText());
drag->setMimeData(mime);
drag->exec();

setCursor(Qt::OpenHandCursor);
}

void NodeTreeItem::mouseReleaseEvent(QGraphicsSceneMous eEvent *event)
{
Q_UNUSED(event);
setCursor(Qt::OpenHandCursor);
}


Enabling ItemIsSelectable or ItemIsMovable does not solve the problem.

Thanks in advance

Added after 36 minutes:

Hi,

It's me again. I figured out the problem. Apparently you have to set the TextInteractionFlag (TextEditorInteraction) to allow the drop.

So problem solved.

Cheers