zoukankan      html  css  js  c++  java
  • QT Graphics-View图元组使用

    通过把一个item作为另一个item的孩子,你可以得到item组的大多数本质特性:这些items会一起移动,所有变换会从父到子传递。QGraphicsItem也可以为它的孩子处理所有的事件,这样就允许以父亲代表它所有的孩子,可以有效地把所有的items看作一个整体。

    另外,QGraphicsItemGroup是一个特殊的item,它既对孩子事件进行处理又有一个接口把items从一个组中增加和删除。把一个item加到 QGraphicsItemGroup仍会保留item的原始位置与变换,而给一个item重新指定父item则会让item根据其新的父亲重新定位。可以用QGraphicsScene::createItemGroup()建组。

     

    1、通过父子关系-如果想要将 items 存储在其他 item 内,可以直接将任何 QGraphicsItem 通过为 setParentItem() 传递一个合适的 parent。

    注意: 对于该方式,QGraphicsItem 可以有自己的子 item 对象。但是,QGraphicsItem 没有 API(例如:setItems()、addChild())添加孩子,它只能允许孩子附加到 parent (setParentItem()),想想也挺神奇的。

     // Item parent-children

    QGraphicsRectItem*  pRectItemTmp  = new QGraphicsRectItem(QRectF(-100.0, -100.0, 50.0, 50.0));

    QGraphicsEllipseItem*   pEllipseItemTmp = new QGraphicsEllipseItem(QRectF(-100.0, -100.0, 50.0, 50.0));

    pEllipseItemTmp->setParentItem(pRectItemTmp);

    pScene->addItem(pRectItemTmp);

    pRectItemTmp->setFlag(QGraphicsItem::ItemIsSelectable);

    pRectItemTmp->setFlag(QGraphicsItem::ItemIsMovable);

     

    2、QGraphicsItemGroup(图元组)是一个容器,它的作用是将加入到该组里的图元当成一个图元来看待。QGraphicsItemGroup的父类是QGraphicsItem,所以它本质上也是一个图元,只是这个图元本身是不可见的。

    QGraphicsItemGroup有两种创建方法:

    一种是手动创建QGraphicsItemGroup对象然后再加入到场景中。

    另一种是使用场景类的createItemGroup方法创建,该方法返回一个QGraphicsItemGroup对象。

    // Item Group

    // [1]

    QGraphicsRectItem* pRectItem  = new QGraphicsRectItem(QRectF(-30.0, -30.0, 60.0, 60.0));

    QGraphicsEllipseItem* pEllipseItem = new QGraphicsEllipseItem(QRectF(-30.0, -30.0, 60.0, 60.0));

    QGraphicsItemGroup* pItemGroup = new QGraphicsItemGroup();

    pItemGroup->setFlag(QGraphicsItem::ItemIsSelectable);

    pItemGroup->setFlag(QGraphicsItem::ItemIsMovable);

    pItemGroup->addToGroup(pRectItem);

    pItemGroup->addToGroup(pEllipseItem);

    pScene->addItem(pItemGroup);

     

    // [2]

    QGraphicsRectItem* pRectItemEx  = new QGraphicsRectItem(QRectF(-30.0, -30.0, 60.0, 60.0));

    QGraphicsEllipseItem* pEllipseItemEx = new QGraphicsEllipseItem(QRectF(-30.0, -30.0, 60.0, 60.0));

    QList<QGraphicsItem*> pItemList;

    pItemList.append(pRectItemEx);

    pItemList.append(pEllipseItemEx);

    QGraphicsItemGroup* pItemGroupEx = pScene->createItemGroup(pItemList);

    pItemGroupEx->setFlag(QGraphicsItem::ItemIsSelectable);

    pItemGroupEx->setFlag(QGraphicsItem::ItemIsMovable);

     

    图元组可以使用addToGroup将图元添加到组里,使用removeFromGroup将图元从组里移除。其他的操作就把它当成QGraphicsItem来看待。如果想要销毁组,可以使用场景类的destroyItemGroup方法即可。

    添加和删除Item的操作保留了Item的场景相对位置和转换,相反,调用setParentItem(),其中仅保留子项目的父项相对位置和转换。

     

    QGraphicsItem 分组比较简单,但在分组之后 group 中的 QGraphicsItem 无法捕获自己的相关事件(例如:鼠标事件、键盘事件),实际接受消息对象为 QGraphicsItemGroup。

    让 QGraphicsItemGroup 中的 item 处理自己的事件

    查看QGraphicsItemGroup源码

    QGraphicsItemGroup::QGraphicsItemGroup(QGraphicsItem *parent)

                     : QGraphicsItem(*new QGraphicsItemGroupPrivate, parent)

    {

             setHandlesChildEvents(true);

    }

     

    setHandlesChildEvents

    This function is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.

    If enabled is true, this item is set to handle all events for all its children (i.e., all events intented for any of its children are instead sent to this item); otherwise, if enabled is false, this item will only handle its own events. The default value is false.

    This property is useful for item groups; it allows one item to handle events on behalf of its children, as opposed to its children handling their events individually.

    If a child item accepts hover events, its parent will receive hover move events as the cursor passes through the child, but it does not receive hover enter and hover leave events on behalf of its child.

  • 相关阅读:
    mysql5.6版本修改密码
    ASP.NET Core MVC 运行所选代码生成器时出错
    abp ef codefirst 设置默认值
    resharper激活
    Ioc(控制反转)、DI(依赖注入)
    统计mysql数据库中数据表/字段等数量的sql
    HeidiSQL安装和使用教程
    mysql的安装步骤
    项目分层的好处
    Python IDLE 的使用与调试
  • 原文地址:https://www.cnblogs.com/MakeView660/p/10829772.html
Copyright © 2011-2022 走看看