Is it possible to read the values ​​of the entire column or row of a table without looping through them?
Now doing a brute force cycle:

def get_column_data(self, column): data = [] for i in range(self.ui.tableWidget.rowCount()): data.append(self.ui.tableWidget.item(i, column).text()) return data 

Are you interested in making it more elegant without a loop?

    1 answer 1

    Now you do:

     tableWidget = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] tableWidget [[1, 2, 3], [4, 5, 6], [7, 8, 9]] def get_column_data(column): data = [] for row in tableWidget: data.append(str(row[column])) return data get_column_data(1) ['2', '5', '8'] 

    One line will be like this:

     data = [ str(row[1]) for row in tableWidget] data ['2', '5', '8'] 

    Everything is working !!!

     from PyQt5 import Qt class TableExample(Qt.QWidget): def __init__(self, parent=None): super().__init__(parent) self.table = Qt.QTableWidget(3, 3) self.fillButton = Qt.QPushButton("Заполнить ячейки (выделенные ячейки)", self) self.fillButton.clicked.connect(self.onFill) layout = Qt.QVBoxLayout(self) layout.addWidget(self.table) layout.addWidget(self.fillButton) def onFill(self): for i, index in enumerate(self.table.selectedIndexes()): item = Qt.QTableWidgetItem() item.setText(str(i)) self.table.setItem(index.row(), index.column(), item) self.get_column_data(1) # будем выбирать столбец с индексом `1` def get_column_data(self, column): # data = [] # for i in range(self.table.rowCount()): # data.append(self.table.item(i, column).text()) # print("data ->", data) data = [ self.table.item(row, column).text() for row in range(self.table.rowCount()) if self.table.item(row, column) is not None ] print("data->", data) return data if __name__ == '__main__': app = Qt.QApplication([]) w = TableExample() w.show() app.exec() 

    enter image description here

    • Your idea is understandable, but apparently it does not work with tableWidget from PyQt - Duncan
    • @Duncan See an example, everything works. - S. Nick
    • Now figured it out. At first I tried to do this trick literally, without using rowCount () and item (row, column). Text () . But in the second case, we get a literal repetition of what I did initially, but using the generator. My question was the following - maybe there is some property for reading a column from a table? That it would not be necessary to sort through the columns on a cycle or a generator. - Duncan