QGraphicsScene 忽略网格布局的延伸
QGraphicsScene ignores Stretches of Grid Layout
我有一个分为两个子小部件的小部件。一个显示图片,另一个提供编辑图片的选项。图片子小部件的水平 space 应该是编辑子小部件的 5 倍。我在网格布局中指定了这个约束。但是,只要我将 QGraphicsScene(和视图)添加到编辑子小部件,约束就会被忽略。
我尝试了几种尺寸政策,但其中 none 符合我的要求,但我对这些政策的使用经验不多。
class MapWidget(QWidget):
""" Widget that renders the map and displays properties """
def __init__(self, main_gui, parent=None):
super().__init__(parent=parent)
self.main_gui = main_gui
# (Re-)Build the ui
self.layout = QGridLayout()
self.setLayout(self.layout)
self.map_scene = QGraphicsScene()
self.map_scene_view = QGraphicsView()
self.map_scene_view.setViewport(QGLWidget())
self.map_scene_view.setScene(self.map_scene)
self.layout.addWidget(self.map_scene_view, 1, 1, 5, 1)
blocks_container = QVBoxLayout()
self.layout.addLayout(blocks_container, 1, 2, 1, 1)
"""
Here I add other widgets to the blocks_container,
however none of those causes problems
"""
# As soon as I also add this group, both widgets have the same size
group_blocks = QGroupBox('Blocks')
self.blocks_scene = QGraphicsScene()
self.blocks_scene_view = QGraphicsView()
self.blocks_scene_view.setViewport(QGLWidget())
self.blocks_scene_view.setScene(self.blocks_scene)
group_blocks_layout = QHBoxLayout()
group_blocks.setLayout(group_blocks_layout)
group_blocks_layout.addWidget(self.blocks_scene_view)
blocks_container.addWidget(group_blocks)
两个小部件的比例不是 5/6 和 1/6,而是两个小部件共享相同的大小,这使得图片看起来很小而编辑小部件太大。此外,一旦我删除 'group_blocks' 小部件,编辑子小部件就会再次具有适当的大小。因此,为简单起见,我排除了它包含的所有其他小部件。
尝试并根据需要进行调整:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtOpenGL import *
class MapWidget(QWidget):
""" Widget that renders the map and displays properties """
def __init__(self, main_gui="???", parent=None): # main_gui="???"
super().__init__(parent=parent)
self.main_gui = main_gui
# (Re-)Build the ui
self.layout = QGridLayout()
self.setLayout(self.layout)
self.map_scene = QGraphicsScene()
self.map_scene_view = QGraphicsView()
self.map_scene_view.setViewport(QGLWidget())
self.map_scene_view.setScene(self.map_scene)
# self.layout.addWidget(self.map_scene_view, 1, 1, 5, 1)
self.layout.addWidget(self.map_scene_view, 1, 1, 5, 5)
blocks_container = QVBoxLayout()
# self.layout.addLayout(blocks_container, 1, 2, 1, 1)
self.layout.addLayout(blocks_container, 1, 6, 5, 1)
self.layout.setColumnMinimumWidth(1, 320) # +++
self.layout.setRowMinimumHeight(1, 200) # +++
self.layout.setColumnStretch(1, 5) # +++
self.layout.setColumnStretch(6, 1) # +++
"""
Here I add other widgets to the blocks_container,
however none of those causes problems
"""
# As soon as I also add this group, both widgets have the same size
group_blocks = QGroupBox('Blocks')
self.blocks_scene = QGraphicsScene()
self.blocks_scene_view = QGraphicsView()
self.blocks_scene_view.setViewport(QGLWidget())
self.blocks_scene_view.setScene(self.blocks_scene)
group_blocks_layout = QHBoxLayout()
group_blocks.setLayout(group_blocks_layout)
group_blocks_layout.addWidget(self.blocks_scene_view)
blocks_container.addWidget(group_blocks)
if __name__=="__main__":
app = QApplication(sys.argv)
myapp = MapWidget()
myapp.show()
sys.exit(app.exec_())
我有一个分为两个子小部件的小部件。一个显示图片,另一个提供编辑图片的选项。图片子小部件的水平 space 应该是编辑子小部件的 5 倍。我在网格布局中指定了这个约束。但是,只要我将 QGraphicsScene(和视图)添加到编辑子小部件,约束就会被忽略。
我尝试了几种尺寸政策,但其中 none 符合我的要求,但我对这些政策的使用经验不多。
class MapWidget(QWidget):
""" Widget that renders the map and displays properties """
def __init__(self, main_gui, parent=None):
super().__init__(parent=parent)
self.main_gui = main_gui
# (Re-)Build the ui
self.layout = QGridLayout()
self.setLayout(self.layout)
self.map_scene = QGraphicsScene()
self.map_scene_view = QGraphicsView()
self.map_scene_view.setViewport(QGLWidget())
self.map_scene_view.setScene(self.map_scene)
self.layout.addWidget(self.map_scene_view, 1, 1, 5, 1)
blocks_container = QVBoxLayout()
self.layout.addLayout(blocks_container, 1, 2, 1, 1)
"""
Here I add other widgets to the blocks_container,
however none of those causes problems
"""
# As soon as I also add this group, both widgets have the same size
group_blocks = QGroupBox('Blocks')
self.blocks_scene = QGraphicsScene()
self.blocks_scene_view = QGraphicsView()
self.blocks_scene_view.setViewport(QGLWidget())
self.blocks_scene_view.setScene(self.blocks_scene)
group_blocks_layout = QHBoxLayout()
group_blocks.setLayout(group_blocks_layout)
group_blocks_layout.addWidget(self.blocks_scene_view)
blocks_container.addWidget(group_blocks)
两个小部件的比例不是 5/6 和 1/6,而是两个小部件共享相同的大小,这使得图片看起来很小而编辑小部件太大。此外,一旦我删除 'group_blocks' 小部件,编辑子小部件就会再次具有适当的大小。因此,为简单起见,我排除了它包含的所有其他小部件。
尝试并根据需要进行调整:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtOpenGL import *
class MapWidget(QWidget):
""" Widget that renders the map and displays properties """
def __init__(self, main_gui="???", parent=None): # main_gui="???"
super().__init__(parent=parent)
self.main_gui = main_gui
# (Re-)Build the ui
self.layout = QGridLayout()
self.setLayout(self.layout)
self.map_scene = QGraphicsScene()
self.map_scene_view = QGraphicsView()
self.map_scene_view.setViewport(QGLWidget())
self.map_scene_view.setScene(self.map_scene)
# self.layout.addWidget(self.map_scene_view, 1, 1, 5, 1)
self.layout.addWidget(self.map_scene_view, 1, 1, 5, 5)
blocks_container = QVBoxLayout()
# self.layout.addLayout(blocks_container, 1, 2, 1, 1)
self.layout.addLayout(blocks_container, 1, 6, 5, 1)
self.layout.setColumnMinimumWidth(1, 320) # +++
self.layout.setRowMinimumHeight(1, 200) # +++
self.layout.setColumnStretch(1, 5) # +++
self.layout.setColumnStretch(6, 1) # +++
"""
Here I add other widgets to the blocks_container,
however none of those causes problems
"""
# As soon as I also add this group, both widgets have the same size
group_blocks = QGroupBox('Blocks')
self.blocks_scene = QGraphicsScene()
self.blocks_scene_view = QGraphicsView()
self.blocks_scene_view.setViewport(QGLWidget())
self.blocks_scene_view.setScene(self.blocks_scene)
group_blocks_layout = QHBoxLayout()
group_blocks.setLayout(group_blocks_layout)
group_blocks_layout.addWidget(self.blocks_scene_view)
blocks_container.addWidget(group_blocks)
if __name__=="__main__":
app = QApplication(sys.argv)
myapp = MapWidget()
myapp.show()
sys.exit(app.exec_())