将自己的对象放入QGraphicsScene中
我想要一个基于图像的拖放功能。如果我拖放一个图像,我想知道我挑选了哪个图像并与之一起移动(一个简单的 std::string 可以唯一地标识该图像所代表的对象)。我的想法是将我自己的对象(QPixmapItem)存储在 QGraphicsScene 中:
#ifndef QPIXMAPITEM_H
#define QPIXMAPITEM_H
#include <QGraphicsPixmapItem>
#include <QPoint>
class QPixmapItem : public QGraphicsPixmapItem
{
public:
QPixmapItem(std::string path, std::string id, int x, int y);
std::string getIdentifier (){return this->identifier;}
QPoint getPosition () const{return this->position;}
private:
std::string identifier;
QPoint position;
};
#endif // QPIXMAPITEM_H
这是我用来将对象添加到场景中的方法:
void MainWindow::addPixmapItemToScene(std::string path, int x, int y)
{
// generate pixmap item & add it to the scene
QPixmapItem *item = new QPixmapItem(path, std::string("id123"), x, y);
ui->roomView->scene()->addItem(item);
// only update the affected area
ui->roomView->updateSceneRect(item->pixmap().rect());
}
这是我尝试在 QMouseEvent 处“捕获”对象的方法:
void MainWindow::mousePressEvent(QMouseEvent *event)
{
std::cout << "mouse pressed" << std::endl;
QGraphicsPixmapItem *currentItem = dynamic_cast<QGraphicsPixmapItem *>(childAt(event->pos()));
if (!currentItem) {
return;
}
std::cout << "item pressed" << std::endl;
}
对象被添加到场景,但每当我按下它们时,最后一行(“项目按下”)永远不会出现在屏幕上。
I would like to have a drag-and-drop feature based on images. If I drag and drop an image I would like to know which image I picked out and moved with (a simple std::string will do to uniquely identify the object that that image is representing). My thought is to store my own object (QPixmapItem) in the QGraphicsScene:
#ifndef QPIXMAPITEM_H
#define QPIXMAPITEM_H
#include <QGraphicsPixmapItem>
#include <QPoint>
class QPixmapItem : public QGraphicsPixmapItem
{
public:
QPixmapItem(std::string path, std::string id, int x, int y);
std::string getIdentifier (){return this->identifier;}
QPoint getPosition () const{return this->position;}
private:
std::string identifier;
QPoint position;
};
#endif // QPIXMAPITEM_H
This is the method I use to add my objects to the scene:
void MainWindow::addPixmapItemToScene(std::string path, int x, int y)
{
// generate pixmap item & add it to the scene
QPixmapItem *item = new QPixmapItem(path, std::string("id123"), x, y);
ui->roomView->scene()->addItem(item);
// only update the affected area
ui->roomView->updateSceneRect(item->pixmap().rect());
}
Here is how I try to 'catch' the object at a QMouseEvent:
void MainWindow::mousePressEvent(QMouseEvent *event)
{
std::cout << "mouse pressed" << std::endl;
QGraphicsPixmapItem *currentItem = dynamic_cast<QGraphicsPixmapItem *>(childAt(event->pos()));
if (!currentItem) {
return;
}
std::cout << "item pressed" << std::endl;
}
The objects are being added to the scene but whenever I press them, the final line ("item pressed") never makes it onto the screen..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
QGraphicsItem
已经支持在QGraphicsScene
中通过拖放的方式移动。您所需要的只是设置QGraphicsItem::ItemIsMovable
标志。如果您想在发生这种情况时收到通知,请覆盖
QGraphicsItem ::itemChange()
在您的自定义QGraphicsItem
中。QGraphicsItem
already support moving by way of drag-and-drop within aQGraphicsScene
. All you need is set theQGraphicsItem::ItemIsMovable
flag.If you want to get notified when that happens, override
QGraphicsItem::itemChange()
in your customQGraphicsItem
.childAt() 不起作用,因为它返回一个 QWidget,而 QGraphicsPixMapItems 不是 QWidget(因此 childAt() 永远不会返回指向任何类型的 QGraphicsItem 的指针,即使它以某种方式返回,dynamic_cast 转换也会返回 NULL)。
为了获取与场景中指定点相交的 QGraphicsItems 列表,请调用 QGraphicsScene::items() 。然后迭代返回的列表以找出其中有哪些 QGraphicsPixMapItems(如果有)。
childAt() won't work because it returns a QWidget, and QGraphicsPixMapItems are not QWidgets (therefore childAt() will never return a pointer to any kind of QGraphicsItem, and even if it somehow did, the dynamic_cast conversion would return NULL anyway).
In order to get a list of QGraphicsItems that intersect a specified point in the scene, call QGraphicsScene::items() on your scene object. Then iterate through the returned list to find out what QGraphicsPixMapItems (if any) are in it.