I’ve been trying to find information about this and haven’t been able to find anyone with this same issue. Basically what I’m trying to do is display a series of arbitrarily arranged frames each having a series of arbitrarily arranged buttons inside. It seems like QGraphicsView/Scene will allow me the freedom to arbitrarily arrange things (unless there’s some other layout that allows this). But when I try to have a QGraphicsView inside another QGraphicsView, none of the inside widgets display.
I’m including test code that shows the basic QGraphicsView with buttons to the left. And a QGraphicsView on the right which contains 4 of those same views which don’t display their buttons.
import sys, math
import PyQt4.QtCore as QtCore
import PyQt4.QtGui as QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class TestApp():
def __init__(self):
pass
def main(self):
app = QtGui.QApplication(sys.argv)
w = QtGui.QWidget()
w.resize(250, 150)
w.move(300, 300)
w.setWindowTitle('Test App')
self.frame1Layout = QHBoxLayout()
self.frame1Layout.addWidget(SelectionWidget())
self.frame1Layout.addWidget(ViewDialog())
self.frame1Layout.setContentsMargins(1,1,1,1)
w.setLayout(self.frame1Layout)
w.show()
sys.exit(app.exec_())
class ViewDialog(QWidget):
def __init__(self, *args):
QtGui.QWidget.__init__(self, *args)
self.setupUI()
self.populateUI()
self.update()
def setupUI(self):
self.viewLayout = QHBoxLayout()
self.view = QtGui.QGraphicsView()
#self.view.setOptimizationFlags(QGraphicsView.DontSavePainterState)
#self.view.setViewportUpdateMode(QGraphicsView.BoundingRectViewportUpdate)
self.scene = QGraphicsScene()
#self.scene.setItemIndexMethod(QGraphicsScene.NoIndex)
self.view.setScene(self.scene)
self.viewLayout.addWidget(self.view)
self.setLayout(self.viewLayout)
def populateUI(self):
for y in range(2):
for x in range(2):
selectionWidget = SelectionWidget()
item = self.scene.addWidget(selectionWidget)
item.setPos(QPointF(x * selectionWidget.width() + 50,
y * selectionWidget.height() + 50) )
#item.setFlag(QGraphicsItem.ItemIsSelectable)
item.setAcceptHoverEvents(True)
self.scene.setSceneRect(0, 0, self.scene.itemsBoundingRect().width(),
self.scene.itemsBoundingRect().height() )
class SelectionWidget(QWidget):
def __init__(self, *args):
super(SelectionWidget, self).__init__(*args)
self.setupWidget()
def setupWidget(self):
self.minWidth = 180
self.minHeight = 60
#self.setFixedSize(self.minWidth, self.minHeight)
self.hbox = QHBoxLayout(self)
self.hbox.setContentsMargins(5,1,5,5)
self.selectionFrame = QGroupBox()
self.selectionFrame.setTitle('selection group')
self.hbox.addWidget(self.selectionFrame)
self.setLayout(self.hbox)
#rnkQt.applyStyle(SelectionWidget, self, "dark", 'blue')
self.selectionScene = QGraphicsScene()
self.selectionView = QGraphicsView()
self.selectionView.setScene(self.selectionScene)
self.selectionView.setFrameStyle(QFrame.NoFrame)
#self.selectionView.setOptimizationFlags(QGraphicsView.DontSavePainterState)
#self.selectionView.setViewportUpdateMode(QGraphicsView.BoundingRectViewportUpdate)
#self.selectionScene.setItemIndexMethod(QGraphicsScene.NoIndex)
selectionFrameLayout = QHBoxLayout(self.selectionFrame)
selectionFrameLayout.setContentsMargins(2,4,2,2)
selectionFrameLayout.addWidget(self.selectionView)
self.setupCtrls()
def setupCtrls(self):
idealWidth = 180
defaultWidth = 20
defaultHeight = 20
defaultSpacing = 5
numCtrls = 14
# figure out how many columns based on overall size
#
numCols = int(math.floor((idealWidth ) / (defaultWidth + defaultSpacing)) )
numRows = int(math.ceil(numCtrls/numCols) )
# create the ctrl widgets
#
# col is combination of widget and spacing column
curCol = 0
curRow = 0
for index in range(numCtrls):
if curCol > numCols:
curCol = 0
curRow +=1
button = QPushButton()
button.setFixedSize(defaultWidth, defaultHeight)
item = self.selectionScene.addWidget(button)
x = (defaultSpacing * (curCol+1) ) + (defaultWidth * curCol)
y = (defaultSpacing * (curRow+1)) + (defaultHeight * curRow)
item.setPos(x, y)
item.setFlag(QtGui.QGraphicsItem.ItemIsSelectable)
item.setAcceptHoverEvents(True)
curCol += 1
self.selectionScene.update(self.selectionScene.sceneRect())
if __name__ == '__main__':
a = TestApp()
a.main()
↧