Qt QTableView排序

编程入门 行业动态 更新时间:2024-10-27 00:28:02

<a href=https://www.elefans.com/category/jswz/34/1769097.html style=Qt QTableView排序"/>

Qt QTableView排序

1.简介

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

步骤:

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

2.示例

自定义QAbstractTableModel类:

#ifndef MYTABLEMODEL_H
#define MYTABLEMODEL_H#include <QAbstractTableModel>
#include <QObject>
#include <QList>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<Student> 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<Student> m_lstStu;
};#endif // MYMODEL_H#include "MyTableModel.h"MyTableModel::MyTableModel(QObject *parent): QAbstractTableModel(parent)
{}void MyTableModel::update(QList<Student> students)
{m_lstStu = students;for(int i=0;i<m_lstStu.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 <QSortFilterProxyModel>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<Student> students;QList<QString> 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;
}

更多推荐

Qt QTableView排序

本文发布于:2023-11-16 06:50:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1614133.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Qt   QTableView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!