gpt4 book ai didi

python - QGraphicsAnchorLayout 未锚定在 QGraphicsScene 中

转载 作者:行者123 更新时间:2023-12-01 00:30:17 27 4
gpt4 key购买 nike

我目前在理解 QGraphicsAnchorLayout 在 QGraphicsScene 中的行为时遇到问题。我创建了 4 个框并锚定了每个框的角,但似乎没有正确应用 anchor ,或者至少没有像我认为的那样应用正确。

黄色框应该始终位于 QGraphicsScene 的左上角,即使 GraphicsView 展开时也是如此。蓝色框被锚定为与右侧的黄色框相邻,其顶部与 QGraphicsScene/视口(viewport)的顶部重合。

绿色框的左上角锚定到蓝色框的右下角,同样红色框锚定到绿色框。但这就是我得到的:

enter image description here

我希望黄色框始终位于图形场景/视口(viewport)的顶部。我希望即使向右滚动它也始终保持可见,但我相信这可能是一个单独的问题。但是,当我垂直扩展窗口时,所有框都居中,包括我希望保留在顶部的黄色框。

蓝色、绿色和红色框似乎与我应用的 anchor 没有相似之处。

以下是我用来生成此代码的代码。这些 anchor 如何工作以及我可以采取什么措施来纠正这个问题?

import numpy as np
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from debug_utils import *
from PyQt5.QtWidgets import QGraphicsAnchorLayout, QGraphicsWidget, QGraphicsLayoutItem

def qp(p):
return "({}, {})".format(p.x(), p.y())

class box(QtWidgets.QGraphicsWidget):
pressed = QtCore.pyqtSignal()

def __init__(self, rect, color, parent=None):
super(box, self).__init__(parent)
self.raw_rect = rect
self.rect = QtCore.QRectF(rect[0], rect[1], rect[2], rect[3])
self.color = color

def boundingRect(self):
pen_adj = 0
return self.rect.normalized().adjusted(-pen_adj, -pen_adj, pen_adj, pen_adj)

def paint(self, painter, option, widget):
r = self.boundingRect()

brush = QtGui.QBrush()
brush.setColor(QtGui.QColor(self.color))
brush.setStyle(Qt.SolidPattern)

#rect = QtCore.QRect(0, 0, painter.device().width(), painter.device().height())
painter.fillRect(self.boundingRect(), brush)
painter.setRenderHint(QtGui.QPainter.Antialiasing)
painter.setPen(Qt.darkGray)

painter.drawRect(self.boundingRect())
#painter.drawRect(0, 0, max_time*char_spacing, self.bar_height)

def mousePressEvent(self, ev):
self.pressed.emit()
self.update()

def mouseReleaseEvent(self, ev):
self.update()

class GraphicsView(QtWidgets.QGraphicsView):
def __init__(self, parent=None):
super(GraphicsView, self).__init__(parent)
scene = QtWidgets.QGraphicsScene(self)
self.setScene(scene)
self.numbers = []
self.setMouseTracking(True)

l = QGraphicsAnchorLayout()
l.setSpacing(0)
w = QGraphicsWidget()
#painter = QtGui.QPainter(self)
w.setPos(0, 0)
w.setLayout(l)
scene.addItem(w)
self.main_widget = w
self.main_layout = l

self.makeBoxs()

def makeBoxs(self):
rect = [0, 0, 600, 250]
blue_box = box(rect, QtGui.QColor(0, 0, 255, 128))
green_box = box(rect, QtGui.QColor(0, 255, 0, 128))
red_box = box([0, 0, 200, 50], QtGui.QColor(255, 0, 0, 128))
yellow_box_left = box([0, 0, 75, 600], QtGui.QColor(255, 255, 0, 128))
#self.scene().setSceneRect(blue_box.rect)
#self.scene().setSceneRect(bar_green.rect)

