本文介绍了当 ItemIgnoresTransformations 打开时,QGraphicsView 的 fitInView 出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试实现项目大小不改变但项目之间的距离被放大的场景时,我在使用以下绘制矩形和文本A"的代码时遇到了这个问题.现在,如果我在矩形项目上设置标志 ItemIgnoresTransformations,放大会导致矩形消失(在矩形周围单击并拖动鼠标按钮).但是在文本的情况下不会发生这种情况.此外,fitInView 设置的新视口区域与我要求的非常不同:

While trying to implement a scene where item sizes do not change but distances between items get magnified I encountered this problem with the following code which draws a rectangle and the text "A". Now if I set the flag ItemIgnoresTransformations on the rectangle item, zooming in causes the rectangle to vanish (click and drag mouse button around the rectangle). But that does not happen in case of the text. Also, the new viewport area set by fitInView is very different for I asked for:

import sys
from PyQt4 import QtCore, QtGui

class GV(QtGui.QGraphicsView):
    def __init__(self, *args, **kwargs):
        QtGui.QGraphicsView.__init__(self, *args, **kwargs)

    def mousePressEvent(self, event):
        pos = QtCore.QPoint(event.pos())
        self.startPos = pos

    def mouseReleaseEvent(self, event):
        pos = QtCore.QPoint(event.pos())
        self.endPos = pos
        rect = QtCore.QRect(self.startPos, self.endPos)
        sceneRect = self.mapToScene(rect).boundingRect()
        print 'Selected area: viewport coordinate:', rect, \
            ', scene coordinate:', sceneRect
        self.fitInView(sceneRect)
        print 'new viewport in scene coordinates:', \
            self.mapToScene(self.viewport().geometry()).boundingRect()

class Scene(QtGui.QGraphicsScene):
    def __init__(self, *args, **kwargs):
        QtGui.QGraphicsScene.__init__(self, *args, **kwargs)
        self.itemA = QtGui.QGraphicsSimpleTextItem('A')
        self.itemA.setPos(20, 20)
        self.itemA.setFlag(QtGui.QGraphicsItem.ItemIgnoresTransformations, True)
        self.addItem(self.itemA)
        self.itemB = QtGui.QGraphicsRectItem(30, 50, 20, 20)
        self.addItem(self.itemB)
        self.itemB.setFlag(QtGui.QGraphicsItem.ItemIgnoresTransformations, True)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    widget = QtGui.QMainWindow()
    scene = Scene()
    view = GV(scene, widget)
    widget.setCentralWidget(view)
    widget.show()
    app.exec_()

任何解释将不胜感激!

推荐答案

事实上,矩形并没有消失.但它奇怪地"移动.

In fact, the rectangle does not vanish. But it moves around "strangely".

self.itemB = QtGui.QGraphicsRectItem(30, 50, 20, 20)

这一行可能不是您想要的.这将创建一个项目并在本地坐标中放置一个从 (30, 50) 开始的矩形/正方形.然后将其添加到场景中.这会给你一个锚定在 (0, 0) 的项目,跨越 (50, 70) 但只在右下角绘制一个矩形 20x20.

This line may not be what you want. This creates an item and puts a rectangle/square starting from (30, 50) in local coordinates. Then you add this to the scene. This gives you an item anchored at (0, 0), spans up to (50, 70) but draws a rectangle only in the bottom right 20x20.

当您设置 ItemIgnoresTransformations 时,项目无法在缩放时进行常规转换.场景放大,对于忽略这种变换的项目,它有点缩小"自己.但它仍然锚定在 (0, 0) 并且矩形在右下角,所以绘制的矩形向左上角移动.

When you set ItemIgnoresTransformations, item can't do its regular transformations in case of a zoom. Scene zooms in, for item to ignore this transformation, it kind of "shrinks" itself. But it's still anchored at (0, 0) and the rectangle is at the bottom-right, so the drawn rectangle moves toward the upper-left.

解决方案相当简单.不要在本地坐标中创建矩形,即矩形应该从 (0, 0) 开始,并且应该明确定位它.转化为:

Solution is rather simple. Don't create your rectangle in local coordinates, i.e. your rectangle should start from (0, 0) and you should position it explicitly. Which translates to this:

self.itemB = QtGui.QGraphicsRectItem(0, 0, 20, 20)
self.itemB.setPos(30, 50)

这篇关于当 ItemIgnoresTransformations 打开时,QGraphicsView 的 fitInView 出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 03:17