简体   繁体   中英

Pyqt: Graphics view next to another widget

I am trying to set up a graphics view next to a custom widget that acts as a container. The graphics view is taking up the entire window. I tried explicitly setting the size but that dose not seem to work. I also added another widget in place of the graphics view and that is working. My Guess is it is something to do with how I am scaling the graphics view. Any ideas?

Min Example File to show the problem I am having with Pyqt and drawing widgets

#Program Stucture
'''
-Main Widget
   |
    - drawing Area
   |
    - Attributes pannel
              |
               - Attributes based on selected node
'''

#import code mods
import sys
from PyQt4 import QtGui, QtCore
from array import *



'''
Check Click events on the scene Object
Also Stores the node data
Not related to issue but needed for demonstration
'''
class Scene(QtGui.QGraphicsScene):

    nodes = []
    connections = []
    selectedNodeID = None

    def __init__(self, x, y, w, h, p):
        super(Scene, self).__init__()
        self.width = w
        self.height = h
        print 'scene created'

    def mousePressEvent(self, e):
        #print("Scene got mouse press event")
        #print("Event came to us accepted: %s"%(e.isAccepted(),))
        QtGui.QGraphicsScene.mousePressEvent(self, e)

    def mouseReleaseEvent(self, e):
        #print("Scene got mouse release event")
        #print("Event came to us accepted: %s"%(e.isAccepted(),))
        QtGui.QGraphicsScene.mouseReleaseEvent(self, e)

    def dragMoveEvent(self, e):
        QtGui.QGraphicsScene.dragMoveEvent(self, e)   


'''
This widget acts as a container for user options. I have added a check box for testing
'''
class sidePannel(QtGui.QWidget):

    attributes = None
    layout = None

    def __init__(self):      
        super(sidePannel, self).__init__()
        self.initUI()

    def initUI(self):
        self.setMinimumSize(1, 30)
        self.layout = QtGui.QVBoxLayout()

        cb = QtGui.QCheckBox('Show title', self)
        self.layout.addWidget(cb)


'''
Main contanier for the scene and sidePannel
'''

class mainWindowWidget(QtGui.QWidget):
    view = None
    scene = None
    appSidePannel = None

    leftFrame = None
    rightFrame = None

    layout = None

    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.initScene()
        self.show()

    def initScene(self):


        #This is what I want but will not work....
        self.scene = Scene(0, 0, 50, 50, self)
        self.view = QtGui.QGraphicsView()
        self.view.setScene(self.scene)


        '''
        #What works to check. This gives me the correct layout. want to use a QGraphicsView instead of QPushButton 
        self.view = QtGui.QPushButton("Button 1", self)
        '''



        self.appSidePannel = sidePannel()

        self.layout = QtGui.QHBoxLayout(self)

        self.leftFrame = self.view
        self.rightFrame = self.appSidePannel

        self.layout.addWidget(self.leftFrame)
        self.layout.addWidget(self.rightFrame)




class MainWindowUi(QtGui.QMainWindow):
    def __init__(self):

        QtGui.QMainWindow.__init__(self)
        self.setWindowTitle('RIS RIB Generator')

        screen = QtGui.QDesktopWidget().screenGeometry()
        self.setGeometry(0, 0, screen.width()/2, screen.height()/2)  

        mainwindowwidget = mainWindowWidget()
        self.setCentralWidget(mainwindowwidget)

        exitAction = QtGui.QAction(QtGui.QIcon('exit24.png'), 'Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Exit application')
        exitAction.triggered.connect(self.close)

        self.statusBar()
        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')

        fileMenu.addAction(exitAction)

'''
Start Point
'''
if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    win = MainWindowUi()
    win.show()
    sys.exit(app.exec_())

You are forgetting to call the setLayout method after you've created the layouts. So in your mainWindowWidget.initScene method it should say:

self.layout = QtGui.QHBoxLayout(self)
self.setLayout(self.layout)

Also add a setLayout to the sidePannel.initUI method and it should work.

BTW: there is no need to define all your attributes as class attributes. Perhaps you should read the following article: http://www.toptal.com/python/python-class-attributes-an-overly-thorough-guide

PS it is common practice to start class definitions with a capital letter. So please rename mainWindowWidget to MainWindowWidget . Idem for sidePannel .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM