PySide / PyQt:如何创建自定义类?

时间:2017-06-26 19:08:45

标签: python pyqt pyside qcheckbox qframe

我正在尝试创建复选框,框架和网格布局对象,并将它们存储在自己的列表中。

Qt Designer的输出如下:enter image description here

基本上,我想创建Checkbox1,然后创建一个框架和网格布局来存储Checkbox2和Checkbox3。

我的方法是首先创建一个空列表来存储我正在创建的对象: checkboxList = [] 然后通过调用来附加它以创建适当的对象checkboxList.append(CreateCheckBox(self.frame_main, self.gridLayout_main, 0, 'Checkbox 1')(例如)

这会产生输出:enter image description here

我创建的框架不可见! 我猜这有几个可能的原因:

  1. 我正在错误地创建课程
  2. 我正在存储我错误创建的对象(即init不返回任何内容,所以我实际上并没有存储任何内容?)
  3. 我没有正确地将我的对象添加到主框架中,因此它们没有显示
  4. 为什么我的类实现不像Qt Designer中的代码那样工作,以及我如何进行更改以获得类似的输出?

    从Qt Designer导入的代码如下所示:

    from PySide import QtCore, QtGui
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(494, 210)
            self.centralwidget = QtGui.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.frame_main = QtGui.QFrame(self.centralwidget)
            self.frame_main.setGeometry(QtCore.QRect(20, 10, 435, 141))
            self.frame_main.setFrameShape(QtGui.QFrame.StyledPanel)
            self.frame_main.setFrameShadow(QtGui.QFrame.Raised)
            self.frame_main.setObjectName("frame_main")
            self.gridLayout_2 = QtGui.QGridLayout(self.frame_main)
            self.gridLayout_2.setContentsMargins(0, 0, 0, 0)
            self.gridLayout_2.setSpacing(0)
            self.gridLayout_2.setObjectName("gridLayout_2")
            self.gridLayout_main = QtGui.QGridLayout()
            self.gridLayout_main.setSpacing(0)
            self.gridLayout_main.setObjectName("gridLayout_main")
            self.frame2 = QtGui.QFrame(self.frame_main)
            self.frame2.setFrameShape(QtGui.QFrame.StyledPanel)
            self.frame2.setFrameShadow(QtGui.QFrame.Raised)
            self.frame2.setObjectName("frame2")
            self.gridLayout_16 = QtGui.QGridLayout(self.frame2)
            self.gridLayout_16.setContentsMargins(0, 0, 0, 0)
            self.gridLayout_16.setObjectName("gridLayout_16")
            self.gridLayout2 = QtGui.QGridLayout()
            self.gridLayout2.setObjectName("gridLayout2")
            spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Minimum)
            self.gridLayout2.addItem(spacerItem, 0, 0, 1, 1)
            self.checkBox2 = QtGui.QCheckBox(self.frame2)
            self.checkBox2.setObjectName("checkBox2")
            self.gridLayout2.addWidget(self.checkBox2, 0, 1, 1, 1)
            self.checkBox3 = QtGui.QCheckBox(self.frame2)
            self.checkBox3.setObjectName("checkBox3")
            self.gridLayout2.addWidget(self.checkBox3, 1, 1, 1, 1)
            self.gridLayout_16.addLayout(self.gridLayout2, 0, 0, 1, 1)
            self.gridLayout_main.addWidget(self.frame2, 1, 1, 1, 1)
            self.checkBox1 = QtGui.QCheckBox(self.frame_main)
            self.checkBox1.setObjectName("checkBox1")
            self.gridLayout_main.addWidget(self.checkBox1, 0, 1, 1, 1)
            spacerItem1 = QtGui.QSpacerItem(50, 20, QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Minimum)
            self.gridLayout_main.addItem(spacerItem1, 0, 0, 1, 1)
            self.gridLayout_2.addLayout(self.gridLayout_main, 0, 0, 1, 1)
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtGui.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 494, 21))
            self.menubar.setObjectName("menubar")
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtGui.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)
    
            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
        def retranslateUi(self, MainWindow):
            MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
            self.checkBox2.setText(QtGui.QApplication.translate("MainWindow", "Checkbox2", None, QtGui.QApplication.UnicodeUTF8))
            self.checkBox3.setText(QtGui.QApplication.translate("MainWindow", "Checkbox3", None, QtGui.QApplication.UnicodeUTF8))
            self.checkBox1.setText(QtGui.QApplication.translate("MainWindow", "Checkbox1", None, QtGui.QApplication.UnicodeUTF8))
    
    
    if __name__ == "__main__":
        import sys
        app = QtGui.QApplication(sys.argv)
        MainWindow = QtGui.QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(MainWindow)
        MainWindow.show()
        sys.exit(app.exec_())
    

    我的代码类似于:

    from PySide import QtCore, QtGui
    
    
    class CreateCheckBox(QtGui.QCheckBox):
        def __init__(self, frame, grid, gridLoc, name, parent=None):
            #QtGui.QCheckBox.__init__(self, parent=parent)  Is this the same as the super below?
            super(CreateCheckBox, self).__init__(parent)
            self.checkbox = QtGui.QCheckBox(frame)
            grid.addWidget(self.checkbox, gridLoc, 2, 1, 1)
            self.checkbox.setText(name)
    
    
    class CreateFrame(QtGui.QFrame):
        def __init__(self, parentFrame, parentGridLayout, gridLoc, parent=None):
            #QtGui.QFrame.__init__(self, parent=parent)  Is this the same as the super below?
            super(CreateFrame, self).__init__(parent)
            self.frame = QtGui.QFrame(parentFrame)
            self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
            self.frame.setFrameShadow(QtGui.QFrame.Raised)
            parentGridLayout.addWidget(self.frame, gridLoc, 2, 1, 1)
    
    
    class CreateGridLayout(QtGui.QGridLayout):
        def __init__(self, parentFrame, parent=None):
            super(CreateGridLayout, self).__init__(parent)
            #QtGui.QGridLayout.__init__(self)  # This line throws an error if I include parent=parent, why?..
            self.gridLayoutSecondary = QtGui.QGridLayout(parentFrame)
            self.gridLayoutSecondary.setContentsMargins(0, 0, 0, 0)
            self.gridLayoutPrimary = QtGui.QGridLayout()
            spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Minimum)
            self.gridLayoutPrimary.addItem(spacerItem, 0, 0, 1, 1)
            self.gridLayoutSecondary.addLayout(self.gridLayoutPrimary, 0, 0, 1, 1)
    
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(494, 210)
            self.centralwidget = QtGui.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.frame_main = QtGui.QFrame(self.centralwidget)
            self.frame_main.setGeometry(QtCore.QRect(20, 10, 435, 141))
            self.frame_main.setFrameShape(QtGui.QFrame.StyledPanel)
            self.frame_main.setFrameShadow(QtGui.QFrame.Raised)
            self.frame_main.setObjectName("frame_main")
            self.gridLayout_main = QtGui.QGridLayout()
            self.gridLayout_main.setSpacing(0)
            self.gridLayout_main.setObjectName("gridLayout_main")
            spacerItem1 = QtGui.QSpacerItem(50, 20, QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Minimum)
            self.gridLayout_main.addItem(spacerItem1, 0, 0, 1, 1)
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtGui.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 494, 21))
            self.menubar.setObjectName("menubar")
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtGui.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)
    
            # List that contains new checkboxes
            checkboxList = []
            # List that contains new frames
            frameList = []
            # List than contains grid layouts for new frames
            gridLayoutList = []
    
            # Create 'checkBox1'
            checkboxList.append(CreateCheckBox(self.frame_main, self.gridLayout_main, 0, 'Checkbox 1'))
            # Create 'frame2'
            frameList.append(CreateFrame(self.frame_main, self.gridLayout_main, 1))
            # Create 'gridLayout2'
            gridLayoutList.append(CreateGridLayout(frameList[0]))
            # Create 'checkBox2'
            checkboxList.append(CreateCheckBox(frameList[0], gridLayoutList[0], 0, 'Checkbox 2'))
            # Create 'checkBox3'
            checkboxList.append(CreateCheckBox(frameList[0], gridLayoutList[0], 1, 'Checkbox 3'))
    
            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
        def retranslateUi(self, MainWindow):
            MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
    
    
    if __name__ == "__main__":
        import sys
        app = QtGui.QApplication(sys.argv)
        MainWindow = QtGui.QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(MainWindow)
        MainWindow.show()
        sys.exit(app.exec_())
    

