• Qt QTableView排序


    1.简介

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

    步骤:

    • 首先创建了一个QStandardItemModel对象或者继承QAbstractTableModel类作为数据模型,并设置了一些数据。
    • 然后创建一个QTableView对象,并将数据模型设置为其模型。
    • 接下来,创建一个QSortFilterProxyModel对象,并将QStandardItemModel对象设置为其源模型。
    • 然后设置QTableView开启排序功能。
    • 最后将QSortFilterProxyModel对象设置为QTableView的模型。

    2.示例

    自定义QAbstractTableModel类:

    1. #ifndef MYTABLEMODEL_H
    2. #define MYTABLEMODEL_H
    3. #include
    4. #include
    5. #include
    6. typedef struct _student
    7. {
    8. QString name;
    9. int age;
    10. double score;
    11. }Student;
    12. class MyTableModel : public QAbstractTableModel
    13. {
    14. Q_OBJECT
    15. public:
    16. MyTableModel(QObject *parent = nullptr);
    17. enum RoleNames{
    18. Name,
    19. Age,
    20. Score
    21. };
    22. public:
    23. //更新
    24. void update(QList students);
    25. //行数量
    26. virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
    27. //列数量
    28. virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
    29. // 表格项数据
    30. virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    31. // 表头数据
    32. virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
    33. private:
    34. QList m_lstStu;
    35. };
    36. #endif // MYMODEL_H
    37. #include "MyTableModel.h"
    38. MyTableModel::MyTableModel(QObject *parent)
    39. : QAbstractTableModel(parent)
    40. {
    41. }
    42. void MyTableModel::update(QList students)
    43. {
    44. m_lstStu = students;
    45. for(int i=0;isize();i++)
    46. {
    47. beginInsertRows(QModelIndex(),i,i);
    48. endInsertRows();
    49. }
    50. }
    51. int MyTableModel::rowCount(const QModelIndex &parent) const
    52. {
    53. Q_UNUSED(parent);
    54. return m_lstStu.count();
    55. }
    56. int MyTableModel::columnCount(const QModelIndex &parent) const
    57. {
    58. Q_UNUSED(parent);
    59. return 3;
    60. }
    61. QVariant MyTableModel::data(const QModelIndex &index, int role) const
    62. {
    63. if (!index.isValid())
    64. return QVariant();
    65. int nColumn = index.column();
    66. int nRow = index.row();
    67. Student stu = m_lstStu.at(nRow);
    68. if(role == Qt::DisplayRole)
    69. {
    70. if (nColumn == MyTableModel::Name)
    71. return stu.name;
    72. else if(nColumn == MyTableModel::Age)
    73. return stu.age;
    74. else if(nColumn == MyTableModel::Score)
    75. return stu.score;
    76. }
    77. return QVariant();
    78. }
    79. QVariant MyTableModel::headerData(int section, Qt::Orientation orientation, int role) const
    80. {
    81. Q_UNUSED(section);
    82. if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
    83. {
    84. if (section == MyTableModel::Name)
    85. return QStringLiteral("姓名");
    86. else if(section == MyTableModel::Age)
    87. return QStringLiteral("年龄");
    88. else if(section == MyTableModel::Score)
    89. return QStringLiteral("分数");
    90. }
    91. return QVariant();
    92. }

    使用代码示例:

    1. #include "form.h"
    2. #include "ui_form.h"
    3. #include "MyTableModel.h"
    4. #include
    5. Form::Form(QWidget *parent) :
    6. QWidget(parent),
    7. ui(new Ui::Form)
    8. {
    9. ui->setupUi(this);
    10. //去除选中虚线框
    11. ui->tableView->setFocusPolicy(Qt::NoFocus);
    12. //设置最后一栏自适应长度
    13. ui->tableView->horizontalHeader()->setStretchLastSection(true);
    14. //设置整行选中
    15. ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    16. //不显示垂直表头
    17. ui->tableView->verticalHeader()->setVisible(false);
    18. MyTableModel *pModel = new MyTableModel(this);
    19. // 构造数据,更新界面
    20. QList students;
    21. QList nameList;
    22. nameList<<"张三"<<"李四"<<"王二"<<"赵五"<<"刘六";
    23. for (int i = 0; i < 5; ++i)
    24. {
    25. Student student;
    26. student.name = nameList.at(i);
    27. student.age = qrand()%6 + 13;//随机生成13到19的随机数
    28. student.score = qrand()%20 + 80;//随机生成0到100的随机数;
    29. students.append(student);
    30. }
    31. pModel->update(students);
    32. ui->tableView->setModel(pModel);
    33. // 设置可排序
    34. ui->tableView->setSortingEnabled(true);
    35. // 设置数据源模型
    36. QSortFilterProxyModel *pProxyModel = new QSortFilterProxyModel(this);
    37. pProxyModel->setSourceModel(pModel);
    38. ui->tableView->setModel(pProxyModel);
    39. // 设置按得分降序排列
    40. ui->tableView->sortByColumn(MyTableModel::Score, Qt::DescendingOrder);
    41. }
    42. Form::~Form()
    43. {
    44. delete ui;
    45. }

  • 相关阅读:
    不愧是阿里资深架构师,这本“分布式架构笔记”写得如此透彻明了
    【LeetCode】640. 求解方程
    springboot+学生信息管理 毕业设计-附源码191219
    系统架构设计高级技能 · 通信系统架构设计理论与实践
    python基于PHP+MySQL的英语在线测评学习网站
    招商银行余额截图生成器在线,虚拟金额中国农业邮政建设工商,易语言开源例子
    ARC122E Increasing LCMs
    浅谈C++|STL之list+forward_list篇
    网络安全之python学习第二天
    Java电子招投标采购系统源码-适合于招标代理、政府采购、企业采购、等业务的企业
  • 原文地址:https://blog.csdn.net/wzz953200463/article/details/134249488