简体   繁体   English

QGraphicsItem中的itemChanged()用于许多不同的项目

[英]itemChanged() in QGraphicsItem for a many different items

I have an application containing many different items in a QGraphisScene . 我有一个包含QGraphisScene中许多不同项目的应用程序。 These items would be rectangles, ellipses, pixmaps, or anything that inherits from QGraphicsItem . 这些项目将是矩形,椭圆,像素图或任何从QGraphicsItem继承的东西。 The user can move around these items. 用户可以移动这些项目。 (each of the QGraphicsItem::ItemIsMovable flag is set). (设置了每个QGraphicsItem::ItemIsMovable标志)。

The application needs to get an event (a callback, a signal or whatever) to get the new position. 应用程序需要获取事件(回调,信号或其他)才能获得新位置。

How can I overload the itemChanged() method for all of these possible items in one go?. 如何itemChanged()为所有这些可能的项重载itemChanged()方法? I'd like do avoid subclassing each of my possible items (ie doing a derived class for QGraphicsEllipseItem , and onother for QGraphicsPixmapItem and a subclass for any future items...)? 我想就避免继承我的每一个可能的项目(即做派生类QGraphicsEllipseItem ,并onother为QGraphicsPixmapItem和未来的任何物品子类)?

I would like to be able to tell: Each time a QGraphicsItem (or anything derived from it) changes, call my function: 我希望能够告诉:每次QGraphicsItem (或从它派生的任何东西)发生变化时,请调用我的函数:

 my_item_changed(QGraphicItem* the_change_item,...).

And then being able to add different item types without having to worry about that any longer... 然后能够添加不同的项目类型,而不必再担心...

Any hint? 任何提示?

You can install an event filter on the QGraphicsItems. 您可以在QGraphicsItems上安装事件过滤器。 In particular, you'll want to use this function: - 特别是,您将要使用此功能: -

void QGraphicsItem::installSceneEventFilter(QGraphicsItem * filterItem);

As the Qt documentation states, here's an example of its usage: - 正如Qt文档所述,这是一个使用它的例子: -

QGraphicsScene scene;
QGraphicsEllipseItem *ellipse = scene.addEllipse(QRectF(-10, -10, 20, 20));
QGraphicsLineItem *line = scene.addLine(QLineF(-10, -10, 20, 20));

line->installSceneEventFilter(ellipse);
// line's events are filtered by ellipse's sceneEventFilter() function.

ellipse->installSceneEventFilter(line);
// ellipse's events are filtered by line's sceneEventFilter() function.

Based on this, create a class, derived from QGraphicsItem, which can receive the events first. 在此基础上,创建一个派生自QGraphicsItem的类,它可以先接收事件。 For each item you add to the scene, call the installSceneEventFilter: - 对于添加到场景中的每个项目,请调用installSceneEventFilter: -

mySceneEventItem.installSceneEventFilter(pGraphicsItem);

Next, your eventFilter object overrides the function: - 接下来,您的eventFilter对象会覆盖该函数: -

bool QGraphicsItem::sceneEventFilter(QGraphicsItem * watched, QEvent * event)
{
    if(event->type() == QEvent::GraphicsSceneMove)
    {
        emit my_item_changed(watched); // signal that the item was moved
    }

    return false; // pass the event to the original target item
}

This allows you to check events and handle those that you're interested in. If you return false from sceneEventFilter, the event will be passed onto the original object, after you've dealt with it; 这允许您检查事件并处理您感兴趣的事件。如果您从sceneEventFilter返回false,则事件将在您处理完之后传递给原始对象; returning true will block the event from being passed on. 返回true将阻止事件被传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM