? 本文程序效果如下:
代码如下
///.cpp
#include "QtableWidget.h"
QtableWidget::QtableWidget(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
ui.tableWidget->setShowGrid(true);
ui.tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
ui.tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
ui.tableWidget->setColumnCount(2);
ui.tableWidget->verticalHeader()->hide();//取消默认行号
QStringList header;
header << tr("No") << tr("Value");
ui.tableWidget->setHorizontalHeaderLabels(header);
m_nCurPage = 1;
m_nRowsPerPage = 7;
m_nTotalPages = 1;
InitTable();
connect(ui.pbtnBefore,SIGNAL(clicked()),this,SLOT(OnBtnPrePage())); //响应上一页
connect(ui.btnNext, SIGNAL(clicked()), this, SLOT(OnBtnNextPage()));//响应下一页
connect(ui.tableWidget->verticalScrollBar(), SIGNAL(valueChanged(int)),this,SLOT(OnSlideAction(int)));//响应滚动条翻页
}
void QtableWidget::InitTable()
{
for (int i = 0; i < 100;i++)
{
m_vecData.push_back(i);
}
//计算总页数-----本demo一个数据一行数据。若是一行多个数据,请按照多个数据计算页数。
if (m_vecData.size() % m_nRowsPerPage == 0) //计算总页数
{
m_nTotalPages = m_vecData.size() / m_nRowsPerPage;
}
else
{
m_nTotalPages = m_vecData.size() / m_nRowsPerPage + 1;
}
//插入表数据---本demo一个数据一行数据。若是一行多个数据,请按照数据个数插入表格数据。
ui.labPage->setText(QString("%1/%2").arg(m_nCurPage).arg(m_nTotalPages));
ui.tableWidget->setRowCount(m_nTotalPages* m_nRowsPerPage); //设置显示的行数
for (int i = 0; i < m_vecData.size(); i++)
{
ui.tableWidget->setItem(i, 0, new QTableWidgetItem(QString::number(m_vecData[i] + 1)));
ui.tableWidget->setItem(i, 1, new QTableWidgetItem(QString::number(m_vecData[i] + 1)));
}
}
void QtableWidget::OnBtnPrePage()
{
m_nCurPage--;
if (m_nCurPage <= 0)
{
m_nCurPage = m_nTotalPages;
}
ui.tableWidget->verticalScrollBar()->setSliderPosition(m_nCurPage*m_nRowsPerPage - m_nRowsPerPage);
ui.labPage->setText(QString("%1/%2").arg(m_nCurPage).arg(m_nTotalPages));
}
void QtableWidget::OnBtnNextPage()
{
m_nCurPage++;
if (m_nCurPage > m_nTotalPages)
{
m_nCurPage = 1;
}
ui.tableWidget->verticalScrollBar()->setSliderPosition(m_nCurPage*m_nRowsPerPage - m_nRowsPerPage);
ui.labPage->setText(QString("%1/%2").arg(m_nCurPage).arg(m_nTotalPages));
}
void QtableWidget::OnSlideAction(int nRowIndex)
{
qDebug() << "Row " << nRowIndex << endl;
++nRowIndex;
//计算当前行是那一页
if (nRowIndex % m_nRowsPerPage == 0)
{
m_nCurPage = nRowIndex / m_nRowsPerPage;
}
else
{
m_nCurPage = nRowIndex / m_nRowsPerPage+1;
}
ui.labPage->setText(QString("%1/%2").arg(m_nCurPage).arg(m_nTotalPages));
}
//.h//
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QtableWidget.h"
#include <qtablewidget.h>
#include <vector>
#include <qscrollbar.h>
#include <qdebug.h>
using namespace std;
class QtableWidget : public QMainWindow
{
Q_OBJECT
public:
QtableWidget(QWidget *parent = Q_NULLPTR);
//初始化数据
void InitTable();
public slots:
void OnBtnPrePage();
void OnBtnNextPage();
void OnSlideAction(int nRowIndex);
private:
Ui::QtableWidgetClass ui;
vector <int> m_vecData; //数据
int m_nCurPage; //当前页数
int m_nRowsPerPage; //每页显示的行数
int m_nTotalPages; //表格总页数
};
注:用到的滑动条的信号
[signal] void QAbstractSlider::valueChanged(int value)
This signal is emitted when the slider value has changed, with the new slider value as argument.
Note: Notifier signal for property value.
本文章部分代码参考于QTableWidget实现表格数据显示以及翻页效果_shuifei2020的博客-CSDN博客_qtablewidget翻页
|