C++ 数组分页,经常有用到分页,索性做一个简单封装 已解决

在项目设计中, 有鼠标滑动需求,但是只能说能力有限,索性使用 php版本的数组分页,解决问题。 经常有用到分页,索性做一个简单封装、

测试用例

复制代码
  QTime curtime = QTime::currentTime();

        nHour = curtime.hour();
        nMin = curtime.minute();
        nSec = curtime.second();

  setTimeBtnWidget(nHour, 24, ceil(nHour / 6), 6,1, ui->verticalLayout_10);


  setTimeBtnWidget(nMin, 60, ceil(nMin / 6), 6, 2, ui->verticalLayout_2);


  setTimeBtnWidget(nSec, 60, ceil(nSec / 6), 6, 3, ui->verticalLayout_4);

使用方法

复制代码
	/**
	* @breif  : 设置 时间上下按钮
	* @param  : index 第几个
	* @param  : count 总数量
	* @param  : page  第几页
	* @param  : size  显示数量
	* @param  : type  类型
	* @param  : QLayout  布局名称
	* @return : void
	* @date   : 2024/01/10 16:28
	*/
	void setTimeBtnWidget(int index,int count,int page,int size,int type, QLayout* layout);

封装函数

复制代码
void CCalendarWidget::setTimeBtnWidget(int index,int count,int page,int size,int type, QLayout* layout)
{
    clearLayout(layout);
    
    int  tatakPage = (int)ceil(count / size);

    int offset = (page - 1) * size;
    for (int i = 0; i < 8; i++) {

        QPushButton* pBtn = new QPushButton();
     
        if (i == 0)
        {
            //星期widget
            QWidget* upBtnWidget = new QWidget;
            upBtnWidget->setMaximumWidth(40);
            //星期布局
            QHBoxLayout* layoutUpWidget = new QHBoxLayout(upBtnWidget);

            pBtn->setFixedSize(10, 6);
            pBtn->setChecked(true);
            pBtn->setStyleSheet(
                "border-image:url(:/images/Resources/images/CCalendarWidget/upBtn.png);border:none;font-size: 14px;color:#FFFFFF;");

            pBtn->setCursor(Qt::PointingHandCursor);

            layoutUpWidget->spacing();
            layoutUpWidget->addWidget(pBtn);
            layoutUpWidget->spacing();

            layout->addWidget(upBtnWidget);

            connect(pBtn, &QPushButton::clicked, [=]
            {
                if (timeIndex >= 0 && timeIndex <= tatakPage)
                {
                    setTimeBtnWidget(timeIndex * size+3,count, timeIndex, size, type, layout);
                    timeIndex--;
                }
                else {
                    timeIndex = 0;
                }
            });
        }

        if (i == 7)
        {   
            //星期widget
            QWidget* downBtnWidget = new QWidget;
            downBtnWidget->setMaximumWidth(40);
            //星期布局
            QHBoxLayout* layoutDownWidget = new QHBoxLayout(downBtnWidget);

            pBtn->setFixedSize(10, 6);
            pBtn->setChecked(true);
            pBtn->setStyleSheet(
                "border-image:url(:/images/Resources/images/CCalendarWidget/downBtn.png);border:none;font-size: 14px;color:#FFFFFF;");
            pBtn->setCursor(Qt::PointingHandCursor);

            layoutDownWidget->spacing();
            layoutDownWidget->addWidget(pBtn);
            layoutDownWidget->spacing();

            layout->addWidget(downBtnWidget);

            connect(pBtn, &QPushButton::clicked, [=]
            {
                if (timeIndex >= 0 && timeIndex < tatakPage)
                {
                    setTimeBtnWidget(timeIndex * size + 3,count, timeIndex,size, type, layout);
                    timeIndex++;
                }
                else {
                    timeIndex = tatakPage-1;
                }
            });
        }

        if (i > 0 && i < 7)
        {
            if ((type == 1 && page * size + i != 24) || (type == 2 && page * size + i != 60) || (type == 3 && page * size + i != 60))
            {
                pBtn->setChecked(true);

                connect(pBtn, &QPushButton::clicked, [=]
                    {
                        int index = pBtn->text().toInt();
                        setTimeBtnWidget(index, count, page, size, type, layout);

                        if (type == 1)
                        {
                            nHour = index;
                        }
                        if (type == 2)
                        {
                            nMin = index;
                        }
                        if (type == 3)
                        {
                            nSec = index;
                        }
                        setTime();
                    });
            }
            else {
                pBtn->setChecked(false);
            }
            pBtn->setFixedSize(40, 26);
            pBtn->setText(QString::number(page * size + i));
            pBtn->setCursor(Qt::PointingHandCursor);
            layout->addWidget(pBtn);
            layout->spacing();
        }

        if (page * size + i == index)
        {
            pBtn->setChecked(true);

            pBtn->setStyleSheet(
                "background:#0D85FF;"
            );
        }
    }
}
相关推荐
比昨天多敲两行18 分钟前
C++ 二叉搜索树
开发语言·c++·算法
Season45018 分钟前
C++11之正则表达式使用指南--[正则表达式介绍]|[regex的常用函数等介绍]
c++·算法·正则表达式
问好眼1 小时前
《算法竞赛进阶指南》0x04 二分-1.最佳牛围栏
数据结构·c++·算法·二分·信息学奥赛
Birdy_x1 小时前
接口自动化项目实战(1):requests请求封装
开发语言·前端·python
海海不瞌睡(捏捏王子)1 小时前
C++ 知识点概要
开发语言·c++
桌面运维家2 小时前
VLAN配置进阶:抑制广播风暴,提升网络效率
开发语言·网络·php
一轮弯弯的明月2 小时前
Python基础-速通秘籍(下)
开发语言·笔记·python·学习
西西学代码2 小时前
Flutter---回调函数
开发语言·javascript·flutter
大尚来也3 小时前
深入HashMap底层:从JDK1.7到1.8的架构演进与性能突围
开发语言
minji...3 小时前
Linux 进程信号(二)信号的保存,sigset_t,sigprocmask,sigpending
linux·运维·服务器·网络·数据结构·c++·算法