巫溪网站建设,标题翻译为英文wordpress,文档阅读网站模板下载,成都网站开发公司排名1.简介
在开发过程中#xff0c;我们需要通过点击表头来对QTableView或QTreeView等一系列高级视图进行排序操作#xff0c;以下是进行排序的步骤。
步骤#xff1a;
首先创建了一个QStandardItemModel对象或者继承QAbstractTableModel类作为数据模型#xff0c;并设置了…1.简介
在开发过程中我们需要通过点击表头来对QTableView或QTreeView等一系列高级视图进行排序操作以下是进行排序的步骤。
步骤
首先创建了一个QStandardItemModel对象或者继承QAbstractTableModel类作为数据模型并设置了一些数据。然后创建一个QTableView对象并将数据模型设置为其模型。接下来创建一个QSortFilterProxyModel对象并将QStandardItemModel对象设置为其源模型。然后设置QTableView开启排序功能。最后将QSortFilterProxyModel对象设置为QTableView的模型。
2.示例 自定义QAbstractTableModel类
#ifndef MYTABLEMODEL_H
#define MYTABLEMODEL_H#include QAbstractTableModel
#include QObject
#include QListtypedef 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(QListStudent 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:QListStudent m_lstStu;
};#endif // MYMODEL_H#include MyTableModel.hMyTableModel::MyTableModel(QObject *parent): QAbstractTableModel(parent)
{}void MyTableModel::update(QListStudent students)
{m_lstStu students;for(int i0;im_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 QSortFilterProxyModelForm::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);// 构造数据更新界面QListStudent students;QListQString 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;
}