Load QTableWidgets in a Scroll Area on Button Push

2019-03-03 19:52发布

问题:

I have a method named 'test()' that loads 3 one-row tables into a scrollbar.

For some reason that I cannot figure out, however, the while it works if I simply activate test() on load, it doesn't work if I comment it out and then try to activate it via the push of a button.

Here is the main module (with test())

from PyQt5 import QtCore, QtWidgets
from design import Ui_MainWindow

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
        QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
        self.setupUi(self)
        #test(self)

def test(self):
    from random import randint
    x = randint(0, 99)
    print(x)
    height = 30
    yPos = 0
    for i in range(3):

        rowVals = ['test%s' % str(x + i)]

        qTbl = QtWidgets.QTableWidget(self.sawDoc)
        qTbl.setObjectName("tbl%s" % (i))
        qTbl.setGeometry(QtCore.QRect(0, yPos, 880, height))
        qTbl.horizontalHeader().setVisible(False)
        qTbl.verticalHeader().setVisible(False)
        yPos += height

        qTbl.setRowCount(1)
        qTbl.setColumnCount(len(rowVals))
        for r, cell in enumerate(rowVals):
            item = QtWidgets.QTableWidgetItem()
            item.setText(str(cell))
            qTbl.setItem(0, r, item)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

And here is the design module (with the button)

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(896, 453)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.scrDoc = QtWidgets.QScrollArea(self.centralwidget)
        self.scrDoc.setGeometry(QtCore.QRect(0, 0, 891, 391))
        self.scrDoc.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.scrDoc.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.scrDoc.setWidgetResizable(False)
        self.scrDoc.setObjectName("scrTest")
        self.sawDoc = QtWidgets.QWidget()
        self.sawDoc.setGeometry(QtCore.QRect(0, 0, 869, 300))
        self.sawDoc.setObjectName("sawDoc")
        self.scrDoc.setWidget(self.sawDoc)
        self.btnTest = QtWidgets.QPushButton(self.centralwidget)
        self.btnTest.setGeometry(QtCore.QRect(430, 400, 80, 15))
        self.btnTest.setObjectName("btnTest")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

        self.btnTest.clicked.connect(self.test)

    def test(self):
        import main
        main.test(self)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "昊珩のCAT工具"))
        self.btnTest.setText(_translate("MainWindow", "Test"))



if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Simply nothing happens when I push the button (except for the print out working successfully).

Can someone tell what's wrong?

回答1:

The parents show the children at the beginning, but afterwards it is the responsibility of the children to show themselves, the simple solution is to use show():

qTbl = QtWidgets.QTableWidget(self.sawDoc)
qTbl.show()

But I see that you are implementing the solution in an inelegant way, the connection do it in main.py, do not modify the file generated by Qt Designer (delete the connection and the test method in design.py) You must use a layout and there add the QTableWidget.

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
        QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
        self.setupUi(self)
        self.btnTest.clicked.connect(self.test)

    def test(self):
        from random import randint
        x = randint(0, 99)

        lay = QtWidgets.QVBoxLayout(self.sawDoc)
        for i in range(3):
            rowVals = ['test%s' % str(x + i)]
            qTbl = QtWidgets.QTableWidget()
            qTbl.setObjectName("tbl%s" % (i))
            qTbl.horizontalHeader().hide()
            qTbl.verticalHeader().hide()
            qTbl.setRowCount(1)
            qTbl.setColumnCount(len(rowVals))
            for r, cell in enumerate(rowVals):
                item = QtWidgets.QTableWidgetItem()
                item.setText(str(cell))
                qTbl.setItem(0, r, item)
            qTbl.resize(qTbl.sizeHint())
            lay.addWidget(qTbl)