其他分享
首页 > 其他分享> > Qt:tableWidget分页功能,并且插入控件

Qt:tableWidget分页功能,并且插入控件

作者:互联网

在tableWidget加入控件主要用

void QTableWidget::setCellWidget(int row, int column, QWidget *widget)

int rowCount = ui->tableWidget->rowCount();
QWidget *myWidget = new QWidget;
QVBoxLayout *hLayout = new QVBoxLayout();
QCheckBox *ckb = new QCheckBox();
//借助Layout来实现
hLayout->addWidget(ckb);
hLayout->setMargin(0);
//按钮居中
hLayout->setAlignment(ckb, Qt::AlignCenter);
myWidget->setLayout(hLayout);
//在rowCount行的第一列添加QCheckBox按钮
ui->tableWidget->setCellWidget(rowCount, 1, myWidget); 

分页的功能,主要的操作有首页,尾页,上一页,下一页,增加,删除等操作,

直接上代码:

#include "widget.h"
#include "ui_widget.h"
#include <QCheckBox>
#include <QVBoxLayout>
#include <QWidget>
#include <QPushButton>
#include <QTableWidgetItem>
#include <QDebug>

#define PAGE_VALUE 10  //一页显示10条数据

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    init_tableWidget();
    run_tableWidget();
}

void Widget::init_tableWidget()
{
    pageSize = PAGE_VALUE;
    curPage = 0;
    ui->tableWidget->setColumnCount(2);
    QStringList list;
    list << "name" << "statu";
    ui->tableWidget->setHorizontalHeaderLabels(list);
    //插入数据
    for(int i = 0; i < 50; i++)
    {
        int row_count = ui->tableWidget->rowCount();
        ui->tableWidget->insertRow(row_count);
        ui->tableWidget->setItem(i, 0, new QTableWidgetItem("cjs"));
        QWidget *myWidget = new QWidget;
        QVBoxLayout *hLayout = new QVBoxLayout();
        QCheckBox *ckb = new QCheckBox();
        hLayout->addWidget(ckb);
        hLayout->setMargin(0);
        hLayout->setAlignment(ckb, Qt::AlignCenter);
        myWidget->setLayout(hLayout);
        ui->tableWidget->setCellWidget(i, 1, myWidget);
    }
    showPageDate();
}

void Widget::run_tableWidget()
{
    connect(ui->upButton, &QPushButton::clicked, [=]()
    {
        curPage--;
        showPageDate();
    });
    connect(ui->nextButton, &QPushButton::clicked, [=]()
    {
        curPage++;
        showPageDate();
    });
    connect(ui->fristButton, &QPushButton::clicked, [=]()
    {
        curPage = 0;
        showPageDate();
    });
    connect(ui->finalButton, &QPushButton::clicked, [=]()
    {
        curPage = getPageCount(ui->tableWidget->rowCount()) - 1;
        showPageDate();
    });
    connect(ui->addButton, SIGNAL(clicked()), this, SLOT(addButton_click()));
    connect(ui->delButton, SIGNAL(clicked()), this, SLOT(delButton_click()));
}

//点击添加按钮
void Widget::addButton_click()
{
    int rowCount = ui->tableWidget->rowCount();
    ui->tableWidget->insertRow(rowCount);
    ui->tableWidget->setItem(rowCount, 0, new QTableWidgetItem("xxx"));
    QWidget *myWidget = new QWidget;
    QVBoxLayout *hLayout = new QVBoxLayout();
    QCheckBox *ckb = new QCheckBox();
    hLayout->addWidget(ckb);
    hLayout->setMargin(0);
    hLayout->setAlignment(ckb, Qt::AlignCenter);
    myWidget->setLayout(hLayout);
    ui->tableWidget->setCellWidget(rowCount, 1, myWidget);
    showPageDate();
    if(ui->tableWidget->rowCount() > 0)
        ui->delButton->setDisabled(false);
}
//点击删除,删除当前行
void Widget::delButton_click()
{
    if(ui->tableWidget->rowCount() == 0)
    {
        ui->delButton->setDisabled(true);
        return;
    }
    int curRow = ui->tableWidget->currentIndex().row();
    if(curRow >= 0 && curRow <= ui->tableWidget->rowCount())
        ui->tableWidget->removeRow(curRow);
    if(curPage * PAGE_VALUE >= ui->tableWidget->rowCount() - 1)
        curPage--;
    showPageDate();
}

//获取总的page页数
int Widget::getPageCount(int row_count)
{

    int pageCount = 0;
    if(row_count == 0)
        return pageCount = 0;
    if(row_count % pageSize == 0)
         pageCount = row_count / pageSize;
    else
        pageCount = row_count / pageSize + 1;
    return pageCount;
}

//显示当前页的数据
void Widget::showPageDate()
{
    int rowCount = ui->tableWidget->rowCount();
    if(curPage == 0)
    {
        ui->upButton->setDisabled(true);
        ui->fristButton->setDisabled(true);
    }
    else
    {
        ui->upButton->setDisabled(false);
        ui->fristButton->setDisabled(false);
    }

    if(curPage == getPageCount(rowCount) - 1)
    {
        ui->nextButton->setDisabled(true);
        ui->finalButton->setDisabled(true);
    }
    else
    {
        ui->nextButton->setDisabled(false);
        ui->finalButton->setDisabled(false);
    }

    int iStart = pageSize * curPage;
    int iEnd = 0;
    if(iStart + pageSize < rowCount)
        iEnd = iStart + pageSize;
    else
        iEnd = rowCount;
    for(int i = 0; i < rowCount; i++)
    {
        if(i >= iStart && i < iEnd)
            ui->tableWidget->setRowHidden(i, false);
        else
            ui->tableWidget->setRowHidden(i, true);
    }
}

Widget::~Widget()
{
    delete ui;
}

运行效果:

标签:控件,Qt,int,rowCount,tableWidget,ui,new,hLayout
来源: https://blog.csdn.net/chen_jin_sheng/article/details/121120858