QTableView sorting signal?

陌路散爱 提交于 2019-12-08 20:03:45

问题


I use QTableView + QStandardItemModel to show some data (data stored in some other data structure), and this table view is sortable.

Since it is sortable, when sorting this model, I also need to sort the order of stored data. I try to implement a slot for the sorting signal, but I don't know what signal is emitted when clicking the header to start the sorting action.

I tried the clicked signal, but it's only emitted for data row, not for the headerData. what should I do if I want to do something else while sorting the QtableView + QStandardItemModel ?


回答1:


The Header of the View can be obtained by

QHeaderView * QTableView::horizontalHeader () const

Now with the obtained QHeaderView, you can connect a slot to the signal,

void QHeaderView::sectionClicked ( int logicalIndex )   [signal].

From the Qt 4.5 documentation, This signal is emitted when a section is clicked. The section's logical index is specified by logicalIndex.Note that the sectionPressed signal will also be emitted.

Hope it helps.




回答2:


The Header view mentioned above has signal sortIndicatorChanged(int, Qt::SortOrder) so it might be smarter to use it

Also, you might want to have a look into QSortFilterProxyModel more details here http://doc.qt.io/qt-4.8/qsortfilterproxymodel.html#details



来源:https://stackoverflow.com/questions/3081340/qtableview-sorting-signal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!