在按钮按下的滚动区域中加载 QTableWidgets

时间:2023-03-11
本文介绍了在按钮按下的滚动区域中加载 QTableWidgets的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个名为test()"的方法,它将 3 个单行表加载到滚动条中.

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

由于某种原因,我无法弄清楚,但是,如果我只是在加载时激活 test() 它会起作用,但如果我将其注释掉然后尝试通过按下按钮来激活它,它就不起作用.

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.

这里是主模块(带有 test())

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).

谁能告诉我怎么了?

推荐答案

一开始是父母给孩子看,之后孩子自己负责展示自己,简单的解决方案是使用show():

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

但是我看到你以一种不优雅的方式实现解决方案,连接在 main.py 中完成,不要修改 Qt Designer 生成的文件(删除 design.py 中的连接和测试方法)您必须使用布局并在其中添加 QTableWidget.

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)

这篇关于在按钮按下的滚动区域中加载 QTableWidgets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:无法为 pyqt5 gui 应用程序执行脚本 fbs_pyinstaller_hook 下一篇:如何使用 PyQt5 运行 while 循环

相关文章