介绍
qt做的分页控件
如何使用
创建
Pagination必须基于一个QWidget创建,否则会引发错误。
Pagination* pa =newPagination(QWidget*);
设置总页数
Pagination需要设置一个总的页数,来初始化页码。
pa->SetTotalItem(count);
设置可选的每页数量
可以通过传给Pagination一个整形数组,来设置每页显示的条目数。
int args[4]={1,100,200,300};
pa->SetItemPerPage(args,4);
刷新配置参数
当上面参数设置完成后,需要调用一次
Refresh
来刷新配置。
pa->Refresh();
获取当前页数的变化
当Pagination当前的页码发生变化时,会产生一个
PageChanged
信号,原型如下:
voidPageChanged(int nPage);
其中,nPage代表要跳转的页数。
获取每页条目数的变化
当Pagination的每页数量发生变化时,会产生一个
NumberPerPageChanged
信号,原型如下:
voidNumberPerPageChanged(int nCount);
其中,nCount表示最新的每页条目数量。
#include"pagination.h"#include<QPainter>#include<QStyleOption>#include<QListView>#include<QKeyEvent>//q:303103757 and v:qt5_qt6Pagination::Pagination(QWidget *parent):QWidget(parent),m_nCurrentPage(MINIMUM_PAGE),m_nMaxPage(MINIMUM_PAGE),m_nDisplayPage(0){setMinimumSize(800,50);Init();}Pagination::~Pagination(){if(m_pHPageLayout){delete m_pHPageLayout;
m_pHPageLayout =nullptr;}}voidPagination::SetTotalItem(int nTotal){
m_pTotalItem->SetTotal(nTotal);int EachPage = m_pItemEachPage->GetCurrent();if(EachPage >0){
m_nMaxPage = nTotal / EachPage;if(nTotal % EachPage >0)++m_nMaxPage;if(m_nMaxPage <=0) m_nMaxPage = MINIMUM_PAGE;// 如果m_nMaxPage大于MAX_BUTTON_COUNT,则只构建7个按钮,头、尾,中间5个if(m_nMaxPage > MAX_BUTTON_COUNT) m_nDisplayPage = MAX_BUTTON_COUNT +1;else m_nDisplayPage = m_nMaxPage;}}voidPagination::SetItemPerPage(int*args,int size){
m_pItemEachPage->SetItemEachPage(args, size);int EachPage = m_pItemEachPage->GetCurrent();if(EachPage >0){int nTotal = m_pTotalItem->GetTotal();
m_nMaxPage = nTotal / EachPage;if(nTotal % EachPage >0)++m_nMaxPage;if(m_nMaxPage <=0) m_nMaxPage = MINIMUM_PAGE;// 如果m_nMaxPage小于MAX_BUTTON_COUNT,则不变。// 如果m_nMaxPage大于MAX_BUTTON_COUNT,则只构建7个按钮,头、尾,中间5个if(m_nMaxPage > MAX_BUTTON_COUNT) m_nDisplayPage = MAX_BUTTON_COUNT +1;else m_nDisplayPage = m_nMaxPage;}}voidPagination::Refresh(){Reload();}voidPagination::ChangeView(int currentPage){/****************************************************************
* 改变页数的几种情形
* 1、显示页数(m_nDisplay)小于等于MAX_BUTTON_COUNT,则该函数不做任何处理。
* 2、显示页数(m_nDisplay)大于MAX_BUTTON_COUNT,开始处理
* (1).初始(currentPage < MAX_BUTTON_COUNT).
* ① currentPage != 5, [1] 2 3 4 5 6 ... max
* ② currentPage == 5, 此时应当放到(2)去处理,因为此时已经形成 1 ... 3 4 [5]的情况
* (2).(currentPage >= MAX_BUTTON_COUNT)。
* ① 两个省略号中间(currentPage < max - 3):1 ... 3 4 [5] 6 7 ... max
* ② 后省略号后面(currentPage >= max - 3): 1 ... 4 5 [6] 7 8 max
* ***************************************************************/if(currentPage == MINIMUM_PAGE)
m_pBtnLeft->setDisabled(true);// 当前第一页,禁用左按钮else
m_pBtnLeft->setDisabled(false);// 开启右按钮if(currentPage == m_nMaxPage)
m_pBtnRight->setDisabled(true);// 最后一页,禁用右按钮else
m_pBtnRight->setDisabled(false);// 开启左按钮if(m_nDisplayPage <= MAX_BUTTON_COUNT)return;// 1情况if(currentPage < MAX_BUTTON_COUNT && currentPage != MAX_BUTTON_COUNT -1)// 2.1情况{// 页面情况 1 2 3 4 5 6 ... maxfor(unsignedlonglong i =0; i < m_vecPageBtn.size(); i++){
QPushButton* btn = m_vecPageBtn[i];if(btn->isHidden()) btn->show();if(i == m_vecPageBtn.size()-1)
btn->setText(QString::number(m_nMaxPage));else
btn->setText(QString::number(i +1));
btn->adjustSize();if(btn->text().toInt()== currentPage)
btn->setChecked(true);else
btn->setChecked(false);}// 显示后省略号,屏蔽前省略if(!m_pBtnOmitFront->isHidden())
m_pBtnOmitFront->hide();if(m_pBtnOmitBack->isHidden())
m_pBtnOmitBack->show();}else{if(currentPage < m_nMaxPage -3)// 2.2.1情况{// 页面情况 currentPage - 2, currentPage - 1, [currentPage], currentPage + 1, currentPage + 2int value =-2;for(unsignedlonglong i =1; i <=5; i++){
QPushButton* btn = m_vecPageBtn[i];
btn->setText(QString::number(currentPage + value++));
btn->adjustSize();if(btn->text().toInt()== currentPage) btn->setChecked(true);else btn->setChecked(false);}// 开启两边省略号if(m_pBtnOmitFront->isHidden()) m_pBtnOmitFront->show();if(m_pBtnOmitBack->isHidden()) m_pBtnOmitBack->show();}else// 2.2.2情况{// 页面情况 1 ... max - 5, max - 4, [max - 3], max - 2, max - 1, max// 除了第一个不变,其它全变for(unsignedlonglong i =1; i < m_vecPageBtn.size(); i++){
QPushButton* btn = m_vecPageBtn[i];
btn->setText(QString::number(m_nMaxPage - MAX_BUTTON_COUNT + i));
btn->adjustSize();if(btn->text().toInt()== currentPage) btn->setChecked(true);else btn->setChecked(false);}// 显示前省略号,屏蔽后省略if(m_pBtnOmitFront->isHidden()) m_pBtnOmitFront->show();if(!m_pBtnOmitBack->isHidden()) m_pBtnOmitBack->hide();}}}
QPushButton *Pagination::GetPageItem(int nPos){returndynamic_cast<QPushButton*>(m_pHPageLayout->itemAt(nPos)->widget());}voidPagination::Init(){setObjectName("pagination");setStyleSheet(QStringLiteral("QWidget#pagination{background-color:transparent;}"));
m_pHMainLayout =newQHBoxLayout(this);
m_pHPageLayout =new QHBoxLayout;
m_pTotalItem =newTotalItem(this);
m_pItemEachPage =newEachPageItem(this);
m_pGotoItem =newGotoPageItem(this);
m_pBtnLeft =newQPushButton("<",this);
m_pBtnRight =newQPushButton(">",this);
m_pBtnOmitFront =newQPushButton(this);
m_pBtnOmitBack =newQPushButton(this);
m_vecPageBtn.reserve(10);
m_pBtnOmitFront->setStyleSheet(QStringLiteral("QPushButton{border:none;background-color:transparent;background:url(:/pagination/skin/ellipsis.png) no-repeat;background-position:center;}""QPushButton:hover{background:url(:/pagination/skin/FB.png) no-repeat;background-position:center;}"));
m_pBtnOmitBack->setStyleSheet(QStringLiteral("QPushButton{border:none;background-color:transparent;background:url(:/pagination/skin/ellipsis.png) no-repeat;background-position:center;}""QPushButton:hover{background:url(:/pagination/skin/FF.png) no-repeat;background-position:center;}"));
m_nCurrentPage =1;int args[4]={100,200,300,400};SetItemPerPage(args,sizeof(args)/sizeof(int));
m_pBtnOmitFront->hide();
m_pBtnOmitBack->hide();int btnWidth =0;for(int i =0; i < m_nDisplayPage; i++){if(i ==1) m_pHPageLayout->addWidget(m_pBtnOmitFront);if(i == m_nMaxPage -2&& i !=0) m_pHPageLayout->addWidget(m_pBtnOmitBack);
QPushButton* btn =newQPushButton(QString::number(i +1),this);
btn->setCheckable(true);
m_pHPageLayout->addWidget(btn);
m_vecPageBtn.emplace_back(btn);connect(btn,&QPushButton::clicked,this,&Pagination::btn_page_clicked);
btnWidth += btn->width();}for(auto& x :findChildren<QPushButton*>()){
x->setMinimumSize(32,32);
x->setCursor(QCursor(Qt::PointingHandCursor));if(x != m_pBtnOmitFront && x != m_pBtnOmitBack){
x->setStyleSheet(QStringLiteral("QPushButton{font-size:15px;font-weight:bold;border:none;background-color:transparent;color:black;}""QPushButton:hover{color:#409EFF;}""QPushButton:pressed{color:#409EFF;}""QPushButton:checked{color:#409EFF;}""QPushButton:disabled{color:#BCBEC2;}"));}}ChangeView(m_nCurrentPage);
m_pHMainLayout->addWidget(m_pTotalItem);
m_pHMainLayout->addWidget(m_pItemEachPage, Qt::AlignCenter);
m_pHMainLayout->addWidget(m_pBtnLeft);
m_pHMainLayout->addLayout(m_pHPageLayout);
m_pHMainLayout->addWidget(m_pBtnRight);
m_pHMainLayout->addWidget(m_pGotoItem);
m_pHMainLayout->addStretch();// 上一页connect(m_pBtnLeft,&QPushButton::clicked,this,[this](){if(m_nCurrentPage -1<0) m_nCurrentPage =1;else--m_nCurrentPage;ChangeView(m_nCurrentPage);
emit PageChanged(m_nCurrentPage);});// 下一页connect(m_pBtnRight,&QPushButton::clicked,this,[&](){if(m_nCurrentPage +1> m_nMaxPage) m_nCurrentPage = m_nMaxPage;else++m_nCurrentPage;ChangeView(m_nCurrentPage);
emit PageChanged(m_nCurrentPage);});// 左省略号,当作快退,即退m_nDisplay页connect(m_pBtnOmitFront,&QPushButton::clicked,this,[&](){if(m_nCurrentPage - PAGE_STEP < MINIMUM_PAGE) m_nCurrentPage = MINIMUM_PAGE;else m_nCurrentPage -= PAGE_STEP;ChangeView(m_nCurrentPage);
emit PageChanged(m_nCurrentPage);});// 右省略号,当作快进,即进m_nDisplay页connect(m_pBtnOmitBack,&QPushButton::clicked,this,[&](){if(m_nCurrentPage + PAGE_STEP > m_nMaxPage) m_nCurrentPage = m_nMaxPage;else m_nCurrentPage += PAGE_STEP;ChangeView(m_nCurrentPage);
emit PageChanged(m_nCurrentPage);});// 前往第N页connect(m_pGotoItem,&GotoPageItem::GotoPage,this,[&](int nPage){if(nPage > m_nMaxPage) nPage = m_nMaxPage;if(nPage < MINIMUM_PAGE) nPage = MINIMUM_PAGE;if(nPage +1> m_nMaxPage) m_nCurrentPage = m_nMaxPage;else m_nCurrentPage = nPage;ChangeView(m_nCurrentPage);
emit PageChanged(m_nCurrentPage);});// 每页条码变化connect(m_pItemEachPage,&EachPageItem::EachPageItemChanged,this,[&](int nCount){int EachPage = m_pItemEachPage->GetCurrent();if(EachPage >0){int nTotal = m_pTotalItem->GetTotal();
m_nMaxPage = nTotal / EachPage;if(nTotal % EachPage >0)++m_nMaxPage;if(m_nMaxPage <=0)
m_nMaxPage = MINIMUM_PAGE;// 如果m_nMaxPage小于MAX_BUTTON_COUNT,则不变。// 如果m_nMaxPage大于MAX_BUTTON_COUNT,则只构建7个按钮,头、尾,中间5个if(m_nMaxPage > MAX_BUTTON_COUNT)
m_nDisplayPage = MAX_BUTTON_COUNT +1;else
m_nDisplayPage = m_nMaxPage;}Reload();
emit NumberPerPageChanged(nCount);});}voidPagination::paintEvent(QPaintEvent *event){
QStyleOption opt;
opt.init(this);
QPainter p(this);style()->drawPrimitive(QStyle::PE_Widget,&opt,&p,this);QWidget::paintEvent(event);}
QPushButton *Pagination::FindPage(int nIndex)const{for(int i =0; i < m_pHPageLayout->count(); i++){
QLayoutItem* item = m_pHPageLayout->itemAt(i);if(item){
QPushButton* btn =qobject_cast<QPushButton*>(item->widget());if(btn && btn->text().compare(QString::number(nIndex))==0){return btn;}}}returnnullptr;}voidPagination::ClearPage(){int sz = m_pHPageLayout->count();for(int i =0; i < sz; i++){
QLayoutItem* item = m_pHPageLayout->takeAt(0);
QWidget* tmp = item->widget();
m_pHPageLayout->removeWidget(tmp);delete tmp;
tmp =nullptr;delete item;
item =nullptr;}
m_vecPageBtn.clear();}voidPagination::Reload(){ClearPage();int lastPage = m_nCurrentPage;
m_nCurrentPage =1;
m_pBtnOmitFront =newQPushButton(this);
m_pBtnOmitBack =newQPushButton(this);
m_pBtnOmitFront->setStyleSheet(QStringLiteral("QPushButton{border:none;background-color:transparent;background:url(:/pagination/skin/ellipsis.png) no-repeat;background-position:center;}""QPushButton:hover{background:url(:/pagination/skin/FB.png) no-repeat;background-position:center;}"));
m_pBtnOmitBack->setStyleSheet(QStringLiteral("QPushButton{border:none;background-color:transparent;background:url(:/pagination/skin/ellipsis.png) no-repeat;background-position:center;}""QPushButton:hover{background:url(:/pagination/skin/FF.png) no-repeat;background-position:center;}"));
m_pBtnOmitFront->setCursor(QCursor(Qt::PointingHandCursor));
m_pBtnOmitBack->setCursor(QCursor(Qt::PointingHandCursor));
m_pBtnOmitFront->hide();
m_pBtnOmitBack->hide();for(int i =0; i < m_nDisplayPage; i++){
QPushButton* btn =newQPushButton(QString::number(i +1),this);
btn->setCheckable(true);
m_pHPageLayout->addWidget(btn);
btn->setMinimumSize(32,32);
btn->setCursor(QCursor(Qt::PointingHandCursor));
btn->setStyleSheet(QStringLiteral("QPushButton{font-size:15px;font-weight:bold;border:none;background-color:transparent;color:black;}""QPushButton:hover{color:#409EFF;}""QPushButton:pressed{color:#409EFF;}""QPushButton:checked{color:#409EFF;}""QPushButton:disabled{color:#BCBEC2;}"));connect(btn,&QPushButton::clicked,this,&Pagination::btn_page_clicked);
m_vecPageBtn.emplace_back(btn);}
m_pHPageLayout->insertWidget(1, m_pBtnOmitFront);
m_pHPageLayout->insertWidget(m_pHPageLayout->count()-1, m_pBtnOmitBack);// 初始化一次界面ChangeView(m_nCurrentPage);if(lastPage > m_nMaxPage)// 上次停留的页数大于现在的最大页数
m_nCurrentPage = m_nMaxPage;// 直接变成当前最大页数else
m_nCurrentPage = lastPage;// 否则还是保持当前页数ChangeView(m_nCurrentPage);// 再次刷新下当前界面// 左省略号,当作快退,即退PAGE_STEP页connect(m_pBtnOmitFront,&QPushButton::clicked,this,[&](){if(m_nCurrentPage - PAGE_STEP < MINIMUM_PAGE) m_nCurrentPage = MINIMUM_PAGE;else m_nCurrentPage -= PAGE_STEP;ChangeView(m_nCurrentPage);
emit PageChanged(m_nCurrentPage);});// 右省略号,当作快进,即进PAGE_STEP页connect(m_pBtnOmitBack,&QPushButton::clicked,this,[&](){if(m_nCurrentPage + PAGE_STEP > m_nMaxPage) m_nCurrentPage = m_nMaxPage;else m_nCurrentPage += PAGE_STEP;ChangeView(m_nCurrentPage);
emit PageChanged(m_nCurrentPage);});}voidPagination::btn_page_clicked(){
QPushButton* btn =qobject_cast<QPushButton*>(sender());if(btn && btn != m_pBtnOmitFront && btn != m_pBtnOmitBack){
QString str = btn->text();int nIndex = str.toInt();// 要跳转的页数
QPushButton* page =FindPage(m_nCurrentPage);// 找到当前页数按钮if(page) page->setChecked(false);// 取消选中
QPushButton* nPage =FindPage(nIndex);if(nPage) nPage->setChecked(true);
m_nCurrentPage = nIndex;ChangeView(m_nCurrentPage);
emit PageChanged(nIndex);}}//TotalItem/TotalItem::TotalItem(QWidget *parent):QWidget(parent),m_nTotal(0){
m_pLabel =newQLabel("共 0 条",this);setObjectName("total_item");setStyleSheet(QStringLiteral("QWidget#total_item{background-color:transparent;}"));
m_pLabel->setStyleSheet("QLabel{font-family:Microsoft Yahei;font-size:14px;}");
m_pLabel->adjustSize();setMinimumSize(m_pLabel->size().width(), m_pLabel->size().height());
m_pLabel->move(0,(height()- m_pLabel->height())/2);}TotalItem::~TotalItem(){}voidTotalItem::SetTotal(int nTotal){if(nTotal <0)return;
m_nTotal = nTotal;
QString str =QString::number(nTotal);
m_pLabel->setText("共 "+ str +" 条");
m_pLabel->adjustSize();setMinimumSize(m_pLabel->size().width(), m_pLabel->size().height());}intTotalItem::GetTotal()const{return m_nTotal;}voidTotalItem::paintEvent(QPaintEvent *event){
QStyleOption opt;
opt.init(this);
QPainter p(this);style()->drawPrimitive(QStyle::PE_Widget,&opt,&p,this);QWidget::paintEvent(event);}///EachPageItem//EachPageItem::EachPageItem(QWidget *parent):QWidget(parent){setObjectName("per_page_item");setStyleSheet(QStringLiteral("QWidget#per_page_item{background-color:transparent;}"));
m_pCbx =newQComboBox(this);
m_pCbx->setFixedSize(100,27);setFixedSize(105,30);
m_pCbx->setView(newQListView(this));
m_pCbx->view()->setFocusPolicy(Qt::NoFocus);
m_pCbx->setStyleSheet(QStringLiteral("QComboBox{text-align:center;font-size:15px;border:1px solid #DCDFE6;border-radius:5px;}""QComboBox:hover{border:1px solid #409EFF;}""QComboBox::drop-down{background-color:transparent;}""QComboBox::down-arrow{image:url(:/pagination/skin/drop_down.png);}""QComboBox::down-arrow:on{image:url(:/pagination/skin/drop_up.png);}""QComboBox QAbstractItemView{text-align:center;font-weight:bold;border:none;font-size:14px;outline:0px;}""QComboBox QAbstractItemView::item{border:none;color:#606266;border:none;height:30px;}""QComboBox QAbstractItemView::item:selected{color:#409EFF;background:rgb(245,247,250);}"));
m_pCbx->setCursor(QCursor(Qt::PointingHandCursor));
m_pCbx->move(width()/2- m_pCbx->width()/2,height()/2- m_pCbx->height()/2);
m_vecPage.reserve(5);connect(m_pCbx,QOverload<int>::of(&QComboBox::currentIndexChanged),this,[&](int index){
emit EachPageItemChanged(m_vecPage[index]);});}EachPageItem::~EachPageItem(){}voidEachPageItem::SetItemEachPage(int*args,int size){
m_vecPage.clear();
m_pCbx->clear();
m_vecPage.reserve(5);for(int i =0; i < size; i++){int n = args[i];if(n >0){if(i >0){if(n <= m_vecPage[i -1])continue;}
m_vecPage.emplace_back(n);
m_pCbx->addItem(QString::number(n)+"条/页");}}}intEachPageItem::GetCurrent(){unsignedlonglong idx = m_pCbx->currentIndex();if(m_vecPage.size()>= idx +1){return m_vecPage[idx];}elsereturn0;}voidEachPageItem::paintEvent(QPaintEvent *event){
QStyleOption opt;
opt.init(this);
QPainter p(this);style()->drawPrimitive(QStyle::PE_Widget,&opt,&p,this);QWidget::paintEvent(event);}//GotoPageItem///GotoPageItem::GotoPageItem(QWidget *parent):QWidget(parent){setObjectName("goto_page_item");setStyleSheet(QStringLiteral("QWidget#goto_page_item{background-color:transparent;}"));
m_pHMainLayout =newQHBoxLayout(this);
m_pLabel1 =newQLabel("前往",this);
m_pEditPage =newQLineEdit(this);
m_pLabel2 =newQLabel("页",this);
m_pLabel1->setStyleSheet(QStringLiteral("QLabel{font-size:15px;font-family:Microsoft Yahei;}"));
m_pEditPage->setStyleSheet(QStringLiteral("QLineEdit{font-size:15px;font-family:Microsoft Yahei;border-radius:5px;border:1px solid #DCDFE6;}""QLineEdit:hover{border:1px solid #C0C4CC}""QLineEdit:focus{border:1px solid #409EFF;}"));
m_pLabel2->setStyleSheet(QStringLiteral("QLabel{font-size:15px;font-family:Microsoft Yahei;}"));
m_pLabel1->adjustSize();
m_pLabel2->adjustSize();
m_pEditPage->setFixedSize(60,30);
QRegExp reg("[0-9]+$");
QValidator* va =newQRegExpValidator(reg, m_pEditPage);
m_pEditPage->setValidator(va);
m_pHMainLayout->addWidget(m_pLabel1);
m_pHMainLayout->addWidget(m_pEditPage);
m_pHMainLayout->addWidget(m_pLabel2);
m_pHMainLayout->addStretch();
m_pHMainLayout->setContentsMargins(0,0,0,0);
m_pEditPage->installEventFilter(this);}GotoPageItem::~GotoPageItem(){}voidGotoPageItem::paintEvent(QPaintEvent *event){
QStyleOption opt;
opt.init(this);
QPainter p(this);style()->drawPrimitive(QStyle::PE_Widget,&opt,&p,this);QWidget::paintEvent(event);}boolGotoPageItem::eventFilter(QObject *watched, QEvent *event){if(event->type()== QKeyEvent::KeyRelease){
Qt::Key k =static_cast<Qt::Key>((dynamic_cast<QKeyEvent*>(event)->key()));if(k == Qt::Key_Return || k == Qt::Key_Enter){
QString str = m_pEditPage->text();int nPage =1;if(!str.isEmpty()) nPage = str.toInt();
emit GotoPage(nPage);returntrue;}}returnQWidget::eventFilter(watched, event);}
#ifndefPAGINATION_H#definePAGINATION_H#include<QWidget>#include<QHBoxLayout>#include<QPushButton>#include<QLabel>#include<QComboBox>#include<QLineEdit>#defineMINIMUM_PAGE1#ifndefMAX_BUTTON_COUNT#defineMAX_BUTTON_COUNT6#endif#ifndefPAGE_STEP#definePAGE_STEP5#endifclassTotalItem;classEachPageItem;classGotoPageItem;classPagination:publicQWidget{
Q_OBJECT
public:explicitPagination(QWidget *parent =nullptr);~Pagination()override;public:/**
* @brief SetTotalItem 设置总条目的显示
* @param nTotal 总条目
*/voidSetTotalItem(int nTotal);/**
* @brief SetItemPerPage 设置每页显示多少条目
* @param args 条目数组,数组元素必须大于0,且后面数必须大于前面
* @param size 数组大小
*/voidSetItemPerPage(int* args,int size);/**
* @brief Refresh 设置完条目或者每页条目后必须调用该函数刷新
*/voidRefresh();protected:voidInit();voidpaintEvent(QPaintEvent* event)override;private:/**
* @brief FindPage 找到某页
* @param nIndex 要查找的页数,从1开始
* @return 返回找到的页数按钮,没有则返回空
*/
QPushButton*FindPage(int nIndex)const;/**
* @brief ClearPage 清除所有页
*/voidClearPage();/**
* @brief ChangeView 修改页数的显示
* @param currentPage 当前页数
*/voidChangeView(int currentPage);/**
* @brief GetPageItem 获取m_pHPageLayout里面的第nPos按钮
* @param nPos 要获取的按钮的位置,nPos的索引从0开始
* @return 返回找到的按钮
*/
QPushButton*GetPageItem(int nPos);/**
* @brief Reload 重新加载分页
*/voidReload();private:
QHBoxLayout* m_pHMainLayout;
QHBoxLayout* m_pHPageLayout;// 存放页数
QPushButton* m_pBtnLeft;
QPushButton* m_pBtnRight;
QPushButton* m_pBtnOmitFront;// 前面的省略号
QPushButton* m_pBtnOmitBack;// 后面的省略号
TotalItem* m_pTotalItem;
EachPageItem* m_pItemEachPage;
GotoPageItem* m_pGotoItem;int m_nCurrentPage;// 当前页数int m_nMaxPage;// 最大页数int m_nDisplayPage;// 显示的页数
std::vector<QPushButton*> m_vecPageBtn;
signals:voidPageChanged(int nPage);// 页码变化voidNumberPerPageChanged(int nCount);// 每页条码变化private slots:voidbtn_page_clicked();};classTotalItem:publicQWidget{
Q_OBJECT
public:explicitTotalItem(QWidget* parent =nullptr);~TotalItem()override;voidSetTotal(int nTotal);intGetTotal()const;protected:voidpaintEvent(QPaintEvent* event)override;private:
QLabel* m_pLabel;// QHBoxLayout* m_pHMainLayout;int m_nTotal;};classEachPageItem:publicQWidget{
Q_OBJECT
public:explicitEachPageItem(QWidget* parent =nullptr);~EachPageItem()override;voidSetItemEachPage(int* args,int size);intGetCurrent();protected:voidpaintEvent(QPaintEvent* event)override;
signals:voidEachPageItemChanged(int nCount);private:
QComboBox* m_pCbx;
std::vector<int> m_vecPage;};classGotoPageItem:publicQWidget{
Q_OBJECT
public:explicitGotoPageItem(QWidget* parent =nullptr);~GotoPageItem()override;protected:voidpaintEvent(QPaintEvent* event)override;booleventFilter(QObject *watched, QEvent *event)override;
signals:voidGotoPage(int nPage);private:
QHBoxLayout* m_pHMainLayout;
QLabel* m_pLabel1;
QLineEdit* m_pEditPage;
QLabel* m_pLabel2;};#endif// PAGINATION_H
版权归原作者 vqt5_qt6 所有, 如有侵权,请联系我们删除。