gpt4 book ai didi

Python Qt 绑定(bind) : setCosmetic() and sceneRect(), 边距问题

转载 作者:行者123 更新时间:2023-11-28 19:20:36 26 4
gpt4 key购买 nike

使用以下简单示例(适用于 PySide 或 PyQt4):

import sys
import random
import numpy
from PySide import QtGui, QtCore

class Window(QtGui.QWidget):

def __init__(self):
super(Window, self).__init__()

self.resize(600, 400)
self.view = QtGui.QGraphicsView()
self.scene = QtGui.QGraphicsScene()
self.view.setScene(self.scene)
self.setWindowTitle('Example')

# Layout
layout = QtGui.QGridLayout()
layout.addWidget(self.view, 0, 0)
self.setLayout(layout)

# Styles
self.pen = QtGui.QPen(QtCore.Qt.black, 0, QtCore.Qt.SolidLine)
self.brush = QtGui.QBrush(QtGui.QColor(255, 255, 255, 0))

def addLine(self, x0, y0, x1, y1):
line = QtCore.QLineF(x0, -y0, x1, -y1)
pen = QtGui.QPen(QtGui.QColor(0, 0, 255, 255), 0, QtCore.Qt.SolidLine)
l = self.scene.addLine(line, pen)

def addRect(self, left, top, width, height):
rect = QtCore.QRectF(left, -top, width, abs(height))
r = self.scene.addRect(rect, self.pen, self.brush)

def fit(self):
self.view.fitInView(self.scene.sceneRect())

def resizeEvent(self, event = None):
self.fit()


if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
window.addLine(-1, -1, 2, 2)
window.addLine(0, 1, 1, 0)
window.addRect(0, 1, 1, 1)
window.fit()
sys.exit(app.exec_())

我可以画一个矩形和两条穿过它的蓝线。请注意,使用宽度为 0QtGui.QPen 如何使蓝线具有恒定的宽度,无论正方形/线条的大小或窗口的大小如何:

enter image description here enter image description here

那是因为默认情况下,零宽度笔是装饰性的。根据 Qt 文档:

Cosmetics pens are used to draw strokes that have a constant width regarless of any transformations applied to the QPainter they are used with. Drawing a shape with a cosmetic pen ensures that its outline will have the same thickness at different scale factors.

非零宽度的笔也可以设置为修饰,使用方法setCosmetic(True)。因此,将示例的 addLine() 方法中的笔宽设置为 2 并将笔设置为修饰:

    def addLine(self, x0, y0, x1, y1):
line = QtCore.QLineF(x0, -y0, x1, -y1)
pen = QtGui.QPen(QtGui.QColor(0, 0, 255, 255), 2, QtCore.Qt.SolidLine)
pen.setCosmetic(True)
l = self.scene.addLine(line, pen)

给出以下输出:

enter image description here enter image description here

如您所见,边距很大,我真的希望交叉线在窗口的左下角和右上角开始和结束,没有边距。

添加这些边距似乎是因为 scene.sceneRect() 被修改,好像该行不是装饰性的。请参见本例,其中宽度也设置为 2,但笔未设置为装饰品:

    def addLine(self, x0, y0, x1, y1):
line = QtCore.QLineF(x0, -y0, x1, -y1)
pen = QtGui.QPen(QtGui.QColor(0, 0, 255, 255), 2, QtCore.Qt.SolidLine)
l = self.scene.addLine(line, pen)

enter image description here

为什么会这样?不应该仅在 isCosmetic() == False 时添加额外的边距吗?如果此行为是故意的,有人可以解释原因吗?

还有,有什么办法可以避免吗? “干净”的东西,不同于在将线添加到场景之前手动更改线的边界(或不同于稍后从场景中减去额外的边距)。也许有配置参数或其他方式将线添加到场景中?

编辑

将帽样式设置为“扁平”会导致边距变小,但问题仍然存在:

    def addLine(self, x0, y0, x1, y1):
line = QtCore.QLineF(x0, -y0, x1, -y1)
pen = QtGui.QPen(QtGui.QColor(0, 0, 255, 255), 2, QtCore.Qt.SolidLine)
pen.setCosmetic(True)
pen.setCapStyle(QtCore.Qt.FlatCap)
l = self.scene.addLine(line, pen)

enter image description here

再一次,我们可以看到页边距与使用非化妆笔时的效果相同:

enter image description here

最佳答案

这不是答案,但我认为它可能有帮助:

我是用 C++ 做的,但它很容易翻译。在您的 QGraphicsView 中,设置滚动条策略:

view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

然后在您对 fitInView 的调用中,添加以下标志:

view->fitInView(scene->sceneRect(), Qt::KeepAspectRatioByExpanding);

关于Python Qt 绑定(bind) : setCosmetic() and sceneRect(), 边距问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26231374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com