Question

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);
}
Was it helpful?

Solution

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top