将自己的对象放入QGraphicsScene中

发布于 2024-12-24 03:25:56 字数 1454 浏览 1 评论 0原文

我想要一个基于图像的拖放功能。如果我拖放一个图像,我想知道我挑选了哪个图像并与之一起移动(一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯了 2024-12-31 03:25:56

QGraphicsItem 已经支持在 QGraphicsScene 中通过拖放的方式移动。您所需要的只是设置 QGraphicsItem::ItemIsMovable 标志。

如果您想在发生这种情况时收到通知,请覆盖 QGraphicsItem ::itemChange() 在您的自定义QGraphicsItem中。

QGraphicsItem already support moving by way of drag-and-drop within a QGraphicsScene. All you need is set the QGraphicsItem::ItemIsMovable flag.

If you want to get notified when that happens, override QGraphicsItem::itemChange() in your custom QGraphicsItem.

九局 2024-12-31 03:25:56

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文