gpt4 book ai didi

python - 在小部件中嵌入 QGraphics

转载 作者:行者123 更新时间:2023-12-01 07:15:58 27 4
gpt4 key购买 nike

我的窗口中有一个主小部件,其中包含很多小部件。如何在该小部件中插入 QGraphics View 和 QGraphicsScene?我还没有找到直接插入的方法,所以我尝试使用包装器,在本例中是盒子布局,但这不是一个好的解决方案。 QGraphicsScene 在布局限制中脱颖而出。

代码:

class UiVentana(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setFixedSize(1500, 1015)

widget_central = QtWidgets.QWidget(self)

wrapper = QtWidgets.QHBoxLayout(widget_central)

scene = QtWidgets.QGraphicsScene(wrapper)
vista = QtWidgets.QGraphicsView(scene)

wrapper.addWidget(vista)

self.diedrico = Diedrico() # This is a class who draw things, not relevant
self.diedrico.setFixedSize(2000, 2000)
scene.addWidget(self.diedrico)
self.setCentralWidget(widget_central)

我想得到这个结果:

from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QWidget
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter, QPen, QColor
import sys


class Diedrico(QWidget):
def __init__(self, parent):
super().__init__(parent)

def paintEvent(self, event):
qp = QPainter(self)
qp.setPen(QPen(QColor(Qt.black), 5))
qp.drawRect(500, 500, 1000, 1000)


class UiVentana(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(UiVentana, self).__init__(parent)
self.resize(520, 520)
self.widget_central = QtWidgets.QWidget(self)
scrol = QtWidgets.QScrollArea(self.widget_central)
scrol.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
scrol.setGeometry(QtCore.QRect(30, 30, 500, 500))
scrol.setWidgetResizable(False)

contenido = QtWidgets.QWidget()
contenido.setGeometry(QtCore.QRect(0, 0, 2000, 2000))
scrol.setWidget(contenido)

self.Diedrico = Diedrico(contenido)
self.Diedrico.setGeometry(QtCore.QRect(0, 0, 2000, 2000))
self.setCentralWidget(self.widget_central)


if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ui = UiVentana()
ui.show()
sys.exit(app.exec_())

但是使用 QGraphics 而不是滚动区域

最佳答案

使用窗口小部件创建的 QGraphicsProxyWidget 会考虑窗口小部件的最小尺寸来设置边界矩形,而 QGraphicsScene 使用边界矩形来设置初始场景矩形。

from PyQt5 import QtCore, QtGui, QtWidgets


class Diedrico(QtWidgets.QWidget):
def paintEvent(self, event):
qp = QtGui.QPainter(self)
pen = QtGui.QPen(QtGui.QColor(QtCore.Qt.black), 5)
qp.setPen(pen)
qp.drawRect(500, 500, 1000, 1000)

def minimumSizeHint(self):
return QtCore.QSize(2000, 2000)


class UiVentana(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(UiVentana, self).__init__(parent)
self.resize(520, 520)

widget_central = QtWidgets.QWidget()
self.setCentralWidget(widget_central)

lay = QtWidgets.QVBoxLayout(widget_central)

scene = QtWidgets.QGraphicsScene(self)
view = QtWidgets.QGraphicsView(scene)

diedrico = Diedrico()
scene.addWidget(diedrico)

lay.addWidget(view)


if __name__ == "__main__":
import sys

app = QtWidgets.QApplication(sys.argv)
ui = UiVentana()
ui.show()
sys.exit(app.exec_())

关于python - 在小部件中嵌入 QGraphics,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57946188/

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