在开发过程中,我们需要通过点击表头来对QTableView或QTreeView等一系列高级视图进行排序操作,以下是进行排序的步骤。
步骤:

自定义QAbstractTableModel类:
- #ifndef MYTABLEMODEL_H
- #define MYTABLEMODEL_H
-
- #include
- #include
- #include
-
- typedef struct _student
- {
- QString name;
- int age;
- double score;
- }Student;
-
-
- class MyTableModel : public QAbstractTableModel
- {
- Q_OBJECT
- public:
- MyTableModel(QObject *parent = nullptr);
-
-
- enum RoleNames{
- Name,
- Age,
- Score
- };
-
- public:
- //更新
- void update(QList
students) ; -
- //行数量
- virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
-
- //列数量
- virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
-
- // 表格项数据
- virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
-
- // 表头数据
- virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
-
- private:
- QList
m_lstStu; - };
-
- #endif // MYMODEL_H
-
-
- #include "MyTableModel.h"
-
- MyTableModel::MyTableModel(QObject *parent)
- : QAbstractTableModel(parent)
- {
-
- }
-
- void MyTableModel::update(QList
students) - {
- m_lstStu = students;
- for(int i=0;i
size();i++) - {
- beginInsertRows(QModelIndex(),i,i);
- endInsertRows();
- }
- }
-
- int MyTableModel::rowCount(const QModelIndex &parent) const
- {
- Q_UNUSED(parent);
-
- return m_lstStu.count();
- }
-
- int MyTableModel::columnCount(const QModelIndex &parent) const
- {
- Q_UNUSED(parent);
-
- return 3;
- }
-
- QVariant MyTableModel::data(const QModelIndex &index, int role) const
- {
- if (!index.isValid())
- return QVariant();
-
- int nColumn = index.column();
- int nRow = index.row();
- Student stu = m_lstStu.at(nRow);
-
- if(role == Qt::DisplayRole)
- {
- if (nColumn == MyTableModel::Name)
- return stu.name;
- else if(nColumn == MyTableModel::Age)
- return stu.age;
- else if(nColumn == MyTableModel::Score)
- return stu.score;
- }
-
-
- return QVariant();
- }
-
- QVariant MyTableModel::headerData(int section, Qt::Orientation orientation, int role) const
- {
- Q_UNUSED(section);
-
- if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
- {
- if (section == MyTableModel::Name)
- return QStringLiteral("姓名");
- else if(section == MyTableModel::Age)
- return QStringLiteral("年龄");
- else if(section == MyTableModel::Score)
- return QStringLiteral("分数");
-
- }
-
- return QVariant();
- }

使用代码示例:
- #include "form.h"
- #include "ui_form.h"
- #include "MyTableModel.h"
- #include
-
- Form::Form(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Form)
- {
- ui->setupUi(this);
-
- //去除选中虚线框
- ui->tableView->setFocusPolicy(Qt::NoFocus);
-
- //设置最后一栏自适应长度
- ui->tableView->horizontalHeader()->setStretchLastSection(true);
-
- //设置整行选中
- ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
-
- //不显示垂直表头
- ui->tableView->verticalHeader()->setVisible(false);
-
- MyTableModel *pModel = new MyTableModel(this);
-
- // 构造数据,更新界面
- QList
students; - QList
nameList; - nameList<<"张三"<<"李四"<<"王二"<<"赵五"<<"刘六";
-
- for (int i = 0; i < 5; ++i)
- {
- Student student;
- student.name = nameList.at(i);
- student.age = qrand()%6 + 13;//随机生成13到19的随机数
- student.score = qrand()%20 + 80;//随机生成0到100的随机数;
-
- students.append(student);
- }
-
- pModel->update(students);
- ui->tableView->setModel(pModel);
-
- // 设置可排序
- ui->tableView->setSortingEnabled(true);
-
- // 设置数据源模型
- QSortFilterProxyModel *pProxyModel = new QSortFilterProxyModel(this);
- pProxyModel->setSourceModel(pModel);
- ui->tableView->setModel(pProxyModel);
-
- // 设置按得分降序排列
- ui->tableView->sortByColumn(MyTableModel::Score, Qt::DescendingOrder);
- }
-
- Form::~Form()
- {
- delete ui;
- }