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

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

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

测试用例

  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;"
            );
        }
    }
}

你可能感兴趣的:(c++,c++,开发语言)