在按下按钮时将QTableWidgets加载到滚动区域中

时间:2018-07-03 10:15:37

标签: python python-3.x pyqt pyqt5

我有一个名为“ test()”的方法,该方法将3个单行表加载到滚动条中。

出于某种原因,我无法弄清楚,但是,如果我只是在加载时激活test(),它就可以工作,但是如果我注释掉它,然后尝试通过按一下按钮来激活它,那是行不通的

这是主要模块(带有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_())

这是设计模块(带有按钮)

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_())

当我按下按钮时,什么也没发生(打印输出成功完成除外)。

有人可以说出什么问题吗?

1 个答案:

答案 0 :(得分:0)

父母一开始会给孩子看,但是事后孩子有责任表现自己,简单的解决方法是使用show()

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

但是我看到您正在以一种不雅的方式实现该解决方案,该连接在main.py中完成,请勿修改Qt Designer生成的文件(在design.py中删除该连接和测试方法) 您必须使用布局并在其中添加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)