参考:Qt:tableWidget分页功能,并且插入控件_chen_jin_sheng的博客-CSDN博客_qtablewidget分页显示td
头文件:
#pragma once
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButtonUpPage_clicked();
void on_pushButtonNextPage_clicked();
void on_pushButtonFirstPage_clicked();
void on_pushButtonLastPage_clicked();
private:
void InitTableWidget();
void showPageData();
int getPageCount(const int row_count);
private slots:
void setCurPage(int n);
void setTotalPage(int n);
void on_lineEdit_textChanged(const QString &arg1);
private:
Ui::MainWindow *ui;
//数据库分页显示
int pageSize;
int curPage;
};
.cpp文件
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QCheckBox>
#include <QHBoxLayout>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
InitTableWidget();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::InitTableWidget()
{
pageSize = 10;
curPage = 0;
setCurPage(curPage);
ui->tableWidget->setColumnCount(2);
QStringList list;
list << "name" << "编号";
ui->tableWidget->setHorizontalHeaderLabels(list);
//插入数据
for(int i = 0; i < 55; i++)
{
int row_count = ui->tableWidget->rowCount();
ui->tableWidget->insertRow(row_count);
ui->tableWidget->setItem(i, 0, new QTableWidgetItem(QString::number(i+1) + "hhh"));
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);
}
showPageData();
}
void MainWindow::showPageData()
{
int rowCount = ui->tableWidget->rowCount();
if(curPage == 0)
{
ui->pushButtonUpPage->setDisabled(true);
ui->pushButtonFirstPage->setDisabled(true);
}
else
{
ui->pushButtonUpPage->setDisabled(false);
ui->pushButtonFirstPage->setDisabled(false);
}
if(curPage == getPageCount(rowCount) - 1)
{
ui->pushButtonNextPage->setDisabled(true);
ui->pushButtonLastPage->setDisabled(true);
}
else
{
ui->pushButtonNextPage->setDisabled(false);
ui->pushButtonLastPage->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);
}
}
int MainWindow::getPageCount(const int row_count)
{
int pagecount = 0;
if(row_count == 0)
{
return 0;
}
else if(row_count % pageSize == 0)
{
pagecount = row_count / pageSize;
}
else
{
pagecount = row_count / pageSize + 1;
}
setTotalPage(pagecount);
return pagecount;
}
void MainWindow::setCurPage(int n)
{
ui->labelCurPage->setText(QString("第 %1 页").arg(n+1));
}
void MainWindow::setTotalPage(int n)
{
ui->labelTotalPage->setText(QString("总共 %1 页").arg(n));
}
void MainWindow::on_pushButtonUpPage_clicked()
{
curPage--;
setCurPage(curPage);
showPageData();
}
void MainWindow::on_pushButtonNextPage_clicked()
{
curPage++;
setCurPage(curPage);
showPageData();
}
void MainWindow::on_pushButtonFirstPage_clicked()
{
curPage = 0;
setCurPage(curPage);
setCurPage(curPage);
showPageData();
}
void MainWindow::on_pushButtonLastPage_clicked()
{
curPage = getPageCount(ui->tableWidget->rowCount()) -1;
setCurPage(curPage);
showPageData();
}
void MainWindow::on_lineEdit_textChanged(const QString &arg1)
{
int turnpage = arg1.toInt();
if(turnpage > 0 && turnpage < getPageCount(ui->tableWidget->rowCount()) + 1)
{
curPage = turnpage -1;
setCurPage(curPage);
showPageData();
}
}
实现效果:
文章来源:https://www.toymoban.com/news/detail-610324.html
大佬写得很棒,很简单就实现了qtablewidget的分页功能,但是不知道会不会有什么问题,现在看起来是没问题的,加了个跳转功能,写得比较潦草文章来源地址https://www.toymoban.com/news/detail-610324.html
到了这里,关于Qt::实现qtablewidget分页功能的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!