1 个答案:

答案 0 :(得分:2)

El problema se soluciona maquetando correctamentetuaplicación,en la siguiente imagen se muestra el maquetado de QMainWindow,la imagen muestra que QMainWindow varias secciones。

enter image description here

在你的情况下,你应该设计centralwidget,

enter image description here

然后,可以实现答案,观察不必使用QGridLayout,因为它用于将其作为矩阵分发。我创建了addCheckbox函数,它添加了小部件并将其存储在列表中。

from PyQt4 import QtGui, QtCore


class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent=parent)

        # add menubar
        self.menubar = QtGui.QMenuBar(self)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 494, 21))
        self.setMenuBar(self.menubar)
        # add statusbar
        self.statusbar = QtGui.QStatusBar(self)
        self.setStatusBar(self.statusbar)

        # set central widget
        self.centralwidget = QtGui.QWidget(self)
        self.setCentralWidget(self.centralwidget)

        lay = QtGui.QVBoxLayout(self.centralwidget)
        self.checkBox = QtGui.QCheckBox("checkbox1", self)
        lay.addWidget(self.checkBox)

        self.frame = QtGui.QFrame(self)
        lay.addWidget(self.frame)

        self.layout_of_frame = QtGui.QVBoxLayout(self.frame)

        self.checkBoxList = []
        self.addCheckbox("checkbox2")
        self.addCheckbox("checkbox3")
        self.addCheckbox("checkbox4")
        self.addCheckbox("checkbox5")

    def addCheckbox(self, text):
        checkbox = QtGui.QCheckBox(text, self)
        self.layout_of_frame.addWidget(checkbox)
        self.checkBoxList.append(checkbox)

截图:

enter image description here