이것은 드래그를 사용하려고 시도한 첫 번째 시도입니다 & Qt 드롭 기능. 저는 초심자인데, 과거에 다른 2 Qt 프로그램을 만들었지 만 이번 주에 처음 서브 클래 싱을했습니다.TextItem 용 드래그 앤 드롭을 구현할 수 없습니다
드래그로 이동 가능 QGraphicsTextItem을 Drop하면 해당 QGraphicScene에 연결된 QGraphicsView에 표시됩니다. 그래서 아이템의 새로운 위치를 가져올 수 있습니다.
내가 애니메이션 로봇 예제를 보면서이 링크 한 : http://www.qtcentre.org/threads/50028-Drag-and-Drop-QGraphicsTextItem
위의 링크의 코드는, 내 잘 보았다. 그래서 구현했지만 구현할 때 컴파일러는 모든 종류의 오류를 보여줍니다. 어떻게 극복해야할지 모르겠습니다. 나는 나타나는 ... 오류
예를 어디에서 시작해야할지 모르고 코드의 어떤 부분이 잘못 알고하지 않습니다
:
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;
^
내가 코드를 떠날거야 헤더 :
#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
및 구현 :
#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);
}
@ lazlo-papp 코드가 예상대로 작동하지 않습니다. 장면 주위로 텍스트를 드래그 앤 드롭하고 새로운 위치를 검색하고 싶습니다. 나는 그렇게 어렵다고 믿을 수있다. 위의 코드는 비록 여러분이 제안한대로 적절한 include를 추가 한 후에도 작동하지만, 그렇게 할 수는 없지만 로봇 예제를 살펴 봤지만 GraphicTextItem을 드래그 할 수 있습니다. 너 나 좀 도와 줄 수있어? – user2348235
내가 위에서 언급했듯이, 나는 원하는대로 내 물건을 작동시킬 수 있었다. 그러나 마지막에 정말 쉬웠습니다 : 항목의 생성자에서이 속성을 켜거나 장면에 추가하기 전에 속성을 호출하기 만하면됩니다 : setFlags (QGraphicsItem :: ItemIsMovable | QGraphicsItem :: ItemIsSelectable | QGraphicsItem :: ItemSendGeometryChanges); setAcceptedMouseButtons (Qt :: LeftButton); setAcceptDrops (true); – user2348235