QTableView disable sorting for some columns

随声附和 提交于 2019-12-08 12:35:18

问题


I am using QtableView(qt5.9) with 10 columns and want to disable sorting for 2nd and 3rd (only some) columns when the user clicks on the header of these columns.

  • I use setsortingenabled flag to make my QtableView allow sorting

  • Is there any signal which I should listen to on clicking of header and then call some appropraite method or deny sorting.


回答1:


You can use the header signal sortIndicatorChanged to restore the current sort indicator.

Example:

    connect(m_poTableView->header(), &QHeaderView::sortIndicatorChanged,
            this, &MyClass::HandleIndicatorChanged);


    MyClass::HandleIndicatorChanged(int logicalIndex, Qt::SortOrder eSort)
    {
       if (logicalIndex != 0)
       {
             this->m_poTableView->horizontalHeader()->setSortIndicator(
                0, this->m_poTableView->model()->sortOrder());
       }
    }


来源:https://stackoverflow.com/questions/47195261/qtableview-disable-sorting-for-some-columns

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