PyQt5自定义窗口小部件未出现在GridLayout中

时间:2019-01-14 18:23:44

标签: python pyqt pyqt5

我正在尝试将自定义窗口小部件添加到布局。我可以成功地将许多PushButtons添加到我的GridLayout中,但是当我尝试添加自定义窗口小部件时,它不会显示。

我试图提供一个最小的例子:

from PyQt5 import QtCore, QtGui, QtWidgets
import sys

class moduleForm(QtWidgets.QWidget):

    def __init__(self, parent = None):

        self.parent = parent

        self.setObjectName("moduleForm")
        self.resize(300, 400)
        self.fModule = QtWidgets.QPushButton("Test")
        self.fModule.setGeometry(QtCore.QRect(0, 0, 80, 20))

        self.retranslateUi(self.parent)

        QtCore.QMetaObject.connectSlotsByName(self)

    def retranslateUi(self, moduleForm):
        _translate = QtCore.QCoreApplication.translate

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):

        MainWindow.setObjectName("Rb Controller")
        MainWindow.resize(900, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.loMainTab = QtWidgets.QHBoxLayout(self.centralwidget)
        self.centralwidget.setLayout(self.loMainTab)

        self.saChannels = QtWidgets.QScrollArea(self.centralwidget)
        self.saChannels.setWidgetResizable(True)
        self.saChannels.setGeometry(QtCore.QRect(10,10,10,10))

        self.fButtons = QtWidgets.QFrame(self.centralwidget)
        self.fButtons.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.pbAddModule = QtWidgets.QPushButton(self.fButtons)
        self.pbAddModule.setGeometry(QtCore.QRect(10, 10, 80, 20))
        self.pbAddModule.setObjectName("pbAddModule")
        self.loButtons = QtWidgets.QHBoxLayout(self.fButtons)
        self.loButtons.addWidget(self.pbAddModule)
        self.loButtons.addStretch()
        self.fButtons.setLayout(self.loButtons)

        self.hlwChannelsContents = QtWidgets.QWidget()
        self.hlwChannelsContents.setObjectName("hlwChannelsContents")
        self.hloChannelsContents = QtWidgets.QHBoxLayout(self.hlwChannelsContents)
        self.hloChannelsContents.setObjectName("hloChannelsContents")
        self.gloChannelsContents = QtWidgets.QGridLayout()
        self.hloChannelsContents.addLayout(self.gloChannelsContents)
        self.saChannels.setWidget(self.hlwChannelsContents)

        self.loMainTab.addWidget(self.fButtons)
        self.loMainTab.addWidget(self.saChannels,1)

        for ii in range(10):
            for jj in range(10):
                self.r_button = QtWidgets.QPushButton("Element %s,%s " % (ii, jj))
                self.gloChannelsContents.addWidget(self.r_button,ii,jj)

        MainWindow.setCentralWidget(self.centralwidget)


        self.retranslateUi(MainWindow)
        self.pbAddModule.clicked.connect(self.createModule)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Rb Controller"))
        self.pbAddModule.setText(_translate("MainWindow", "Add Module"))


    def createModule(self):
        createModule = moduleForm()
        self.gloChannelsContents.addWidget(createModule)
        createModule.show()


class ApplicationWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(ApplicationWindow, self).__init__()

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)


def main():
    app = QtWidgets.QApplication(sys.argv)
    application = ApplicationWindow()
    application.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

我已经添加了super().__init__函数,但是它仍然无法正常工作。如果我添加的不是自定义小部件,则会显示自定义小部件中的PushButton,因此其他所有代码都可以。

如果我有

self.gloChannelsContents.addWidget(createModule.fModule,self.i,self.j)    

createModule中,我会得到一个动态PushButton,但是,如果我尝试使用自定义小部件

self.gloChannelsContents.addWidget(createModule,self.i,self.j)        

什么都没有出现。

1 个答案:

答案 0 :(得分:0)

尝试一下:

from PyQt5 import QtCore, QtGui, QtWidgets
import sys

class moduleForm(QtWidgets.QWidget):

    def __init__(self, row, parent = None):                            # + row
        super().__init__()

        self.parent = parent

        self.setObjectName("moduleForm")
        self.resize(300, 400)
        self.fModule = QtWidgets.QPushButton("Test {}".format(row))    # + row
        self.fModule.setGeometry(QtCore.QRect(0, 0, 80, 20))

        self.retranslateUi(self.parent)

        QtCore.QMetaObject.connectSlotsByName(self)

    def retranslateUi(self, moduleForm):
        _translate = QtCore.QCoreApplication.translate

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):

        self.i = 11                                               # + self.i

        MainWindow.setObjectName("Rb Controller")
        MainWindow.resize(900, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.loMainTab = QtWidgets.QHBoxLayout(self.centralwidget)
        self.centralwidget.setLayout(self.loMainTab)

        self.saChannels = QtWidgets.QScrollArea(self.centralwidget)
        self.saChannels.setWidgetResizable(True)
        self.saChannels.setGeometry(QtCore.QRect(10,10,10,10))

        self.fButtons = QtWidgets.QFrame(self.centralwidget)
        self.fButtons.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.pbAddModule = QtWidgets.QPushButton(self.fButtons)
        self.pbAddModule.setGeometry(QtCore.QRect(10, 10, 80, 20))
        self.pbAddModule.setObjectName("pbAddModule")
        self.loButtons = QtWidgets.QHBoxLayout(self.fButtons)
        self.loButtons.addWidget(self.pbAddModule)
        self.loButtons.addStretch()
        self.fButtons.setLayout(self.loButtons)

        self.hlwChannelsContents = QtWidgets.QWidget()
        self.hlwChannelsContents.setObjectName("hlwChannelsContents")
        self.hloChannelsContents = QtWidgets.QHBoxLayout(self.hlwChannelsContents)
        self.hloChannelsContents.setObjectName("hloChannelsContents")
        self.gloChannelsContents = QtWidgets.QGridLayout()
        self.hloChannelsContents.addLayout(self.gloChannelsContents)
        self.saChannels.setWidget(self.hlwChannelsContents)

        self.loMainTab.addWidget(self.fButtons)
        self.loMainTab.addWidget(self.saChannels,1)

        for ii in range(10):
            for jj in range(10):
                self.r_button = QtWidgets.QPushButton("Element %s,%s " % (ii, jj))
                self.gloChannelsContents.addWidget(self.r_button, ii, jj)

        MainWindow.setCentralWidget(self.centralwidget)


        self.retranslateUi(MainWindow)
        self.pbAddModule.clicked.connect(self.createModule)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Rb Controller"))
        self.pbAddModule.setText(_translate("MainWindow", "Add Module"))


    def createModule(self):
        self.createModule = moduleForm(self.i)                                   # +
        self.gloChannelsContents.addWidget(self.createModule.fModule, self.i, 0) # +
        self.i += 1                                                              # +
#        self.createModule.show()  # ???


class ApplicationWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(ApplicationWindow, self).__init__()

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)


def main():
    app = QtWidgets.QApplication(sys.argv)
    application = ApplicationWindow()
    application.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()  

enter image description here