I'd like to define two class in pyside.
The first one:StartFrame is the MainWindow. The second one:HelpPanel is a part of GUI.
I try to import HelpPanel into StartFrame and output it. But the result is an empty window, no HelpPanel in it.
Here is my code:
import sys
from PySide import QtGui
from PySide import QtCore
from PySide.QtCore import Signal as pyqtSignal
from PySide.QtCore import Slot as pyqtSlot
class StartFrame(QtGui.QMainWindow):
def __init__(self, parent = None):
super(StartFrame, self).__init__(parent)
self.parent = parent
self.initialize()
def initialize(self):
self.hp = HelpPanel(self)
grid = QtGui.QGridLayout()
grid.addWidget(self.hp, 1, 0)
self.setLayout(grid)
self.show()
class HelpPanel(QtGui.QWidget):
def __init__(self, parent):
super(HelpPanel,self).__init__(parent)
self.parent = parent
bsize = QtGui.QVBoxLayout()
title = QtGui.QLabel('Help Information')
text_area = QtGui.QTextEdit()
text_area.setReadOnly(True)
bsize.addWidget(title)
bsize.addStretch(0)
bsize.addWidget(text_area)
bsize.addStretch(1)
self.setLayout(bsize)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = StartFrame()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
If without StartFrame, the HelpPanel itself can run and get output. I suspect that the problem happens on self.hp = HelpPanel(self). But I'm not sure. Thanks in advance.
1 Answer 1
I'm not sure why PySide doesn't give a warning, but if you run this code under PyQt you get this message:
QWidget::setLayout: Attempting to set QLayout "" on StarteFrame "", which already has a layout
The problem is that the QMainWindow
class comes with its own layout already, and you are trying to overwrite it. One way to overcome this is to place your layout inside a new widget and use QMainWindow::setCentralWidget()
to assign it to you instance of QMainWindow
Your new initialize
function would look something like this:
def initialize(self):
self.hp = HelpPanel(self)
grid = QtGui.QGridLayout()
grid.addWidget(self.hp)
widget = QtGui.QWidget()
widget.setLayout(grid)
self.setCentralWidget(widget)
self.show()