[英]Pyqt: Graphics view next to another widget
我試圖在充當容器的自定義窗口小部件旁邊設置圖形視圖。 圖形視圖占據了整個窗口。 我試圖明確設置大小,但似乎不起作用。 我還添加了另一個小部件來代替圖形視圖,並且該小部件正在運行。 我的猜測是,這與我縮放圖形視圖的方式有關。 有任何想法嗎?
最小示例文件,以顯示我在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
添加到sidePannel.initUI
方法中,它應該可以工作。
順便說一句:不需要將所有屬性都定義為類屬性。 也許您應該閱讀以下文章: http : //www.toptal.com/python/python-class-attributes-an-overly-thorough-guide
PS:通常的做法是使用大寫字母開始類定義。 因此,請將mainWindowWidget
重命名為MainWindowWidget
。 同上sidePannel
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.