柚子快報激活碼778899分享:開發(fā)語言 qt做的分頁控件
柚子快報激活碼778899分享:開發(fā)語言 qt做的分頁控件
介紹
qt做的分頁控件
如何使用
創(chuàng)建
Pagination必須基于一個QWidget創(chuàng)建,否則會引發(fā)錯誤。
Pagination* pa = new Pagination(QWidget*);
設(shè)置總頁數(shù)
Pagination需要設(shè)置一個總的頁數(shù),來初始化頁碼。
pa->SetTotalItem(count);
設(shè)置可選的每頁數(shù)量
可以通過傳給Pagination一個整形數(shù)組,來設(shè)置每頁顯示的條目數(shù)。
int args[4] = {1, 100, 200, 300};
pa->SetItemPerPage(args, 4);
刷新配置參數(shù)
當(dāng)上面參數(shù)設(shè)置完成后,需要調(diào)用一次Refresh來刷新配置。
pa->Refresh();
獲取當(dāng)前頁數(shù)的變化
當(dāng)Pagination當(dāng)前的頁碼發(fā)生變化時,會產(chǎn)生一個PageChanged信號,原型如下:
void PageChanged(int nPage);
其中,nPage代表要跳轉(zhuǎn)的頁數(shù)。
獲取每頁條目數(shù)的變化
當(dāng)Pagination的每頁數(shù)量發(fā)生變化時,會產(chǎn)生一個NumberPerPageChanged信號,原型如下:
void NumberPerPageChanged(int nCount);
其中,nCount表示最新的每頁條目數(shù)量。
#include "pagination.h"
#include
#include
#include
#include
//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,則只構(gòu)建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,則只構(gòu)建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)
{
/****************************************************************
* 改變頁數(shù)的幾種情形
* 1、顯示頁數(shù)(m_nDisplay)小于等于MAX_BUTTON_COUNT,則該函數(shù)不做任何處理。
* 2、顯示頁數(shù)(m_nDisplay)大于MAX_BUTTON_COUNT,開始處理
* (1).初始(currentPage < MAX_BUTTON_COUNT).
* ① currentPage != 5, [1] 2 3 4 5 6 ... max
* ② currentPage == 5, 此時應(yīng)當(dāng)放到(2)去處理,因為此時已經(jīng)形成 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); // 當(dāng)前第一頁,禁用左按鈕
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
}
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
{
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);
});
// 左省略號,當(dāng)作快退,即退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);
});
// 右省略號,當(dāng)作快進,即進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,則只構(gòu)建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
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) // 上次停留的頁數(shù)大于現(xiàn)在的最大頁數(shù)
m_nCurrentPage = m_nMaxPage; // 直接變成當(dāng)前最大頁數(shù)
else
m_nCurrentPage = lastPage; // 否則還是保持當(dāng)前頁數(shù)
ChangeView(m_nCurrentPage); // 再次刷新下當(dāng)前界面
// 左省略號,當(dāng)作快退,即退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);
});
// 右省略號,當(dāng)作快進,即進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
if(btn && btn != m_pBtnOmitFront && btn != m_pBtnOmitBack)
{
QString str = btn->text();
int nIndex = str.toInt(); // 要跳轉(zhuǎn)的頁數(shù)
QPushButton* page = FindPage(m_nCurrentPage); // 找到當(dāng)前頁數(shù)按鈕
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
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
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
#include
#include
#include
#include
#include
#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 設(shè)置總條目的顯示
* @param nTotal 總條目
*/
void SetTotalItem(int nTotal);
/**
* @brief SetItemPerPage 設(shè)置每頁顯示多少條目
* @param args 條目數(shù)組,數(shù)組元素必須大于0,且后面數(shù)必須大于前面
* @param size 數(shù)組大小
*/
void SetItemPerPage(int* args, int size);
/**
* @brief Refresh 設(shè)置完條目或者每頁條目后必須調(diào)用該函數(shù)刷新
*/
void Refresh();
protected:
void Init();
void paintEvent(QPaintEvent* event) override;
private:
/**
* @brief FindPage 找到某頁
* @param nIndex 要查找的頁數(shù),從1開始
* @return 返回找到的頁數(shù)按鈕,沒有則返回空
*/
QPushButton* FindPage(int nIndex) const;
/**
* @brief ClearPage 清除所有頁
*/
void ClearPage();
/**
* @brief ChangeView 修改頁數(shù)的顯示
* @param currentPage 當(dāng)前頁數(shù)
*/
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; // 存放頁數(shù)
QPushButton* m_pBtnLeft;
QPushButton* m_pBtnRight;
QPushButton* m_pBtnOmitFront; // 前面的省略號
QPushButton* m_pBtnOmitBack; // 后面的省略號
TotalItem* m_pTotalItem;
EachPageItem* m_pItemEachPage;
GotoPageItem* m_pGotoItem;
int m_nCurrentPage; // 當(dāng)前頁數(shù)
int m_nMaxPage; // 最大頁數(shù)
int m_nDisplayPage; // 顯示的頁數(shù)
std::vector
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
};
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
柚子快報激活碼778899分享:開發(fā)語言 qt做的分頁控件
精彩文章
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。