QTableWidget和 QTableView翻页效果

以QTableView举例,QTableWidget使用相同
 
int CQTTableViewPageSwitch::pageCount(QTableView *p)//QTableView 总页数
{
    if(p == NULL)  return -1;
 
  
    int rowCount = p->model()->rowCount();
    int rowHeight = p->rowHeight(0);
    int tableViewHeight = p->height();
    int rowCountPerPage = tableViewHeight/rowHeight-1;//每页显示行数
    int ret = rowCount/rowCountPerPage;
    int tem = rowCount%rowCountPerPage;
    if (tem != 0) ret++;
    return ret;
}
 
  
BOOL CQTTableViewPageSwitch::pageTo(QTableView *p, int pageNO)//翻到指定页
{
    if(p == NULL)  return false;
 
  
    int maxPage = pageCount(p);
    if(pageNO > maxPage)  return false;
 
  
    int rowCount = p->model()->rowCount();
    int rowHeight = p->rowHeight(0);
    int tableViewHeight = p->height();
    int rowCountPerPage = tableViewHeight/rowHeight-1;		//每页显示行数
    int canNotViewCount = rowCount-rowCountPerPage;			//看不见的行数
    if(canNotViewCount == 0) return false;
 
  
    int maxValue = p->verticalScrollBar()->maximum();		// 当前SCROLLER最大显示值
    if(maxValue == 0) return false;
 
  
    int pageValue = (maxValue*rowCountPerPage)/canNotViewCount;
    p->verticalScrollBar()->setSliderPosition(pageValue*(pageNO-1));
 
  
}
 
  
BOOL CQTTableViewPageSwitch::pageUp(QTableView *p,BOOL isLoop)//上翻
{
    if(p == NULL)  return false;
 
  
    int rowCount = p->model()->rowCount();
    int rowHeight = p->rowHeight(0);
    int tableViewHeight = p->height();
    int rowCountPerPage = tableViewHeight/rowHeight-1;		//每页显示行数
    int canNotViewCount = rowCount-rowCountPerPage;			//看不见的行数
    if(canNotViewCount == 0) return false;
 
  
    int maxValue = p->verticalScrollBar()->maximum();		// 当前SCROLLER最大显示值
    if(maxValue == 0) return false;
    int pageValue = (maxValue*rowCountPerPage)/canNotViewCount;
    int nCurScroller = p->verticalScrollBar()->value();		//获得当前scroller值
 
  
    if(nCurScroller>0)
        p->verticalScrollBar()->setSliderPosition(nCurScroller-pageValue);
    else
    {
        if(isLoop == TRUE)
            p->verticalScrollBar()->setSliderPosition(maxValue);
    }
}
 
  
BOOL CQTTableViewPageSwitch::pageDown(QTableView *p,BOOL isLoop)//下翻
{
    if(p == NULL)  return false;
 
  
    int rowCount = p->model()->rowCount();
    int rowHeight = p->rowHeight(0);
    int tableViewHeight = p->height();
    int rowCountPerPage = tableViewHeight/rowHeight-1;		//每页显示行数
 
  
    int canNotViewCount = rowCount-rowCountPerPage;			//看不见的行数
    if(canNotViewCount == 0)
        return false;
    int maxValue = p->verticalScrollBar()->maximum();		// 当前SCROLLER最大显示值
    if(maxValue == 0)
        return false;
    int pageValue = (maxValue*rowCountPerPage)/canNotViewCount;
    int nCurScroller = p->verticalScrollBar()->value();		//获得当前scroller值
 
  
    if(nCurScroller<maxValue)
        p->verticalScrollBar()->setSliderPosition(nCurScroller+pageValue);
    else
    {
        if(isLoop == TRUE)
            p->verticalScrollBar()->setSliderPosition(0);
    }
}
 
  
BOOL CQTTableViewPageSwitch::pageHome(QTableView *p)//首页
{
    if(p == NULL)  return false;
 
  
    int maxValue = p->verticalScrollBar()->maximum(); // 当前SCROLLER最大显示值
    if(maxValue == 0)  return false;
    p->verticalScrollBar()->setSliderPosition(0);
}
 
  
BOOL CQTTableViewPageSwitch::pageEnd(QTableView *p)//末页
{
    if(p == NULL)  return false;
 
  
    int maxValue = p->verticalScrollBar()->maximum(); // 当前SCROLLER最大显示值
    if(maxValue == 0)  return false;
    p->verticalScrollBar()->setSliderPosition(maxValue);
}

你可能感兴趣的:(QT)