# Adding anchors adds the item to the layout which is part of the scene
self.main_layout.addCornerAnchors(yellow_box_left, Qt.TopLeftCorner, self.main_layout, Qt.TopLeftCorner)
self.main_layout.addCornerAnchors(blue_box, Qt.TopLeftCorner, yellow_box_left, Qt.TopRightCorner)
self.main_layout.addCornerAnchors(green_box, Qt.TopLeftCorner, blue_box, Qt.BottomRightCorner)
self.main_layout.addCornerAnchors(red_box, Qt.TopLeftCorner, green_box, Qt.BottomRightCorner)

#self.main_layout.addAnchor(bar_green, Qt.AnchorTop, blue_box, Qt.AnchorBottom)
#self.main_layout.addAnchor(bar_green, Qt.AnchorLeft, blue_box, Qt.AnchorRight)

def printStatus(self, pos):
msg = "Viewport Position: " + str(qp(pos))
v = self.mapToScene(pos)
v = QtCore.QPoint(v.x(), v.y())
msg = msg + ", Mapped to Scene: " + str(qp(v))
v = self.mapToScene(self.viewport().rect()).boundingRect()
msg = msg + ", viewport Mapped to Scene: " + str(qp(v))
v2 = self.mapToScene(QtCore.QPoint(0, 0))
msg = msg + ", (0, 0) to scene: " + qp(v2)
self.parent().statusBar().showMessage(msg)

def mouseMoveEvent(self, event):
pos = event.pos()
self.printStatus(pos)
super(GraphicsView, self).mouseMoveEvent(event)

def resizeEvent(self, event):
self.printStatus(QtGui.QCursor().pos())
h = self.mapToScene(self.viewport().rect()).boundingRect().height()
r = self.sceneRect()
r.setHeight(h)

height = self.viewport().height()
for item in self.items():
item_height = item.boundingRect().height()

super(GraphicsView, self).resizeEvent(event)

class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)

gv = GraphicsView()
self.setCentralWidget(gv)
self.setGeometry(475, 250, 600, 480)
scene = self.scene = gv.scene()
sb = self.statusBar()

def main():
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

if __name__ == "__main__":
main()

编辑:添加预期输出根据 anchor 的定义方式,我希望输出如下所示。由于我还无法实际创建我需要的内容,因此我在 PowerPoint 中创建了它。但是,当然,除了让它发挥作用之外,我还希望了解如何在更一般的意义上使用 anchor 。

enter image description here

编辑2:再次感谢您的更新。这并不完全符合我的预期,当我滚动时会出现奇怪的现象。只是为了澄清,

  1. 我希望黄色小部件始终可见,位于具有最高 z 顺序的视口(viewport)左上角。我认为您提供了这一点。
  2. 所有其他小部件都应该滚动,保持其相对 anchor 。我删除了蓝色框到黄色框的 anchor ,但所有框都左对齐。
  3. 在您当前的实现中,非黄色框不会滚动,但是当我向右和向左调整滚动条大小或移动滚动条时,我会看到此工件:

enter image description here

最佳答案

您有 2 个错误:

  • 您的 Box 类构建得很差,它只设置大小,而不是覆盖boundingRect,因为位置将由布局处理。

  • 布局的位置始终相对于它所在的小部件。在您的情况下,您希望 main_layout 的左上角与视口(viewport)的左上角匹配,因此您必须修改 main_widget 的左上角以匹配。

考虑到上述情况,解决方案是:

from PyQt5 import QtCore, QtGui, QtWidgets


def qp(p):
return "({}, {})".format(p.x(), p.y())


class Box(QtWidgets.QGraphicsWidget):
pressed = QtCore.pyqtSignal()

def __init__(self, size, color, parent=None):
super(Box, self).__init__(parent)
self.setMinimumSize(size)
self.setMaximumSize(size)
self.color = color

def paint(self, painter, option, widget):
brush = QtGui.QBrush()
brush.setColor(QtGui.QColor(self.color))
brush.setStyle(QtCore.Qt.SolidPattern)

