Issue
I am looking to develop a Qt application whose main/parent widget is capable of 'holding' one or more "special" widgets. Each special widget displays a separate QGraphicsView and QGraphicsScene.
As far as I can see there can only be one central widget to which a graphics view can be set i.e. QMainWindow::setCentralWidget( QGraphicsView )
, whereas I would like to have multiple graphics views.
Here is my failed quick and dirty attempt (in PySide)
#!/usr/bin/python
from PySide import QtGui
import sys
class MyApp(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
layout = QtGui.QHBoxLayout()
self.scene1 = QtGui.QGraphicsScene(self)
self.scene1.addRect(5,5,200,200)
self.view1 = QtGui.QGraphicsView(self.scene1, self)
self.scene2 = QtGui.QGraphicsScene(self)
self.scene2.addLine(500,500,300,300)
self.view2 = QtGui.QGraphicsView(self.scene1, self)
layout.addWidget(self.view1)
layout.addWidget(self.view2)
self.setLayout(layout)
self.show()
if __name__=="__main__":
app=QtGui.QApplication(sys.argv)
myapp = MyApp()
sys.exit(app.exec_())
Solution
QMainWindow has a default layout, you do not have to set it. In Qt a QWidget can be used as a container, so we create a widget that will be the container of the QGraphicsViews and also the centralwidget:
class MyApp(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MyApp, self).__init__(parent)
self.scene1 = QtGui.QGraphicsScene(self)
self.scene1.addRect(5,5,200,200)
self.view1 = QtGui.QGraphicsView(self.scene1)
self.scene2 = QtGui.QGraphicsScene(self)
self.scene2.addLine(500,500,300,300)
self.view2 = QtGui.QGraphicsView(self.scene2)
central_widget = QtGui.QWidget()
layout = QtGui.QHBoxLayout(central_widget)
layout.addWidget(self.view1)
layout.addWidget(self.view2)
self.setCentralWidget(central_widget)
self.show()
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.