显示自定义图像并能够捕获鼠标输入

发布于 2024-11-10 19:19:53 字数 665 浏览 0 评论 0原文

我试图在主窗口中显示多个图像,并能够在这些图像接收 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美胚控场 2024-11-17 19:19:53

如果您想使用QWidget,您应该查看掩模概念来优化图像占用的空间(请注意此实现的性能)。

如果您决定使用QGraphicsItems,您可以重新实现虚拟方法QPainterPath QGraphicsItem::shape () const以返回表示尽可能边界的路径你的图像。

Qt 文档说:

形状有很多用途,包括碰撞检测、命中测试以及 QGraphicsScene::items() 函数。

我鼓励您使用 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 method QPainterPath QGraphicsItem::shape () const to return a path representing as well as possible the boundary of your images.

The Qt documentation says :

The shape is used for many things, including collision detection, hit tests, and for the QGraphicsScene::items() functions.

I encourage you to go with the QGraphics system, it's sounds more appropriate in your case.

半寸时光 2024-11-17 19:19:53

我的理解是,您想要缩小/扩展/变换图像以适应自定义形式(如圆柱体)。
我的建议是使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文