赞
踩
1.使用QTableWidget实现了首页,上下页切换,尾页,以及跳转。
widget.h #ifndef WIDGET_H #define WIDGET_H #include <QWidget> QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACE class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); void updataPage(); void updataPageData(); void initHeader(const QStringList &headers); void addLogData(const QVariantList &data); void addLogDatas(const QList<QVariantList> &datas); private slots: void on_pushButton_3_clicked(); void on_pushButton_clicked(); void on_pushButton_2_clicked(); void on_pushButton_4_clicked(); void on_pushButton_5_clicked(); private: Ui::Widget *ui; QList<QVariantList> m_lDatas; int currentPageSum; int pageSum; int currentPage; }; #endif // WIDGET_H
widget.cpp #include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); initHeader(QStringList() << "行号" << "内容"); currentPageSum = 50; currentPage = 1; for (int i = 1; i <= 100000; i++) { QVariantList varList; varList << i << QString("测试"); m_lDatas.push_back(varList); } addLogDatas(m_lDatas); } Widget::~Widget() { delete ui; } void Widget::updataPage() { if (m_lDatas.size() <= 50) { pageSum = 1; } if ((m_lDatas.size() % 50) == 0) { pageSum = m_lDatas.size() / 50; } else { pageSum = (m_lDatas.size() / 50) + 1; } ui->label->setText(QString("%1/%2").arg(currentPage).arg(pageSum)); } void Widget::updataPageData() { if (currentPage == 0) { return; } //获取具体下标 (当前页号*当前页的总数)-当前页的总数)= 某一页的第一个的具体数据下标 int index = (currentPage * currentPageSum) - currentPageSum; //删除 for(int row = ui->logTabWidget->rowCount() - 1;row >= 0; row--) { ui->logTabWidget->removeRow(row); } int i = 0; for (; index < m_lDatas.size(); ++index) { if (index >= (currentPage * currentPageSum)) { break; } int rowcount = ui->logTabWidget->rowCount(); ui->logTabWidget->insertRow(rowcount); QVariantList varList = m_lDatas.at(index); for (int j = 0; j < varList.size(); ++j) { QTableWidgetItem* item = new QTableWidgetItem(); item->setData(0, varList.at(j)); item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); ui->logTabWidget->setItem(i, j, item); } i++; } } void Widget::initHeader(const QStringList &headers) { ui->logTabWidget->setColumnCount(headers.size()); ui->logTabWidget->setHorizontalHeaderLabels(headers); ui->logTabWidget->verticalHeader()->setHidden(false); ui->logTabWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); ui->logTabWidget->setColumnWidth(0, 20); ui->logTabWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); //x先自适应宽度 QHeaderView *header = ui->logTabWidget->verticalHeader(); header->setHidden(true);// 隐藏行号 } void Widget::addLogData(const QVariantList &data) { m_lDatas.append(data); updataPageData(); updataPage(); } void Widget::addLogDatas(const QList<QVariantList> &datas) { //m_lDatas = datas; currentPage = 1; updataPageData(); updataPage(); } void Widget::on_pushButton_3_clicked() { //首页 currentPage = 1; updataPageData(); updataPage(); } void Widget::on_pushButton_clicked() { //上一页 if (currentPage == 1) { return; } currentPage--; updataPageData(); updataPage(); } void Widget::on_pushButton_2_clicked() { //下一页 if (currentPage >= pageSum) { return; } currentPage++; updataPageData(); updataPage(); } void Widget::on_pushButton_4_clicked() { //尾页 currentPage = pageSum; updataPageData(); updataPage(); } void Widget::on_pushButton_5_clicked() { //跳转 if (ui->spinBox->value() > pageSum || currentPage == ui->spinBox->value() || ui->spinBox->value() == 0 || pageSum == 1) { return; } currentPage = ui->spinBox->value(); updataPageData(); updataPage(); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。