PDA

View Full Version : [QGraphicsView] Size stretch



bspitz
26th April 2011, 11:43
Hi all,

I'm having troubles stretching QGraphicsViews automatically when the size of my window adjusts.

This is the code I have at the moment.


self.leftview = QtGui.QGraphicsView()
self.rightview = QtGui.QGraphicsView()

self.leftview.setFixedSize(550,600)
self.rightview.setFixedSize(550,600)

leftrect = QtCore.QRectF(self.leftview.rect())
rightrect = QtCore.QRectF(self.rightview.rect())
leftrect.adjust(1,1,-1,-1)
rightrect.adjust(1,1,-1,-1)

self.leftscene = TEMDrawer.DiagramScene(leftrect)
self.rightscene = TEMDrawer.DiagramScene(rightrect)

self.leftview.setScene(self.leftscene)
self.rightview.setScene(self.rightscene)


The constructor of the DiagramScene calls the setSceneRect with the given parameter.
No GUI-related stuff other than that happens in the DiagramScene class.

Removing the setFixedSize() method calls do not enable automatic stretching of the QGraphicsView or QGraphicsScene.

Is there a way to automatically stretch a QGraphicsScene or QGraphicsView to fit the space the layout uses?

Thanks in advance!

PS: I had a look at the proposed threads #1 (http://www.qtcentre.org/threads/11901-Problem-determining-size-of-QGraphicsView)#2 (http://www.qtcentre.org/threads/9803-Setting-the-scene-size-in-QGraphicsView)#3 (http://www.qtcentre.org/threads/9498-Size-of-QGraphicsView), but they did not offer a solution for my problem...

AlexSudnik
26th April 2011, 13:40
Do you use a layout for your graphics view ?

I mean ,your widget's constructor might look like this:




self.MyGraphicsScene = QtGui.QGraphicsScene( self );

# add items to the scene

self.MyGraphicsView = QtGui.QGraphicsView( self.MyGraphicsScene );


self.MainLayout = QtGui.QHBoxLayout( self );

self.MainLayout .addWidget( self.MyGraphicsView );



Now your graphics view will be resized as the parent's size changes.

bspitz
28th April 2011, 12:49
Dear Alex, thanks for your reply.

I do add the views to a layout. The code in the first post is followed by:



self.rightlayout.addWidget(self.qlist)
self.rightlayout.addWidget(self.qinfow)
self.qcheckbox = QtGui.QCheckBox("Hide convergences")
self.rightlayout.addWidget(self.qcheckbox)
self.connect(self.qcheckbox, QtCore.SIGNAL('stateChanged(int)'), self.reflexInfo.convergences_toggled)
self.mainlayout.addWidget(self.splitterwidget,5)
self.mainlayout.addLayout(self.rightlayout)
self.setLayout(self.mainlayout)


Do you happen to know what I am doing wrong?

AlexSudnik
28th April 2011, 13:08
Can you share a code ?

bspitz
10th May 2011, 10:23
This is the drawing code:




def __init__(self,usrlist,tsklist,conc_tasklist,parent =None):
super(TEMReflexionWidget,self).__init__(parent)

self.mainlayout = QtGui.QHBoxLayout()
self.leftlayout = QtGui.QVBoxLayout()
self.innerlayout = QtGui.QHBoxLayout()
self.rightlayout = QtGui.QVBoxLayout()

self.leftview = QtGui.QGraphicsView()
self.rightview = QtGui.QGraphicsView()

self.leftview.setFixedSize(550,600)
self.rightview.setFixedSize(550,600)

self.leftview.setFrameStyle(QtGui.QFrame.NoFrame)
self.rightview.setFrameStyle(QtGui.QFrame.NoFrame)
self.leftview.setVerticalScrollBarPolicy(QtCore.Qt .ScrollBarAlwaysOff)
self.leftview.setHorizontalScrollBarPolicy(QtCore. Qt.ScrollBarAlwaysOff)
self.rightview.setVerticalScrollBarPolicy(QtCore.Q t.ScrollBarAlwaysOff)
self.rightview.setHorizontalScrollBarPolicy(QtCore .Qt.ScrollBarAlwaysOff)

leftrect = QtCore.QRectF(self.leftview.rect())
rightrect = QtCore.QRectF(self.rightview.rect())
leftrect.adjust(1,1,-1,-1)
rightrect.adjust(1,1,-1,-1)

self.leftscene = TEMDrawer.DiagramScene(leftrect)
self.rightscene = TEMDrawer.DiagramScene(rightrect)

self.leftview.setScene(self.leftscene)
self.rightview.setScene(self.rightscene)
self.qcheckbox = QtGui.QCheckBox("Hide convergences")
self.reflexInfo = QReflexionReportWidget()
self.qlist = QTaskList(self.reflexInfo,self.conceptual_tasklist ,self.impltasklist,self.leftscene,self.rightscene, self,self.qcheckbox)
self.qinfow = TEMDrawer.TEMInfoWidget()

self.innerlayout.addWidget(self.leftview)
self.innerlayout.addWidget(self.rightview)

self.testwidget = QtGui.QWidget()
self.testwidget.setLayout(self.innerlayout)
self.splitterwidget = QtGui.QSplitter(0)
self.splitterwidget.addWidget(self.testwidget)
self.splitterwidget.addWidget(self.reflexInfo)

self.rightlayout.addWidget(self.qlist)
self.qline = QtGui.QLineEdit()
self.rightlayout.addWidget(QtGui.QLabel("Filter:"))
self.rightlayout.addWidget(self.qline)
self.rightlayout.addWidget(self.qinfow)

self.rightlayout.addWidget(self.qcheckbox)
self.connect(self.qcheckbox, QtCore.SIGNAL('stateChanged(int)'), self.reflexInfo.convergences_toggled)
self.connect(self.qline, QtCore.SIGNAL('textChanged(QString)'), self.qlist.filter_items)
#self.leftlayout.addLayout(self.innerlayout)
self.mainlayout.addWidget(self.splitterwidget,5)
#self.mainlayout.addLayout(self.leftlayout)
self.mainlayout.addLayout(self.rightlayout)
self.setLayout(self.mainlayout)

self.draw_lanes(self.userlist,self.leftscene,0)
self.draw_lanes(self.userlist,self.rightscene,1)