Question

I have a QTableView object in my dialog.

I need to access the horizontal headers of this table and put them into a QStringList object.

Despite intensive search, I could not find in Qt documentation how to get this list of headers.

Edit: The closest I've found anywhere is this, but it's dealing with QTableWidget, and the functions used aren't available for QTableView. Unfortunately, I'm not at liberty to switch the object in question to a table widget.

Was it helpful?

Solution

So, the only way I can figure out to do it as like this:

QStringList headers;
for(int i = 0; i < myTableView->model()->columnCount(); i++)
{
  headers.append(myTableView->model()->headerData(i, Qt::Horizontal).toString());
}

Honestly this still seems a lot more convoluted than it should have to be, but at least it works.

OTHER TIPS

You can simplify it a bit, like so. this is the python variation

items = []
for x in range(tbl.columnCount()):
    items.append(tbl.horizontalHeaderItem(x).text())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top