Pyqt:另一个小部件旁边的图形视图

Pyqt: Graphics view next to another widget

我正在尝试在用作容器的自定义小部件旁边设置图形视图。图形视图占据了整个 window。我尝试明确设置大小,但似乎不起作用。我还添加了另一个小部件来代替图形视图,并且可以正常工作。我猜这与我缩放图形视图的方式有关。有任何想法吗?

显示我在使用 Pyqt 和绘图小部件时遇到的问题的最小示例文件

#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_())

您忘记在创建布局后调用 setLayout 方法。所以在你的 mainWindowWidget.initScene 方法中它应该说:

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

另外在 sidePannel.initUI 方法中添加一个 setLayout,它应该可以工作。

顺便说一句:没有必要将所有属性都定义为 class 属性。也许您应该阅读以下文章:http://www.toptal.com/python/python-class-attributes-an-overly-thorough-guide

P.S。通常的做法是 class 定义以大写字母开头。所以请将 mainWindowWidget 重命名为 MainWindowWidgetsidePannel.

同上