如何从QGraphicsScene中获取像素点击或在Qt 5中通过鼠标悬停的RGB值?

[英]How could I get the RGB value from QGraphicsScene of a pixel click or hover by mouse in Qt 5?


I'm writing a application. I need to get the position of a image showed in QGraphicsScene, and then get the RGB value of the pixel. How could I do that? It seems that I need to rewrite mouse move event, but I don't know how to do it. It will be great if there is a minimal example.

我正在写一个应用程序。我需要获取QGraphicsScene中显示的图像的位置,然后获取像素的RGB值。我怎么能这样做?看来我需要重写鼠标移动事件,但我不知道该怎么做。如果有一个最小的例子,那就太好了。

My code is hosted at here.

我的代码在这里托管。

1 个解决方案

#1


0  

It seems that I need to rewrite mouse move event, but I don't know how to dot it.

看来我需要重写鼠标移动事件,但我不知道如何点它。

The same way you override any other C++ function. There are tons of examples, and it is an essential part of using the Qt gui C++ API. However, it looks like the one that you need to override is not the mouse move event, but the mouse hover move event:

与覆盖任何其他C ++函数的方式相同。有大量的例子,它是使用Qt gui C ++ API的重要部分。但是,看起来您需要覆盖的不是鼠标移动事件,而是鼠标悬停移动事件:

void hoverMoveEvent(QGraphicsSceneHoverEvent * e) {
  // get pixel value at coordinates e->pos();
}

Conveniently enough, the hover event will give you the position relative to the item.

方便的是,悬停事件将为您提供相对于项目的位置。

You will also have to setAcceptHoverEvents(true) for the graphics item to enable mouse move events.

您还必须为图形项设置setAcceptHoverEvents(true)以启用鼠标移动事件。

Keep in mind you can only get pixel values for QImage. So if you are using a QPixmap item, you will have to do a lot of conversion to QImage each and every time. It will most likely be far more efficient to create your own graphics item that stores a QImage internally, and draws that in its paint event.

请记住,您只能获取QImage的像素值。因此,如果您使用的是QPixmap项目,则必须每次都进行大量的QImage转换。创建自己的内部存储QImage的图形项并在其绘制事件中绘制它可能会更有效。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2017/10/16/44b4d5f6ac2421b8438c132299b9a9f1.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告