Domanda

I currently have a tableview attached to a class that is derived from QSortFilterProxyModel. Now I wanted to know if there is any way by which I can store the order of columns since the users tend to move the columns back and forth. Also is there any signal that is emitted when a user changes the order of the columns.I search this but I cannot find anything that might tel me when a column is moved around and how i can save the tablew columns orders. Any suggetsions would be appreciated

È stato utile?

Soluzione 2

The reason why you cannot find the relevant signal in the documentation because you are checking an about 6-7 years old documentation. That is, it is Qt 4.1. The signal in question was added to Qt in version 4.6.

QAbstractItemModel has this signal recently:

void QAbstractItemModel::columnsMoved(const QModelIndex & sourceParent, int sourceStart, int sourceEnd, const QModelIndex & destinationParent, int destinationColumn) [signal]

This signal is emitted after columns have been moved within the model. The items between sourceStart and sourceEnd inclusive, under the given sourceParent item have been moved to destinationParent starting at the column destinationColumn.

Note: Components connected to this signal use it to adapt to changes in the model's dimensions. It can only be emitted by the QAbstractItemModel implementation, and cannot be explicitly emitted in subclass code.

This function was introduced in QtCore 4.6.

This looks like what you are looking for. See the documentation for further details:

http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#columnsMoved

Also, do not forget the fact that you will actually need QAbstractTableModel in the end of the day.

If you really wish, you could catch this signal as well:

void QHeaderView::sectionMoved(int logicalIndex, int oldVisualIndex, int newVisualIndex) [signal]

This signal is emitted when a section is moved. The section's logical index is specified by logicalIndex, the old index by oldVisualIndex, and the new index position by newVisualIndex.

Please refer to the documentation for further details:

http://qt-project.org/doc/qt-5.1/qtwidgets/qheaderview.html#sectionMoved

Altri suggerimenti

You need to obtain a QHeaderView object using QTableView::horizontalHeader. You can use QHeaderView::saveState and QHeaderView::restoreState to save state of columns. You can use QHeaderView::sectionMoved signal to detect column moving.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top