Question

I currently load the results of an SQL query into a TableView.

self.projectModel = QSqlQueryModel()
self.projectModel.setQuery(sql,db)

I then need to select a specific cell based on the header label (geometry). This column will move position depending on the different table that is search.

When the user clicks anywhere on the row (NOT the cell of geometry column) I would like to select the geometry column cell.

At the moment I have a this associated with the tableView

self.dlg.tableView.clicked.connect(self.cellClicked)

And in that function I have

row = self.projectModel.currentIndex()

If I use QTableView.model(row, column) to select the index, I have to speciify the row and column number. However, this would vary so I would want to do QTableView.model(row, 'geometry') however the model expects integers.

Any solutions?

thanks

Was it helpful?

Solution

So it seems all you need is a method to find a column from its header label, i.e. something like:

def columnFromLabel(self, label):
    model = self.table.horizontalHeader().model()
    for column in range(model.columnCount()):
        if model.headerData(column, QtCore.Qt.Horizontal) == label:
            return column
    return -1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top