ivo*_*ory 3 qt qpixmap mousepress qgraphicsscene
我正在编写代码以从文件加载图像并对这个图像做了一些编辑(改变一些像素的值),放大或缩小,然后保存图像。另外,我想知道与点击 qgraphicsscen 相关联的原始图像中的位置。到目前为止,我找不到任何有用的功能。
我加载图像的代码:
qgraphicsscene = myqgraphicsview->getScene();
qgraphicsscene->setSceneRect(image->rect());
myqgraphicsview->setScene(qgraphicsscene);
qgraphicsscene->addPixmap(QPixmap::fromImage(*image)); // this is the original image
Run Code Online (Sandbox Code Playgroud)
我的编辑代码:
mousePressEvent(QMouseEvent * e){
QPointF pt = mapToScene(e->pos());
scene->addEllipse(pt.x()-1, pt.y()-1, 2.0, 2.0,
QPen(), QBrush(Qt::SolidPattern));}
Run Code Online (Sandbox Code Playgroud)
我想知道 e->pos() 与原始图像中的确切位置之间的关系。
在 GraphicsView 中接收 mousePressEvent 意味着在 MouseEvent 上调用 pos() 将返回视图坐标空间中的一个点。
此时,您可以使用视图的mapToScene函数将坐标转换为场景空间,然后使用场景的itemAt函数查找被选中的项目。
使用返回的项目,场景坐标可以映射到使用项目的mapFromScene函数单击的项目的本地坐标。
因此,在 GraphicsView 中: -
mousePressEvent(QMouseEvent * e)
{
// get scene coords from the view coord
QPointF scenePt = mapToScene(e->pos());
// get the item that was clicked on
QGraphicsItem item* = qgraphicsscene->itemAt(pt, transform());
// get the scene pos in the item's local coordinate space
QPointF localPt = item->mapFromScene(scenePt);
}
Run Code Online (Sandbox Code Playgroud)
对于带有图像的项目的本地位置,只需将其比例映射到原始图像即可。
虽然您可以这样做,但另一种选择是从存储图像的 Qt 类继承并在其中处理 mousePressEvent。这应该为您提供项目本地空间中的坐标,而无需在场景中查找项目并自己转换坐标。
| 归档时间: |
|
| 查看次数: |
2979 次 |
| 最近记录: |