[英]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: 如果要禁用多个选择,建议您执行以下操作:
lastSelection
to a QGraphicsItem around 子类化QGraphicsScene,并保持一个指针lastSelection
到周围的QGraphicsItem QGraphicsScene::selectionChanged
创建一个连接到QGraphicsScene::selectionChanged
的插槽 selectedItems
: 检查selectedItems
:
lastSelection
: nothing to do (=selection didn't really change) 仅包含lastSelection
:无事可做(=选择并没有真正改变) lastSelection
: set lastSelection
to that item (=one item selected for the first time) 包含一个项目,而不是lastSelection
:将lastSelection
设置lastSelection
项目(=第一次选择一个项目) 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: 禁用多项选择的简单方法是:
QGraphicsItem
. 从QGraphicsItem
创建自己的Dirived类。 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.