QTableView表格翻页,Qt翻页基类的封装
简单粗暴,⽼规矩
#include “CTableviewpageswitch.h”
#ifndef CTABLEVIEWPAGESWITCH_H
#define CTABLEVIEWPAGESWITCH_H
#include<QtWidgets/QWidget>
#include<QtWidgets/QTableView>
#include<QtWidgets/QScrollBar>
#include<QEvent>
#include"cbasebutton.h"
#include"cbaselable.h"
#include"cbaselineedit.h"
class CTableViewPageSwitch :public QWidget
{
Q_OBJECT
public:
explicit CTableViewPageSwitch(QTableView *TableView,QWidget *parent =0);
void initpages();// 初始化翻页信息
private:
QTableView *m_TableView;
//    CBaseLable * T_pageTo; // 转到页数
//    CBaseLineEdit * L_pageTo;// 输⼊页数Edit
/
/    CBaseButton * B_pageTo;// Go按钮
CBaseLable * T_pageTip;// 页数提⽰
CBaseButton * pageup;// 上⼀页
CBaseButton * pagedown;// 下⼀页
int allpage;// 总页数
int curpage;// 当前页数
private:
int pageCount();//QTableView 总页数
signals:
private slots:
//        bool pageTo();//翻到指定页
bool pageUp();//上翻
bool pageDown();//下翻
};
#endif// CTABLEVIEWPAGESWITCH_H
#include “CTableviewpageswitch.cpp”
#include"CTableviewpageswitch.h"
CTableViewPageSwitch::CTableViewPageSwitch(QTableView *TableView, QWidget *parent):
QWidget(parent),
m_TableView(TableView),
curpage(1)
{
setFixedSize(1000,80);
allpage =pageCount();
//    setStyleSheet("bac");(BG_TicketRecharge,30,40,160,50,"票卡信息",40,QFont::Bold,"color:#000000");
//    T_pageTo = new CBaseLable(this,230,20,200,30,"转到页",16,QFont::Bold;color:#333333");
//    L_pageTo = new CBaseLineEdit(315,20,40,30,this);
//    L_pageTo->setStyleSheet("font:bold 20px;color:#333333;background-color:#ffffff;border:1px;border-style:solid;border-color:#4668cb;border-radius:4px ;");
L_pageTo->setTextMargins(10 , 1 , 1 , 1);
//    L_pageTo->setAlignment(Qt::AlignCenter);
//    connect(L_pageTo,SIGNAL(returnPressed()),this,SLOT(pageTo()));
//    B_pageTo = new CBaseButton(this,410,20,60,30,"Go","","background-color:#1a76d2;border-radi
us:4px;font:18px;color:#ffffff");
//    connect(B_pageTo,SIGNAL(clicked()),this,SLOT(pageTo()));
T_pageTip =new CBaseLable(this,150,10,300,40,"[当前1/1页]",24,QFont::Normal,"color:#333333");
T_pageTip->setText(QString("[当前%1/%2页]").arg(curpage).arg(allpage));
pageup =new CBaseButton(this,T_pageTip->x()+T_pageTip->width()+40,0,130,60,"上⼀页",24,QFont::Normal,"","background-color:#6156ed;border-rad ius:12px;color:#ffffff");
connect(pageup,SIGNAL(clicked()),this,SLOT(pageUp()));
pagedown =new CBaseButton(this,T_pageTip->x()+T_pageTip->width()*2,0,130,60,"下⼀页",24,QFont::Normal,"","background-color:#6156ed;border-ra dius:12px;color:#ffffff");
connect(pagedown,SIGNAL(clicked()),this,SLOT(pageDown()));
}
void CTableViewPageSwitch::initpages()
{
m_TableView->verticalScrollBar()->setSliderPosition(0);
//    L_pageTo->clear();
curpage =1;
allpage =pageCount();
T_pageTip->setText(QString("[当前%1/%2页]").arg(curpage).arg(allpage));
}
int CTableViewPageSwitch::pageCount()
{
if(m_TableView ==NULL)
return-1;
int rowCount = m_TableView->model()->rowCount();
int rowHeight = m_TableView->rowHeight(0);
int tableViewHeight = m_TableView->height();
int rowCountPerPage = tableViewHeight / rowHeight -1;//每页显⽰⾏数
int ret = rowCount / rowCountPerPage;
int tem = rowCount % rowCountPerPage;
if(tem !=0)
ret++;
return ret;
}
//bool CTableViewPageSwitch::pageTo()
/
/{
//    if(L_pageTo->text() == NULL || L_pageTo->text().toInt() == 0)
//        return false;
//    if(m_TableView == NULL)
//        return false;
//    int maxPage = pageCount();
//    if(L_pageTo->text().toInt() > maxPage)
//        return false;
//    int rowCount = m_TableView->model()->rowCount();
//    int rowHeight = m_TableView->rowHeight(0);
//    int tableViewHeight = m_TableView->height();
/
/    int tableViewHeight = m_TableView->height();
//    int rowCountPerPage = tableViewHeight / rowHeight - 1;  //每页显⽰⾏数
//    int canNotViewCount = rowCount - rowCountPerPage;  //看不见的⾏数
//    if(canNotViewCount == 0)
//        return false;
//    int maxValue = m_TableView->verticalScrollBar()->maximum();  // 当前SCROLLER最⼤显⽰值//    if(maxValue == 0)
//        return false;
//    int pageValue = (maxValue*rowCountPerPage) / canNotViewCount;
//    m_TableView->verticalScrollBar()->setSliderPosition(pageValue*(L_pageTo->text().toInt() - 1)); //    T_pageTip->setText(QString("[当前%1/%2页]").arg(L_pageTo->text().toInt()).arg(allpage));
//    curpage = L_pageTo->text().toInt();
//    return true;
//}
bool CTableViewPageSwitch::pageUp()
{
//    L_pageTo->clear();
if(m_TableView ==NULL)
return false;
int rowCount = m_TableView->model()->rowCount();
int rowHeight = m_TableView->rowHeight(0);
int tableViewHeight = m_TableView->height();
int rowCountPerPage = tableViewHeight / rowHeight -1;//每页显⽰⾏数
htmlborder
int canNotViewCount = rowCount - rowCountPerPage;//看不见的⾏数
if(canNotViewCount ==0)
return false;
int maxValue = m_TableView->verticalScrollBar()->maximum();// 当前SCROLLER最⼤显⽰值if(maxValue ==0)
return false;
int pageValue =(maxValue*rowCountPerPage)/ canNotViewCount;
int nCurScroller = m_TableView->verticalScrollBar()->value();//获得当前scroller值
if(nCurScroller>0)
{
m_TableView->verticalScrollBar()->setSliderPosition(nCurScroller - pageValue);
curpage--;
}
else
{
curpage = allpage;
m_TableView->verticalScrollBar()->setSliderPosition(maxValue);
}
T_pageTip->setText(QString("[当前%1/%2页]").arg(curpage).arg(allpage));
return true;
}
bool CTableViewPageSwitch::pageDown()
{
/
/    L_pageTo->clear();
if(m_TableView ==NULL)
return false;
int rowCount = m_TableView->model()->rowCount();
int rowHeight = m_TableView->rowHeight(0);
int tableViewHeight = m_TableView->height();
int rowCountPerPage = tableViewHeight / rowHeight -1;//每页显⽰⾏数
int canNotViewCount = rowCount - rowCountPerPage;//看不见的⾏数
if(canNotViewCount ==0)
return false;
int maxValue = m_TableView->verticalScrollBar()->maximum();// 当前SCROLLER最⼤显⽰值if(maxValue ==0)
return false;
int pageValue =(maxValue * rowCountPerPage)/ canNotViewCount;
int nCurScroller = m_TableView->verticalScrollBar()->value();//获得当前scroller值
if(nCurScroller<maxValue)
if(nCurScroller<maxValue)
{
m_TableView->verticalScrollBar()->setSliderPosition(nCurScroller + pageValue);
curpage++;
}
else
{
curpage =1;
m_TableView->verticalScrollBar()->setSliderPosition(0);
}
T_pageTip->setText(QString("[当前%1/%2页]").arg(curpage).arg(allpage));
return true;
}
使⽤:
TableView =new QTableView(BG_TicketTable);
TableView->setGeometry(QRect(30,180,1008,370));//设置Table的⼤⼩14,30,1140,505
connect(TableView,SIGNAL(clicked(QModelIndex)),this,SLOT(Slot_CodeTicketViewRowClicked(QModelIndex)));
tableViewPageSwitch =new CTableViewPageSwitch(TableView,BG_TicketTable);
tableViewPageSwitch->setGeometry(0,610,1000,80);
完结,撒花~