为什么我的pyside自定义小部件无法在其父窗口小部件中显示?

时间:2015-01-06 10:14:55

标签: pyside

我想将多个标准pyside小部件组合成一个新的小部件(我的自定义小部件,名为“RasterWidget”),然后在另一个小部件(名为RastersCrlWidget)中显示更多自定义小部件。但奇怪的是,当我运行应用程序时,小部件不会出现在其父窗口小部件中。我将自定义小部件更改为'QtGui.QPushButton',然后一切正常。当单独显示时,自定义小部件可以正常工作(不要将它放在另一个小部件中)。我想知道为什么我的自定义小部件不能显示标准的'QtGui.QPushButton'?

from PySide import QtCore, QtGui
import sys

class RasterWidget(QtGui.QWidget):

    def __init__(self):
        super(RasterWidget, self).__init__()
        self.ui = Ui_Raster()
        self.ui.setupUi(self)


class Ui_Raster(object):
    def setupUi(self, Raster):
        Raster.setObjectName("Raster")
        Raster.resize(250, 212)
        self.lcdNumber = QtGui.QLCDNumber(Raster)
        self.lcdNumber.setGeometry(QtCore.QRect(70, 30, 101, 23))
        self.lcdNumber.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
        self.lcdNumber.setObjectName("lcdNumber")
        self.pushButton = QtGui.QPushButton(Raster)
        self.pushButton.setGeometry(QtCore.QRect(20, 130, 93, 28))
        self.pushButton.setObjectName("pushButton")
        self.label = QtGui.QLabel(Raster)
        self.label.setGeometry(QtCore.QRect(20, 30, 72, 15))
        self.label.setObjectName("label")
        self.label_2 = QtGui.QLabel(Raster)
        self.label_2.setGeometry(QtCore.QRect(20, 70, 72, 15))
        self.label_2.setObjectName("label_2")
        self.lineEdit = QtGui.QLineEdit(Raster)
        self.lineEdit.setGeometry(QtCore.QRect(70, 70, 113, 21))
        self.lineEdit.setObjectName("lineEdit")
        self.pushButton_2 = QtGui.QPushButton(Raster)
        self.pushButton_2.setGeometry(QtCore.QRect(120, 130, 93, 28))
        self.pushButton_2.setObjectName("pushButton_2")

        self.retranslateUi(Raster)
        QtCore.QMetaObject.connectSlotsByName(Raster)

    def retranslateUi(self, Raster):
        Raster.setWindowTitle(QtGui.QApplication.translate("Raster", "Raster", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("Raster", "reset", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("Raster", "read", None, QtGui.QApplication.UnicodeUTF8))
        self.label_2.setText(QtGui.QApplication.translate("Raster", "ID", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_2.setText(QtGui.QApplication.translate("Raster", "modify id", None, QtGui.QApplication.UnicodeUTF8))

class RastersCrlWidget(QtGui.QWidget):

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

    def initUI(self):
        raster1 = RasterWidget()
        raster2 = RasterWidget()

**#fine when use standard widget**
#         okButton = QtGui.QPushButton("OK")
#         cancelButton = QtGui.QPushButton("Cancel")

        hbox = QtGui.QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(raster1)
        hbox.addWidget(raster2)

        vbox = QtGui.QVBoxLayout()
        vbox.addStretch(1)
        vbox.addLayout(hbox)

        self.setLayout(vbox)    

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('more rasters')  
        self.show()  

def main():

    app = QtGui.QApplication(sys.argv)
    ex = RastersCrlWidget()
**# appear normally when show separately** 
#     ex = RasterWidget()
#     ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:0)

您没有RasterWidget的任何布局。如果没有布局,窗口小部件就无法计算其大小(minimumSize等),因此它使用默认值,如果需要可缩小到(0, 0)。此外,您在主布局中添加stretch,这实际上会缩小您的小部件并且它们不可见。

简而言之,请使用布局。