I'm trying to write an exporter, which takes in a QTableView
and writes all its data into a .csv-file, so it can be viewed in MS Excel.
Now I can't seem to find a way to read the text from the horizontal header's sections. There doesn't seem to be a method like QHeaderView::sections
or something like that, so I can't access the QHeaderView's sections. There is a QHeaderView::count
though, which makes me think that they are indeed saved in a container somewhere.
So is there a way to get the text of a section of QHeaderView
?
Asked
Active
Viewed 1,193 times
0

Dmitry Sazonov
- 8,801
- 1
- 35
- 61

LarissaGodzilla
- 620
- 2
- 10
- 21
1 Answers
1
You should use ::headerData
method of your model.
To get number of columns you may use view->model()->columnCount();
method.

Dmitry Sazonov
- 8,801
- 1
- 35
- 61
-
I fear it will come down to that. I was just hoping, there'd be a more convenient solution. – LarissaGodzilla Sep 17 '13 at 10:01
-
You should understand Qt MVC pattern. You could not pick data from view. View doesn't know anything about data. – Dmitry Sazonov Sep 17 '13 at 10:04
-
Yes, of course, that makes sense. But why can I count the sections then? Isn't that a breach of the pattern? – LarissaGodzilla Sep 17 '13 at 10:07
-
No it doesn't. You should call `model->columnCount( QModelIndex() );` for that. In ideal case you should design your code to keep all data independend from view. – Dmitry Sazonov Sep 17 '13 at 10:32
-
I probably _shouldn't_ get the count from the view, but i _could_ if I wanted to, which I find very strange. Anyway, I followed your suggestion and it works now. I also realized the benefits of keeping the data apart from the view. Thank you very much for your help. – LarissaGodzilla Sep 17 '13 at 10:56