获取QGraphicsView的可见矩形? [英] Get visible rectangle of QGraphicsView?

查看:251
本文介绍了获取QGraphicsView的可见矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在拉着我的头发,用这个小时。有一个主题这里,但没有什么似乎是工作。 QGraphicsView :: rect()将返回宽度和高度,但顶部值未正确设置(总是0 - 忽略滚动量)。我想在场景坐标,但它应该是足够容易翻译从任何系统。我不知道 horizo​​ntalScrollBar() - > value()和vert正在返回...似乎是无意义的混乱。

I've been pulling my hair out with this one for hours. There's a thread here about it, but nothing seems to be working. QGraphicsView::rect() will return the width and height, but the left and top values aren't set properly (always 0 -- ignoring the scrolled amount). I want it in scene coordinates, but it should be easy enough to translate from any system. I have no idea what horizontalScrollBar()->value() and vert are returning...seems to be meaningless jibberish.


@ 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));
}


推荐答案

这个似乎工作了。

QRectF EditorView::visibleRect() {
    QPointF tl(horizontalScrollBar()->value(), verticalScrollBar()->value());
    QPointF br = tl + viewport()->rect().bottomRight();
    QMatrix mat = matrix().inverted();
    return mat.mapRect(QRectF(tl,br));
}

这篇关于获取QGraphicsView的可见矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