如何在QGraphicsScene中不按Ctrl键选择多个项目?
在Qt的QGraphicsScene
中,如果我想要一个项目,只需单击它,然后单击另一个可选项目将使所选项目取消选择。如果我想选择多个项目,我会使用 Ctrl 键。但这对于某些情况可能不太方便,那么如何在 QGraphicsScene 中不按 Ctrl 键来选择多个项目呢?
In Qt's QGraphicsScene
, if I wanna one item, just click it, and click another selectable item will make the selected item to be unselected. If I want to select multiple items, I'd use Ctrl-key. But this maybe not convenient for some cases, then how to select multiple items without pressing Ctrl-key within QGraphicsScene
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要更改
QGraphicsScene
的默认行为,因此您必须创建自己的场景类,继承QGraphicsScene
。在您的课程中,您必须至少重新实现
mousePressEvent
并自行处理项目选择。以下是您可以如何做到这一点(继承的场景类称为
GraphicsSelectionScene
):以这种方式实现,单击一个项目并选择它(如果尚未选择),否则将取消选择它。
但要小心,实现
mousePressEvent
当然是不够的:如果您不想要默认行为,您还必须处理mouseDoubleClickEvent
。您的场景必须由 QGraphicsView 显示。以下是视图创建自己的场景的示例(
MainFrm
类继承QGraphicsView
):You want to change the default behavior of
QGraphicsScene
, so you have to create your own scene class, inheritingQGraphicsScene
.In your class, you'll have to reimplement at least
mousePressEvent
and handle the item selection yourself.Here is how you could do it (the inherited scene class is called
GraphicsSelectionScene
) :Implementing this way, clicking on an item with select it if it is not already, or will unselect it otherwise.
But be careful, implement
mousePressEvent
is certainly not enough : you'll have to handle themouseDoubleClickEvent
as well if you don't want the default behavior.Your scene will have to be displayed by a
QGraphicsView
. Here is an example of a view creating it's own scene (MainFrm
class is inheritingQGraphicsView
) :也许这是一个黑客,但它对我有用。在此示例中,您可以使用 Shift 键选择多个项目
maybe it's a hack but it's works for me. In this example you can select multiple items using shift key