Tianchi  v0.0.2 build 20130701
C++ library for Qt with VC & mingW
Public 槽 | 信号 | Public 成员函数 | 所有成员列表
TcSortPaginationTableView类 参考

可排序和分页的TableView,使用时主要分两步: 更多...

#include <tcsortpaginationtableview.h>

类 TcSortPaginationTableView 继承关系图:

Public 槽

void fetchData (int page=-1, int section=-1, Qt::SortOrder order=Qt::AscendingOrder)
 获取数据(实际上内部是发射fetchDataRequested()信号 更多...
 

信号

void fetchDataRequested ()
  请求数据时发出,比如排序和换页
 

Public 成员函数

 TcSortPaginationTableView (QWidget *parent=0)
 
int pageRowCount () const
 返回每页显示记录条数
 
int page () const
 返回当前请求页码
 
int sortSection () const
 返回当前请求排序列
 
Qt::SortOrder sortOrder () const
 返回当前请求排序方向
 
void setPageRowCount (int pageRowCount=-1)
 设置每页显示记录条数, -1表示不分页
 
void setPageInfo (int page, int totalCount)
 设置paginator分页符字符串 更多...
 
void setPaginator (QLabel *label)
 设置分页部件
 
void createTitleMenus ()
 为横标头创建右键关联菜单
 

详细描述

可排序和分页的TableView,使用时主要分两步:

        1. 初始化TcSortPaginationTableView
        2. 处理获取数据请求(排序/分页/过滤)
  1. 初始化TcSortPaginationTableView
    model = new QStandardItemModel(this);
    ......
    tableView->setModel(model);
    tableView->setPaginator(labelPaginator); // 设置显示分页符的label
    tableView->setPageRowCount(20); // 设置每页显示记录数
    tableView->createTitleMenus(); // 创建标题行右键菜单,可显示/隐藏列
    connect(tableView, SIGNAL(fetchDataRequested()),
    this, SLOT(fetchData()));
  2. 处理获取数据请求(排序/分页/过滤)
    void XXXX::fetchData()
    {
    model->removeRows(0, model->rowCount());
    QSqlQuery q;
    QString strSql = "SELECT col1, col2, col3, col4 FROM table1 "
    "WHERE 1 = 1 ";
    QString strSqlCnt = "SELECT COUNT(*) FROM table1 "
    "WHERE 1 = 1 ";
    // 过滤条件
    strSql += " AND col1 LIKE 'xxx%%'";
    strSqlCnt += " AND col1 LIKE 'xxx%%'";
    // 根据是否排序,设定order语句
    if (tableView->isSortingEnabled())
    {
    switch (tableView->sortSection())
    {
    case 0:
    strSql += " ORDER BY col1";
    break;
    case 1:
    strSql += " ORDER BY col2";
    break;
    default:
    strSql += " ORDER BY col3";
    }
    if (tableView->sortOrder() == Qt::DescendingOrder)
    {
    strSql += " DESC";
    }
    }
    else
    { // 如果不可排序,则使用默认排序
    strSql += " ORDER BY col1";
    }
    int count = 0; // 代表符合条件的总记录数
    // 根据是否分页设定来读取当前页数据
    if (tableView->pageRowCount() > 0)
    { // 分页
    strSql += QString(" LIMIT %1 OFFSET %2")
    .arg(tableView->pageRowCount())
    .arg((tableView->page() - 1)
    * tableView->pageRowCount());
    q.exec(strSql);
    while (q.next())
    {
    .....// 获取当前页数据
    }
    // 获取符合条件的总数据条数
    q.exec(strSqlCnt);
    if (q.next())
    {
    count = q.value(0).toInt();
    }
    }
    else
    { // 不分页
    q.exec(strSql);
    while (q.next())
    {
    ......// 不分页,所以获取所有符合条件的数据
    count++;
    }
    }
    // 最后设置分页信息
    tableView->setPageInfo(tableView->page(), count);

成员函数说明

void TcSortPaginationTableView::fetchData ( int  page = -1,
int  section = -1,
Qt::SortOrder  order = Qt::AscendingOrder 
)
slot

获取数据(实际上内部是发射fetchDataRequested()信号

参数
page指定页码(-1表示当前页码不变)
section指定排序列(-1表示排序列不变)
order指定排序方向(section为-1时无意义)
void TcSortPaginationTableView::setPageInfo ( int  page,
int  totalCount 
)

设置paginator分页符字符串

参数
page当前页码
totalCount总记录数

该类的文档由以下文件生成: