开发者

Display custom image and have the ability to catch mouse input

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 d开发者_运维问答ocumentation 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)?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