当前位置:   article > 正文

qt做的分页控件

qt做的分页控件

介绍

qt做的分页控件

如何使用

创建

Pagination必须基于一个QWidget创建,否则会引发错误。

Pagination* pa = new Pagination(QWidget*);
  • 1

设置总页数

Pagination需要设置一个总的页数,来初始化页码。

pa->SetTotalItem(count);
  • 1

设置可选的每页数量

可以通过传给Pagination一个整形数组,来设置每页显示的条目数。

int args[4] = {1, 100, 200, 300};
pa->SetItemPerPage(args, 4);
  • 1
  • 2

刷新配置参数

当上面参数设置完成后,需要调用一次Refresh来刷新配置。

pa->Refresh();
  • 1

获取当前页数的变化

当Pagination当前的页码发生变化时,会产生一个PageChanged信号,原型如下:

void PageChanged(int nPage);
  • 1

其中,nPage代表要跳转的页数。

获取每页条目数的变化

当Pagination的每页数量发生变化时,会产生一个NumberPerPageChanged信号,原型如下:

void NumberPerPageChanged(int nCount);
  • 1

其中,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);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 543
  • 544
  • 545
  • 546
  • 547
  • 548
  • 549
  • 550
  • 551
  • 552
  • 553
  • 554
  • 555
  • 556
  • 557
  • 558
  • 559
  • 560
  • 561
  • 562
  • 563
  • 564
  • 565
  • 566
  • 567
  • 568
  • 569
  • 570
  • 571
  • 572
  • 573
  • 574
  • 575
#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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/911733
推荐阅读
相关标签
  

闽ICP备14008679号