介绍
qt做的分页控件
如何使用
创建
Pagination必须基于一个QWidget创建,否则会引发错误。
Pagination* pa = new Pagination(QWidget*);
设置总页数
Pagination需要设置一个总的页数,来初始化页码。
pa->SetTotalItem(count);
设置可选的每页数量
可以通过传给Pagination一个整形数组,来设置每页显示的条目数。
int args[4] = {1, 100, 200, 300};
pa->SetItemPerPage(args, 4);
刷新配置参数
当上面参数设置完成后,需要调用一次Refresh
来刷新配置。
pa->Refresh();
获取当前页数的变化
当Pagination当前的页码发生变化时,会产生一个PageChanged
信号,原型如下:
void PageChanged(int nPage);
其中,nPage代表要跳转的页数。
获取每页条目数的变化
当Pagination的每页数量发生变化时,会产生一个NumberPerPageChanged
信号,原型如下:
void NumberPerPageChanged(int nCount);
其中,nCount表示最新的每页条目数量。
#include "pagination.h"
#include <QPainter>
#include <QStyleOption>
#include <QListView>
#include <QKeyEvent>
//q:303103757 and v:qt5_qt6
Pagination::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;
}
}
void Pagination::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;
}
}
void Pagination::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;
}
}
void Pagination::Refresh()
{
Reload();
}
void Pagination::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 ... max
for(unsigned long long 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 + 2
int value = - 2;
for(unsigned long long 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(unsigned long long 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)
{
return dynamic_cast<QPushButton*>(m_pHPageLayout->itemAt(nPos)->widget());
}
void Pagination::Init()
{
setObjectName("pagination");
setStyleSheet(QStringLiteral("QWidget#pagination{background-color:transparent;}"));
m_pHMainLayout = new QHBoxLayout(this);
m_pHPageLayout = new QHBoxLayout;
m_pTotalItem = new TotalItem(this);
m_pItemEachPage = new EachPageItem(this);
m_pGotoItem = new GotoPageItem(this);
m_pBtnLeft = new QPushButton("<", this);
m_pBtnRight = new QPushButton(">", this);
m_pBtnOmitFront = new QPushButton(this);
m_pBtnOmitBack = new QPushButton(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 = new QPushButton(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);
});
}
void Pagination::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;
}
}
}
return nullptr;
}
void Pagination::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();
}
void Pagination::Reload()
{
ClearPage();
int lastPage = m_nCurrentPage;
m_nCurrentPage = 1;
m_pBtnOmitFront = new QPushButton(this);
m_pBtnOmitBack = new QPushButton(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 = new QPushButton(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);
});
}
void Pagination::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 = new QLabel("共 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()
{
}
void TotalItem::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());
}
int TotalItem::GetTotal() const
{
return m_nTotal;
}
void TotalItem::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 = new QComboBox(this);
m_pCbx->setFixedSize(100, 27);
setFixedSize(105, 30);
m_pCbx->setView(new QListView(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()
{
}
void EachPageItem::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) + "条/页");
}
}
}
int EachPageItem::GetCurrent()
{
unsigned long long idx = m_pCbx->currentIndex();
if(m_vecPage.size() >= idx + 1)
{
return m_vecPage[idx];
}
else return 0;
}
void EachPageItem::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 = new QHBoxLayout(this);
m_pLabel1 = new QLabel("前往", this);
m_pEditPage = new QLineEdit(this);
m_pLabel2 = new QLabel("页", 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 = new QRegExpValidator(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()
{
}
void GotoPageItem::paintEvent(QPaintEvent *event)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
QWidget::paintEvent(event);
}
bool GotoPageItem::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);
return true;
}
}
return QWidget::eventFilter(watched, event);
}
#ifndef PAGINATION_H
#define PAGINATION_H
#include <QWidget>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QComboBox>
#include <QLineEdit>
#define MINIMUM_PAGE 1
#ifndef MAX_BUTTON_COUNT
#define MAX_BUTTON_COUNT 6
#endif
#ifndef PAGE_STEP
#define PAGE_STEP 5
#endif
class TotalItem;
class EachPageItem;
class GotoPageItem;
class Pagination : public QWidget
{
Q_OBJECT
public:
explicit Pagination(QWidget *parent = nullptr);
~Pagination() override;
public:
/**
* @brief SetTotalItem 设置总条目的显示
* @param nTotal 总条目
*/
void SetTotalItem(int nTotal);
/**
* @brief SetItemPerPage 设置每页显示多少条目
* @param args 条目数组,数组元素必须大于0,且后面数必须大于前面
* @param size 数组大小
*/
void SetItemPerPage(int* args, int size);
/**
* @brief Refresh 设置完条目或者每页条目后必须调用该函数刷新
*/
void Refresh();
protected:
void Init();
void paintEvent(QPaintEvent* event) override;
private:
/**
* @brief FindPage 找到某页
* @param nIndex 要查找的页数,从1开始
* @return 返回找到的页数按钮,没有则返回空
*/
QPushButton* FindPage(int nIndex) const;
/**
* @brief ClearPage 清除所有页
*/
void ClearPage();
/**
* @brief ChangeView 修改页数的显示
* @param currentPage 当前页数
*/
void ChangeView(int currentPage);
/**
* @brief GetPageItem 获取m_pHPageLayout里面的第nPos按钮
* @param nPos 要获取的按钮的位置,nPos的索引从0开始
* @return 返回找到的按钮
*/
QPushButton* GetPageItem(int nPos);
/**
* @brief Reload 重新加载分页
*/
void Reload();
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:
void PageChanged(int nPage); // 页码变化
void NumberPerPageChanged(int nCount); // 每页条码变化
private slots:
void btn_page_clicked();
};
class TotalItem : public QWidget
{
Q_OBJECT
public:
explicit TotalItem(QWidget* parent = nullptr);
~TotalItem() override;
void SetTotal(int nTotal);
int GetTotal() const;
protected:
void paintEvent(QPaintEvent* event) override;
private:
QLabel* m_pLabel;
// QHBoxLayout* m_pHMainLayout;
int m_nTotal;
};
class EachPageItem : public QWidget
{
Q_OBJECT
public:
explicit EachPageItem(QWidget* parent = nullptr);
~EachPageItem() override;
void SetItemEachPage(int* args, int size);
int GetCurrent();
protected:
void paintEvent(QPaintEvent* event) override;
signals:
void EachPageItemChanged(int nCount);
private:
QComboBox* m_pCbx;
std::vector<int> m_vecPage;
};
class GotoPageItem : public QWidget
{
Q_OBJECT
public:
explicit GotoPageItem(QWidget* parent = nullptr);
~GotoPageItem() override;
protected:
void paintEvent(QPaintEvent* event) override;
bool eventFilter(QObject *watched, QEvent *event) override;
signals:
void GotoPage(int nPage);
private:
QHBoxLayout* m_pHMainLayout;
QLabel* m_pLabel1;
QLineEdit* m_pEditPage;
QLabel* m_pLabel2;
};
#endif // PAGINATION_H