显示自定义图像并能够捕获鼠标输入
我试图在主窗口中显示多个图像,并能够在这些图像接收 mousePressEvent 时执行某些操作。
为此,我想创建一个从 QWidget 派生的新类,它有一个 QImage 私有成员。这样,我就能够重写paintEvent和mousePressEvent来显示图像并捕获鼠标输入。
然而,问题是在主窗口中绘制的图像具有窗口小部件的大小及其形状(矩形),并且图像不具有相同的形状(甚至不是常规形状)。这会导致一些问题,因为我能够在不属于我的图像但属于小部件区域的部分上捕获 mousePressEvent。
有谁对如何解决这个问题有任何想法? 我对 QT 还很陌生,所以请不要介意任何巨大的错误:)
更新: 好吧,我尝试了另一种方法。
现在,我的主窗口上有一个图形视图,并有一个与之链接的图形场景。在这个场景中,我添加了一些图形项目。
为了实现我的graphicItems,我必须重载boundingRect 和paint 方法。就文档而言,我的理解是QT使用shape方法(调用boundingRect)来确定要绘制的对象的形状。这可能是我的问题的解决方案,或者至少是提高绘制形状准确性的一种解决方案。
截至目前,我正在返回一个正方形,其图像大小位于boundingRect中。有谁知道我如何“调整”形状以适应我的图像(类似于圆柱体)?
I am trying to display several images in the MainWindow and have the ability to perform certain actions when these images receive mousePressEvent.
For this, I thought I would create a new class, derived from QWidget, which had a QImage private member. With this, I as able to override the paintEvent and the mousePressEvent to both display the image and catch mouse input.
However, the problem is that the image drawn in the MainWindow has the widget size and its form (rectangular), and the image does not possess the same form (not even a regular form). This causes some problems because I am able to catch the mousePressEvent on parts that do not belong to my Image, but do belong to the Widget area.
Does anyone have any ideas on how to solve this?
I am still very new to QT, so don't please don't mind any huge mistake :)
Update:
Ok, I tried another approach.
I now have a graphicView on my MainWindow, with a graphicScene linked to it. To this scene, I added some graphicItems.
To implement my graphicItems I had to overload the boundingRect and paint method. As far as the documentation goes, my understanding is that QT uses shape method (which calls boundingRect) to determine the shape of the object to draw. This could possibly be the solution to my problem, or at least one solution to improve the accuracy of the drawn shape.
As of now, i am returning a square with my image size in boundingRect. Does anyone know how I can "adapt" the shape to my image (which resembles a cylinder)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想使用
QWidget
,您应该查看掩模概念来优化图像占用的空间(请注意此实现的性能)。如果您决定使用
QGraphicsItems
,您可以重新实现虚拟方法QPainterPath QGraphicsItem::shape () const
以返回表示尽可能边界的路径你的图像。Qt 文档说:
我鼓励您使用
QGraphics
系统,它听起来更适合您的情况。If you want to use
QWidget
you should take look at the mask concept to refine the space occupied by your images (be careful about the performance of this implementation).If you decide to go with the
QGraphicsItems
you can re-implement the virtual methodQPainterPath QGraphicsItem::shape () const
to return a path representing as well as possible the boundary of your images.The Qt documentation says :
I encourage you to go with the
QGraphics
system, it's sounds more appropriate in your case.我的理解是,您想要缩小/扩展/变换图像以适应自定义形式(如圆柱体)。
我的建议是使用 OpenGL 模块 QGLWidget 并将矩形图像 (0,0) (0,1) (1,0) (1,1) 的每个坐标映射到在 OpenGL 中创建的自定义表单。
它将充当纹理映射。
编辑:您仍然可以在 OpenGL 中捕获鼠标输入
What I understand is that you want to shrink/expand/transform your image to fit a custom form (like a cylinder).
What I suggest is to use the OpenGL module QGLWidget and to map each coordinate of your rectangular image (0,0) (0,1) (1,0) (1,1) to a custom form created in OpenGL.
It will act as a texture mapping.
Edit: you can still catch mouse input in OpenGL