c – 获取QGraphicsView的可见矩形?
作者:互联网
我一直在用这个把头发拉出来好几个小时.有一个关于它的线程here,但似乎没有任何工作. QGraphicsView :: rect()将返回宽度和高度,但左侧和顶部值未正确设置(始终为0 – 忽略滚动量).我希望它在场景坐标中,但它应该很容易从任何系统翻译.我不知道horizontalScrollBar() – > value()和vert正在返回什么……似乎是毫无意义的乱码.
@ fabrizioM:
// created here
void EditorWindow::createScene() {
m_scene = new EditorScene(this);
m_view = new EditorView(m_scene);
setCentralWidget(m_view);
connect(m_scene, SIGNAL(mousePosChanged(QPointF)), this, SLOT(mousePosChanged(QPointF)));
}
/// with this constructor
EditorView::EditorView(QGraphicsScene* scene, QWidget* parent) : QGraphicsView(scene, parent) {
setRenderHint(QPainter::Antialiasing);
setCacheMode(QGraphicsView::CacheBackground);
setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
setDragMode(QGraphicsView::NoDrag);
scale(1.0, -1.0); // flip coordinate system so that y increases upwards
fitInView(-5, -5, 10, 10, Qt::KeepAspectRatio);
setInteractive(true);
setBackgroundBrush(QBrush(QColor(232,232,232), Qt::DiagCrossPattern));
}
解决方法:
只需使用视图将基于像素的视口矩形映射到场景:
graphicsView->mapToScene(graphicsView->viewport()->geometry()).boundingRect()
再见,
马塞尔
标签:qgraphicsview,c,qt 来源: https://codeday.me/bug/20190930/1836825.html