Python 如何在tableWidget PyQT中添加一行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24044421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:52:58  来源:igfitidea点击:

How to add a row in a tableWidget PyQT?

pythonpyqtpyqt4qtablewidget

提问by sudobangbang

I am currently working on a widget that was designed in Qt Designer. I am having trouble with the syntax / overall concept of trying to add a row to a Qtable in PyQT. There is no method, which I have yet found to dynamically add rows. Any suggestions would be helpful.

我目前正在研究在 Qt Designer 中设计的小部件。我在尝试将一行添加到 PyQT 中的 Qtable 的语法/整体概念上遇到了问题。没有方法,我还没有发现动态添加行。任何的意见都将会有帮助。

Regards

问候

采纳答案by Aleksandar

You can add empty row and later populate all columns. This is how to insert row under all other rows:

您可以添加空行,然后填充所有列。这是在所有其他行下插入行的方法:

rowPosition = self.table.rowCount()
table.insertRow(rowPosition)

after that you have empty row that you can populate like this for example( if you have 3 columns):

之后,您可以像这样填充空行(如果您有 3 列):

table.setItem(rowPosition , 0, QtGui.QTableWidgetItem("text1"))
table.setItem(rowPosition , 1, QtGui.QTableWidgetItem("text2"))
table.setItem(rowPosition , 2, QtGui.QTableWidgetItem("text3"))

You can also insert row at some other position (not necessary at the end of table)

您还可以在其他位置插入行(不需要在表格末尾)

回答by Bo Milanovich

It is somewhat peculiar, I've found. To insert a row, you have to follow something similar to this:

我发现这有点奇怪。要插入一行,您必须遵循与此类似的操作:

tableWidget = QTableWidget()
currentRowCount = tableWidget.rowCount() #necessary even when there are no rows in the table
tableWidget.insertRow(currentRowCount, 0, QTableWidgetItem("Some text"))

To clarify the last line of code, the first parameter of insertRow()is the current row, the second one is current column (remember it's always 0-based), and the third must almost always be of type QTableWidgetItem).

为了澄清最后一行代码,第一个参数insertRow()是当前行,第二个参数是当前列(记住它总是从 0 开始),第三个参数必须几乎总是类型QTableWidgetItem)。

回答by saman koushki

You can use this function

你可以使用这个功能

def table_appender(widget, *args):

    def set_columns(len, pos):
        if pos == len-1:
            widget.setItem(widget.rowCount()-1, pos, QTableWidgetItem(args[pos]))
        else:
            widget.setItem(widget.rowCount()-1, pos, QTableWidgetItem(args[pos]))
            set_columns(len, pos+1)
    widget.insertRow(widget.rowCount())
    set_columns(widget.columnCount(), 0)

回答by Max Keasley

def add_guest(self):
    rowPosition = self.tableWidget.rowCount()
    self.tableWidget.insertRow(rowPosition)
    guest_name = self.lineEdit.text()
    guest_email = self.lineEdit_2.text()
    numcols = self.tableWidget.columnCount()
    numrows = self.tableWidget.rowCount()           
    self.tableWidget.setRowCount(numrows)
    self.tableWidget.setColumnCount(numcols)           
    self.tableWidget.setItem(numrows -1,0,QtGui.QTableWidgetItem(guest_name))
    self.tableWidget.setItem(numrows -1,1,QtGui.QTableWidgetItem(guest_email))
    print "guest added"         

This is how i got it done for my event organisation application

这就是我如何为我的活动组织应用程序完成它