Arthur
Arthur

Reputation: 303

How to get rows data from a QTableWidget Column?

How can I get the value of each row from a specific column in a QTableWidget?

Upvotes: 2

Views: 7371

Answers (2)

Shamith N
Shamith N

Reputation: 11

You can extract the data from each cell of the Table Widget by accessing it by iterating it through each row and column ( which can be done by ranging rowCount() each row & range of columnCount() per each column ) using the item() method.

If you're trying to access another widget from the Table widget's cell, then it is best to use cellWidget(), as it is specially made to access the widget present within the cell of the given table widget.

In the following case, I have assumed that the cell may contain item (like some text). So, I have used the item() method instead of cellWidget(). In both of these methods, if there's no content within the cell, they return None. And lastly, I have appended the text to the data list that store all the content of the Table widget.

data = []
for row in range(tablewidget.rowCount()):
    for col in range(tablewidget.columnCount())
        item = tablewidget.item(row,col)
        if item == None:
            continue
        data.append(text)
print(data)

Upvotes: 1

eyllanesc
eyllanesc

Reputation: 244311

To get the values of all the rows of a given column then you must iterate by obtaining the QTableWidgetItems using the item() method, verify if it is valid and then get the text.

col = ...

data = []
for row in range(tablewidget.rowCount()):
    it = tablewidget.item(row, col)
    text = it.text() if it is not None else ""
    data.append(text)
print(data)

Upvotes: 5

Related Questions