Python - PyQT - 如何在单击像素图中的文本/点时检测鼠标事件 [英] Python - PyQT - How to detect mouse events when clicking text/points in a pixmap

查看:609
本文介绍了Python - PyQT - 如何在单击像素图中的文本/点时检测鼠标事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在包含QPixmap的QLabel中的(x,y)位置绘制了简单文本(比如字母'x')和一些字体参数(如大小20字体等)。当点击精确发生在这些绘制的x之一上时,我需要覆盖哪些相关方法来检测鼠标事件。

Suppose I have drawn simple text (say just the letter 'x') with some font parameters (like size 20 font, etc.) onto an (x,y) location in a QLabel that holds a QPixmap. What are the relevant methods that I will need to override in order to detect a mouse event when a click occurs "precisely" over one of these drawn x's.

我的第一直觉是查找绘制点的存储(x,y)位置,如果鼠标当前位置在该(x,y)位置周围的小盒子内,那么我将执行一些功能,例如允许用户调整该点,如果没有,则执行正常的鼠标事件功能。然而,我正在与之交互的点是人脸上的跟踪特征,如眼睛等。通常,当有人用相机等以奇怪的角度转动时,两个不同的跟踪点可能几乎在每个上面。其他。因此,无论用户如何关注所需的关键点,如果鼠标事件处理中的逻辑标准存在一定的容差,则会出现逻辑认为点击两个不同点的情况。

My first instinct is to lookup the stored (x,y) locations of drawn points and if the mouse current position is inside a tiny box around that (x,y) position, then I will execute some functionality, like allowing the user to adjust that point, and if not then execute normal mouse event functionality. However, the points I am interacting with are tracked features on human faces, like eyes, etc. Often, when someone is turning at an odd angle with the camera, etc., two distinct tracked points can be pretty much right on top of each other. Thus, no matter how well the user focuses on the desired key point, if there is some tolerance in the logic criteria in mouse event handling, there will be cases where the logic believes two different points are being clicked.

我希望尽量减少此类问题,而不会对点击条件产生不合理的精确度。有没有一些根本不同的方式来解释文本的选择(因为,当文本被绘制到像素图时,是否会成为该像素图的任何类型的属性,它知道文本已经被绘制在(x,y)并且因此用户必须尝试点击什么?)

I want to minimize this sort of issue without making unreasonable precision of the click criteria. Is there some fundamentally different way to interpret selection of text (as in, when text is drawn to a pixmap, does there become any sort of attribute of that pixmap that is aware that text has been drawn at (x,y) and so that's what the user must be trying to click on?)

非常感谢PyQT中此类事情的任何建议或示例。

Any advice or examples of this sort of thing in PyQT would be greatly appreciated.

推荐答案

虽然您在前一个问题中提到过您的用户互动计划,但现在很清楚图形视图框架可能更适合您的目标。

Although you alluded to your plans for user interaction in your previous question, it is now clear that the Graphics View Framework may be more appropriate for what you are trying to do.

这与小部件中的绘制明显不同。使用此框架,您可以创建由图形项(QGraphicsItem子类)组成的场景,然后将场景分配给视图。通过视图,您可以与场景中的项目进行交互。他们可以生成点击事件甚至被拖动。文档很广泛,看起来很复杂,但从概念上讲,我认为它更容易理解。

This is distinctly different from drawing in a widget. With this framework, you create a scene composed of graphic items (QGraphicsItem subclasses) and then assigne the scene to a view. Through the view, you can interact with the items in the scene. They can generate click events and even be dragged around. The documentation is extensive and looks complicated but conceptually, I think it is easier to understand.

这篇关于Python - PyQT - 如何在单击像素图中的文本/点时检测鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