【问题标题】:QT - Drag and Drop - Creating Custom Graphic object on Custom Scene in Drop's placeQT - 拖放 - 在拖放位置的自定义场景上创建自定义图形对象
【发布时间】:2016-03-02 12:19:12
【问题描述】:

我创建了简单的 d&d drop - 类似于 QT Creator 菜单 - 侧面的 ListView 和 DragScene(我自己的 QGraphicsScene 子类)。我想创建新的图形项目(我已经为它获得了我的自定义 mclasses)添加到那里 - 在我删除它的地方(类似于 QT Designer 的工作方式)。我创建了自己的课程:

DragScene.h

#include <QGraphicsScene>
#include <QDebug>
#include <QMimeData>

#include "CustomObj.h"

class DragScene : public QGraphicsScene
{
public:
    DragScene(QObject* parent = 0);

 protected:
     void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
     void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
     void dragLeaveEvent(QGraphicsSceneDragDropEvent *event);
     void dropEvent(QGraphicsSceneDragDropEvent *event);
};

DragScene.cpp

#include "DragScene.h"

DragScene::DragScene(QObject* parent)
    : QGraphicsScene(parent)
{
}

void DragScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
}

void DragScene::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
{
}

void DragScene::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
{
}

void DragScene::dropEvent(QGraphicsSceneDragDropEvent *event)
{
        CustomObj* newObject = new CustomObj(0,0,50,50);
        newObject->setPos(event->pos().x(), event->pos().y()); //(1)
        this->addItem(newObject);

        qDebug() <<"New object";
}

现在,当我拖动我的项目时 - 它会出现,但总是 @ 点 (0,0)。我不知道这是为什么。更奇怪的是 - 我补充说:

qDebug() << "Moving : "<< event->pos();

void CustomObj::mouseMoveEvent(QGraphicsSceneMouseEvent *event);

我看到它在我移动它时是如何摇晃的:

移动:QPointF(17, 29)

移动:QPointF(17, 29)

移动:QPointF(18, 29)

移动:QPointF(18, 30)

移动:QPointF(17, 30)

移动:QPointF(18, 29)

移动:QPointF(17, 30)

移动:QPointF(17, 30)

移动:QPointF(18, 31)

移动:QPointF(17, 30)

...

它是从创建它之后的第一个动作开始的——就像我得到的那些参数 @start 与那些对象使用的类型完全不同。

目标 1我想初始化 CustomObjs 以使它们出现在我从 List 中删除我的项目的位置。(但到目前为止 - 来自 mevent 的位置始终为 (0, 0) 和鼠标引起的这个对象的第一次移动 - 我可以从 qDebug() 看到位置不是(0,0),但每个元素仍然出现在同一个地方。) 目标2我想控制他们留在场景中(无法越过它的边界)

【问题讨论】:

  • event-&gt;pos().x()event-&gt;pos().y() 真的返回 0, 0 吗?
  • 是的 - 当我在场景中读取 drop 事件时,它总是 (0,0)

标签: c++ qt drag-and-drop scene


【解决方案1】:

目标 1

使用QGraphicsSceneDragDropEvent::​scenePos() 获得场景坐标中的正确位置。

目标 2

CustomObj* newObject = new CustomObj(0,0,50,50);
newObject->setPos(event->scenePos());

QRectF sceneArea = scene->sceneRect(); // QGraphicsScene::sceneRect()
QRectF itemArea = newObject->sceneBoundingRect();

// Test if your item bounding rectangle completely lies inside the scene rectangle.
if (sceneArea.contains(itemArea))
    scene->addItem(newObject);
else
    qDebug() << "Outside the scene";

【讨论】:

  • @2 - 谢谢,效果很好,但我的意思是在移动对象时 - 不添加它。我是否总是想在对这些对象进行操作时使用 scenePos?如果我要将其保存在文件中或添加放大/缩小选项 - 这些仍然是最佳选择吗?
  • QGraphicsSceneDragDropEvent::​scenePos() 为您提供场景坐标中的位置,而QGraphicsSceneDragDropEvent::​pos() 是视口(小部件)坐标中的位置。当您设置 QGraphicsSceneItem 的位置时,您必须使用场景坐标。 doc.qt.io/qt-4.8/graphicsview.html
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-08
  • 2019-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多