【问题标题】:Is there any way to save the order of columns?有没有办法保存列的顺序?
【发布时间】:2013-09-28 00:53:39
【问题描述】:

我目前有一个附加到从 QSortFilterProxyModel 派生的类的 tableview。现在我想知道是否有任何方法可以存储列的顺序,因为用户倾向于来回移动列。当用户更改列的顺序时是否会发出任何信号。我搜索this 但我找不到任何可能在列移动时告诉我的信息以及如何保存表格列顺序。 任何建议将不胜感激

【问题讨论】:

    标签: c++ qt qtableview qtgui


    【解决方案1】:

    您需要使用QTableView::horizontalHeader 获取QHeaderView 对象。您可以使用QHeaderView::saveStateQHeaderView::restoreState 来保存列的状态。您可以使用QHeaderView::sectionMoved 信号来检测列移动。

    【讨论】:

      【解决方案2】:

      您在文档中找不到相关信号的原因是因为您正在检查大约 6-7 年的文档。也就是说,它是 Qt 4.1。有问题的信号是在 4.6 版中添加到 Qt 中的。

      QAbstractItemModel 最近有这个信号:

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

      在模型中的列移动后发出此信号。 sourceStart 和 sourceEnd 之间的项目,在给定的 sourceParent 项目下,已从destinationColumn 列开始移动到destinationParent。

      注意:连接到此信号的组件使用它来适应模型尺寸的变化。它只能由 QAbstractItemModel 实现发出,不能在子类代码中显式发出。

      此功能是在 QtCore 4.6 中引入的。

      这看起来像您正在寻找的东西。有关详细信息,请参阅文档:

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

      另外,不要忘记您最终将需要QAbstractTableModel

      如果你真的愿意,你也可以捕捉到这个信号:

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

      当一个部分被移动时会发出这个信号。节的逻辑索引由logicalIndex指定,旧索引由oldVisualIndex指定,新索引位置由newVisualIndex指定。

      请参阅文档了解更多详情:

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

      【讨论】: