QGraphicsView 不在滚动事件上重新绘制

发布于 2024-11-10 01:45:21 字数 1010 浏览 6 评论 0原文

QT 4.7

我有一个 QGraphicsView / QGraphicsScene。该场景有一个自定义的 QGraphicsItem,整个场景不会一次显示,因此它有一个视口。

我将重写 QGraphicsItem 的 Paint() 方法,如下所示:

void paint(QPainter *painter,
           const QStyleOptionGraphicsItem *option,
           QWidget *widget)
{
    painter->setClipRect(option->exposedRect);
    painter->save();

    // Default identity matrix
    QTransform transform;

    // Apply some transform later

    // The next line breaks stuff
    painter->setTransform(transform);

    // m_image is just a preloaded pixmap (i.e. watermark on all of these items).
    painter->drawImage(QPoint(0,0), this->m_image);
    painter->restore();

    // Snip, do more drawing...
}

如果我尝试在 QPainter 上设置 Transform (即,如果我尝试旋转项目),视图将停止重新绘制场景作为对水平或水平方向的响应用于平移的垂直滚动条。当我放大或缩小时,视图也会停止调整场景大小。

如果我调整窗口大小或将窗口拖到屏幕外然后再拖回到屏幕上,视图将会刷新。我一直在查看 QPainter 文档 以及示例,但我无法很清楚我做错了什么。我假设这与坐标系有关。

QT 4.7

I have a QGraphicsView / QGraphicsScene. The scene has a custom QGraphicsItem the whole scene is not displayed at one time, so it has a viewport.

I'm overriding the paint() method of my QGraphicsItem as follows:

void paint(QPainter *painter,
           const QStyleOptionGraphicsItem *option,
           QWidget *widget)
{
    painter->setClipRect(option->exposedRect);
    painter->save();

    // Default identity matrix
    QTransform transform;

    // Apply some transform later

    // The next line breaks stuff
    painter->setTransform(transform);

    // m_image is just a preloaded pixmap (i.e. watermark on all of these items).
    painter->drawImage(QPoint(0,0), this->m_image);
    painter->restore();

    // Snip, do more drawing...
}

If I ever try to setTransform on the QPainter (i.e. if I'm trying to rotate the item), the view stops repainting the scene as a response to the horizontal or vertical scrollbars used to pan. The view also stops resizing the scene when I zoom in or zoom out.

The view will refresh if I resize the window or drag the window offscreen and then back onscreen. I've been looking over the QPainter Documentation as well as the examples and I can't quite figure out what I'm doing wrong. I'm assuming it's something to do with the coordinate system.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

゛清羽墨安 2024-11-17 01:45:21

猜测:

paint 方法中的 QPainter 已经进行了转换,该转换考虑了视口属性(缩放、旋转等)。当您在绘制方法中调用 setTransform 时,您将彻底摆脱这一切。您可能希望对现有变换执行矩阵运算,而不是创建新的变换。

由于您在画家上调用 setClipRect ,但随后尝试在完全不同的变换下进行绘画,因此您正在剪辑矩形之外进行绘画,并且什么也没有发生。

当您调整窗口大小或将窗口拖离屏幕时,它会起作用,因为这会强制“重绘所有内容”,因此您的剪辑矩形在备用变换中包含您的绘画区域。尽管我很惊讶它会出现在正确的位置。

A guess:

The QPainter that comes to your paint method already has a transform on it that takes into account the viewport attributes (scale, rotation, etc.). When you call setTransform within your paint method you are blowing all that away. You probably want to perform a matrix operation on the existing transform rather than creating a new one.

Since you are calling setClipRect on your painter but then trying to paint under a completely different transform, you are painting outside your clip rect and nothing is happening.

It works when you resize or drag the window off screen because that forces a "redraw everything," so your clip rect includes your painting area in your alternate transform. Although I'm surprised it would appear in the correct location.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文