简体   繁体   English

Qt和PyQt tablewidget的行数增加

[英]Qt and PyQt tablewidget growing row count

I am learning Python and trying to write a program to help my dad. 我正在学习Python,并试图编写一个程序来帮助我的父亲。 I want it to be excel-like, and it looks great for now, but I have no idea how to make the number of rows (not the columns) in a tableWidget to grow while someones crolls down... Can I do it using QtDesigner or I have to write the code in the .py file? 我希望它像excel一样,并且现在看起来不错,但是我不知道如何在某人向下滚动时如何增加tableWidget中的行数(而不是列数)...我可以使用QtDesigner还是我必须在.py文件中编写代码? Any help is appreciated... Sorry if I am asking silly questions, I an a noob really... 非常感谢您的帮助。对不起,如果我问的是愚蠢的问题,我真是个菜鸟。

Here is sort of a proof-of-concept example: 这是一个概念验证示例:

class Widget(QtGui.QWidget):
    def __init__(self):
        super(Widget, self).__init__()
        self.resize(600,400)

        layout = QtGui.QVBoxLayout(self)
        self.table = QtGui.QTableWidget(20,10)
        self.vBar = self.table.verticalScrollBar()
        self._vBar_lastVal = self.vBar.value()

        layout.addWidget(self.table)

        self.vBar.valueChanged.connect(self.scrollbarChanged)

    def scrollbarChanged(self, val):
        bar = self.vBar
        minVal, maxVal = bar.minimum(), bar.maximum()
        avg = (minVal+maxVal)/2
        rowCount = self.table.rowCount()

        # scrolling down
        if val > self._vBar_lastVal and val >= avg:
            self.table.insertRow(rowCount)

        # scrolling up
        elif val < self._vBar_lastVal:
            lastRow = rowCount-1
            empty = True
            for col in xrange(self.table.columnCount()):
                item = self.table.item(lastRow, col)
                if item and item.text():
                    empty=False
                    break
            if empty:
                self.table.removeRow(lastRow)

        self._vBar_lastVal = val

You have to rely on the vertical scroll bar of the table widget to signal information about it changing value. 您必须依靠表格窗口小部件的垂直滚动条来发出有关其更改值的信息。 So we connect its valueChanged(int) signal to our method. 因此,我们将其valueChanged(int)信号连接到我们的方法。

The scrollbarChanged SLOT will receive the value of the scrollbar. scrollbarChanged SLOT将接收scrollbarChanged的值。 What I am doing here is checking the min and max value of the scrollbar at that moment, and seeing if the current position is at least in the middle. 我在这里要做的是检查滚动条的最小值和最大值,然后查看当前位置是否至少在中间。 We also check if the current scrollbar value is greater than the last time, because we only want to add rows on a down scroll. 我们还检查当前滚动条的值是否大于上一次,因为我们只想在向下滚动中添加行。 If these conditions are true, then we insert a new row. 如果满足这些条件,则我们插入新行。

The act of shrinking it back down is a bit more involved because I am sure you will need to check the last row to make sure it is empty and only remove it if so. 将其收缩回缩的动作涉及更多,因为我确定您将需要检查最后一行以确保其为空,并且只有这样才能将其删除。 My version gives you the rough idea but the math logic would probably need more work. 我的版本为您提供了一个大概的想法,但是数学逻辑可能需要做更多的工作。 But what it is doing is going through every item in the last row to see if its None or an empty value. 但是它正在做的是遍历最后一行中的每个项目,以查看其无或为空值。 If the whole row is empty, it removes it. 如果整行为空,则将其删除。 Thus, you do get a reducing effect when scrolling back up. 因此,向上滚动时确实会降低效果。

Hope this gives you a starting point! 希望这给您一个起点! Feel free to ask about the code if you need more detailed explanation of any parts. 如果您需要任何部分的详细说明,请随时询问代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM