1

this is my first try trying to use Drag&Drop feature of Qt. I'm a beginner, I made my first subclassing this week although I have made other 2 Qt programs in the past.

I need a movable by Drag&Drop QGraphicsTextItem to show on a QGraphicsView that is connected to the corresponding QGraphicScene. So I can retrieve the new position of the item.

I have looked at the animated robot example and this link: http://www.qtcentre.org/threads/50028-Drag-and-Drop-QGraphicsTextItem

The code of the link above, looked well for my. So I reimplemented it but when building, the compiler shows all kind of errors I´m not sure how to overcome. I don't know where to start, and don't know what piece of code is incorrect...

examples of errors appearing:

error: no matching function for call to 'GraphicsTextItem::setCursor(Qt::CursorShape)'
     setCursor(Qt::OpenHandCursor);
                                 ^

error: invalid use of incomplete type 'class QGraphicsSceneDragDropEvent'
     if(event->mimeData()->hasText())
             ^

error: forward declaration of 'class QGraphicsSceneDragDropEvent'
 class QGraphicsSceneDragDropEvent;
   ^

I'll leave the code:

Header:

#ifndef GRAPHICSTEXTITEM_H
#define GRAPHICSTEXTITEM_H

#include <QGraphicsTextItem>

class GraphicsTextItem : public QGraphicsTextItem
{
    Q_OBJECT

public:
    GraphicsTextItem(QGraphicsItem *parent = 0);

protected:
    void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
    void dragLeaveEvent(QGraphicsSceneDragDropEvent *event);
    void dropEvent(QGraphicsSceneDragDropEvent *event);
    void mousePressEvent(QGraphicsSceneMouseEvent *);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *);
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);

private:
    bool dragOver;
};
//! [0]





#endif // GRAPHICSTEXTITEM_H

and the Implementation:

#include <graphicstextitem.h>
#include <QDrag>
//#include <QGraphicsScene>


GraphicsTextItem::GraphicsTextItem(QGraphicsItem *parent)
    :QGraphicsTextItem(parent)
{
    //setFlag(QGraphicsItem::ItemIsSelectable);
    setFlag(QGraphicsItem::ItemIsMovable);
    setTextInteractionFlags(Qt::TextEditorInteraction);
    setAcceptedMouseButtons(Qt::LeftButton);
    setAcceptDrops(true);

    setCursor(Qt::OpenHandCursor);
}



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


void GraphicsTextItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
{
    Q_UNUSED(event);
    dragOver = false;
    update();
}


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


void GraphicsTextItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    Q_UNUSED(event);
    setCursor(Qt::ClosedHandCursor);
}


void GraphicsTextItem::mouseMoveEvent(QGraphicsSceneMouseEvent *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 GraphicsTextItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    Q_UNUSED(event);
    setCursor(Qt::OpenHandCursor);
}
rene
  • 41,474
  • 78
  • 114
  • 152
  • @lazlo-papp The code did't work as I expected. I want to drag & drop some text around a scene and retrieve the new position. I can believe it is so difficult. The code above, although it worked after adding the proper includes as you suggested: cannot do that, I have looked at the robot example, but I can manage to be able to drag my GraphicTextItem around. Can you help me? – user2348235 May 12 '14 at 01:44
  • As I commented above, I was able to make my items work as desired. But it was really easy at the end: It is only needed to turn on this properties in the item's constructor, or to call them before adding it to a scene: setFlags(QGraphicsItem::ItemIsMovable|QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemSendsGeometryChanges); setAcceptedMouseButtons(Qt::LeftButton); setAcceptDrops(true); – user2348235 May 12 '14 at 12:06

1 Answers1

1

The error message is telling you that there is a forward declaration somewhere in your code, although that is not true based on the code you have shown so far.

Either way, for accessingt members of your heap object, you need more than just forward declaration. You are missing this include:

 #include <QGraphicsSceneDragDropEvent>
László Papp
  • 51,870
  • 39
  • 111
  • 135