PyQGIS Plugin – Retrieve Data from Multiple Cells in QTableWidget

pyqgispyqt5qgis-plugins

I am creating a plugin which shows the list of location of vector/raster layers. I am using QTableWidget to show the data. When I select one row/cell and click a QPushButton it opens the specific vector/ratser layers in the QGIS. I use the following code to retrieve the data from the selected cell:

row = self.tableWidget.currentRow()
path = (self.tableWidget.item(row,3).text())

How do I select multiple cells at the same time to open multiple files?

Best Answer

You could try something like:

paths = []
selected = self.tableWidget.selectedItems()
if selected:
    for item in selected:
        if item.column() == 3:
            paths.append(item.data(0))

Assuming that the cells containing the file paths are in the 4th column (index 3) of your table widget. With this method, you can either select the entire row or just the cell containing the path. The paths variable will now be a list containing all path strings from the selected rows or cells.

Related Question