简体   繁体   English

如何禁用qgraphicsitem的多项选择?

[英]How to disable the multiple selection of qgraphicsitem?

I am new to Qt. 我是Qt的新手。 It seems the default for multiple selection of qgraphicsitem is to press ctrl button. 似乎多次选择qgraphicsitem的默认设置是按ctrl按钮。 But is it possible to disable this function? 但是可以禁用此功能吗? Or reload this function? 还是重新加载此功能?

This is controlled by the items' flags. 这由项目的标志控制。 To disable selection for a particular item, do 要禁用对特定项目的选择,请执行

item->setFlag(QGraphicsItem::ItemIsSelectable, false);

If you want to completly disable selecting items for a QGraphicsScene regardless of the item flags I would recommend to connect QGraphicsScene::selectionChanged to QGraphicsScene::clearSelection . 如果要完全禁用QGraphicsScene选择项,而不管项目标志如何,我建议将QGraphicsScene::selectionChanged连接到QGraphicsScene::clearSelection

If you want to disable multiple selection I suggest the following: 如果要禁用多个选择,建议您执行以下操作:

  • Subclass QGraphicsScene and keep a pointer lastSelection to a QGraphicsItem around 子类化QGraphicsScene,并保持一个指针lastSelection到周围的QGraphicsItem
  • Create a slot connected to QGraphicsScene::selectionChanged 创建一个连接到QGraphicsScene::selectionChanged的插槽
  • Check selectedItems : 检查selectedItems
    • it's empty: nothing to do (=nothing selected) 它是空的:无事可做(=未选择任何内容)
    • contains only lastSelection : nothing to do (=selection didn't really change) 仅包含lastSelection :无事可做(=选择并没有真正改变)
    • contains one item, not lastSelection : set lastSelection to that item (=one item selected for the first time) 包含一个项目,而不是lastSelection :将lastSelection设置lastSelection项目(=第一次选择一个项目)
    • contains two items: One must be lastSelection . 包含两个项目:一个必须是lastSelection Remove that one from the selection ( lastSelection->setSelected(false); ), set lastSelection to the remaining item. 从选择项中删除该项( lastSelection->setSelected(false); ),将lastSelection设置为其余项。 (=another item was selected, move selection to it) (=已选择另一个项目,将选择移至该项目)

You might need to block signals during modifying the selection inside the slot. 在修改插槽内的选择期间,您可能需要屏蔽信号。

The simple way to disable multiple selection is: 禁用多项选择的简单方法是:

  1. Create your own Dirived class from QGraphicsItem . QGraphicsItem创建自己的Dirived类。
  2. Overload the protected mousePressEvent function and disable ControlModifier : 重载受保护的mousePressEvent函数并禁用ControlModifier

     protected: void YourOwnQGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) Q_DECL_OVERRIDE { if(mouseEvent->modifiers() & Qt::ControlModifier) { mouseEvent->ignore(); } else { QGraphicsItem::mousePressEvent(mouseEvent); //Do what you want... } } 

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

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