painter.fillRect(self.boundingRect(), brush)
painter.setRenderHint(QtGui.QPainter.Antialiasing)
painter.setPen(QtCore.Qt.darkGray)

painter.drawRect(self.boundingRect())

def mousePressEvent(self, event):
self.pressed.emit()
super().mousePressEvent(event)


class GraphicsView(QtWidgets.QGraphicsView):
messageChanged = QtCore.pyqtSignal(str)

def __init__(self, parent=None):
super(GraphicsView, self).__init__(parent)
self.setMouseTracking(True)
self.setAlignment(QtCore.Qt.AlignTop | QtCore.Qt.AlignLeft)
scene = QtWidgets.QGraphicsScene(self)
self.setScene(scene)

l = QtWidgets.QGraphicsAnchorLayout()
l.setSpacing(0)
w = QtWidgets.QGraphicsWidget()
self.scene().sceneRectChanged.connect(self.update_widget)
self.horizontalScrollBar().valueChanged.connect(self.update_widget)
self.verticalScrollBar().valueChanged.connect(self.update_widget)
w.setLayout(l)
scene.addItem(w)
self.main_widget = w
self.main_layout = l

self.makeBoxs()

def makeBoxs(self):
blue_box = Box(QtCore.QSizeF(300, 125), QtGui.QColor(0, 0, 255, 128))
green_box = Box(QtCore.QSizeF(300, 125), QtGui.QColor(0, 255, 0, 128))
red_box = Box(QtCore.QSizeF(100, 25), QtGui.QColor(255, 0, 0, 128))
yellow_box = Box(QtCore.QSizeF(37.5, 300), QtGui.QColor(255, 255, 0, 128))

# yellow_box_left top-left
self.main_layout.addCornerAnchors(
yellow_box,
QtCore.Qt.TopLeftCorner,
self.main_layout,
QtCore.Qt.TopLeftCorner,
)
self.main_layout.addCornerAnchors(
blue_box, QtCore.Qt.TopLeftCorner, yellow_box, QtCore.Qt.TopRightCorner
)
self.main_layout.addCornerAnchors(
green_box, QtCore.Qt.TopLeftCorner, blue_box, QtCore.Qt.BottomRightCorner
)
self.main_layout.addCornerAnchors(
red_box, QtCore.Qt.TopLeftCorner, green_box, QtCore.Qt.BottomRightCorner
)

# self.setSceneRect(self.scene().itemsBoundingRect())

def update_widget(self):
vp = self.viewport().mapFromParent(QtCore.QPoint())
tl = self.mapToScene(vp)
geo = self.main_widget.geometry()
geo.setTopLeft(tl)
self.main_widget.setGeometry(geo)

def resizeEvent(self, event):
self.update_widget()
super().resizeEvent(event)

def mouseMoveEvent(self, event):
pos = event.pos()
self.printStatus(pos)
super(GraphicsView, self).mouseMoveEvent(event)

def printStatus(self, pos):
msg = "Viewport Position: " + str(qp(pos))
v = self.mapToScene(pos)
v = QtCore.QPoint(v.x(), v.y())
msg = msg + ", Mapped to Scene: " + str(qp(v))
v = self.mapToScene(self.viewport().rect()).boundingRect()
msg = msg + ", viewport Mapped to Scene: " + str(qp(v))
v2 = self.mapToScene(QtCore.QPoint(0, 0))
msg = msg + ", (0, 0) to scene: " + qp(v2)
self.messageChanged.emit(msg)


class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)

gv = GraphicsView()
self.setCentralWidget(gv)

self.setGeometry(475, 250, 600, 480)

gv.messageChanged.connect(self.statusBar().showMessage)


def main():
import sys

app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())


if __name__ == "__main__":
main()

enter image description here

关于python - QGraphicsAnchorLayout 未锚定在 QGraphicsScene 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58241248/

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