一、Table Widget控件基本参数设置
1 .设置表格列宽
int tableWidth = ui.tableWidget->width(); ui.tableWidget->setColumnWidth(0, 50); //设置列宽
2 .隐藏表格最左侧序列号
ui.tableWidget->verticalHeader()->setHidden(true);
3 .设置表格最左侧行数
ui.tableWidget->setItem(m_index, 0, new QTableWidgetItem(QString("%1").arg(m_num, 0, 'f', 0)));
4 .设置表格内容居中
ui.tableWidget->item(m_index, 0)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
5 .设置表格选中字体颜色和背景颜色
// 选中行颜色 ui.tableWidget->setStyleSheet("selection-background-color: red"); // 行背景色 ui.tableWidget->setStyleSheet("QTableView::Item{background-color:#B7B7B7}");
6 .设置表格是否充满(即行末不留空)以及表格列自动等宽
ui->tableWidget->horizontalHeader()->setStretchLastSection(true); ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
7 .删除所有行及其数据
while (ui.tableWidget->rowCount() > 0) { ui.tableWidget->setRowCount(0); }
二、实现逐行增加数据和删除数据
逐行增加数据:
int m_index = 0; double m_num = 1;
//获取总行数 m_index = ui.tableWidget->rowCount(); m_num = m_index + 1; //结果显示 for (m_index; m_index < m_num; m_index++) { //隐藏行列号 ui.tableWidget->verticalHeader()->setHidden(true); //设置最左侧行数 ui.tableWidget->setRowCount(m_index+1); ui.tableWidget->setItem(m_index, 0, new QTableWidgetItem(QString("%1").arg(m_num, 0, 'f', 0))); ui.tableWidget->setItem(m_index, 1, new QTableWidgetItem(QString("%1").arg(resultMountOffset->m_offsetCalibX, 0, 'f', 3))); ui.tableWidget->setItem(m_index, 2, new QTableWidgetItem(QString("%1").arg(resultMountOffset->m_offsetCalibY, 0, 'f', 3))); ui.tableWidget->setItem(m_index, 3, new QTableWidgetItem(QString("%1").arg(resultMountOffset->m_offsetCalibAngel, 0, 'f', 3))); //设置表格内容居中 ui.tableWidget->item(m_index, 0)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); ui.tableWidget->item(m_index, 1)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); ui.tableWidget->item(m_index, 2)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); ui.tableWidget->item(m_index, 3)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); }
删除选中行数据并更新序列:
//删除选中行 int rowIndex = ui.tableWidget->currentRow(); if (rowIndex != -1) ui.tableWidget->removeRow(rowIndex); //删除选中行后自动更新序列号 for (double index = 0; index < ui.tableWidget->rowCount(); index++) { ui.tableWidget->setItem(index, 0, new QTableWidgetItem(QString("%1").arg(index+1, 0, 'f', 0))); ui.tableWidget->item(index, 0)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); }
三、数据的导出保存成EXCEL
1.头文件.h
#include "qfiledialog.h" #include <QTextEdit>
2.实现槽函数
void GetMountOffset::slot_btnSaveDates() { ui.tableWidget->horizontalHeaderItem(0); //构建html文档内容 QString filepath = QFileDialog::getSaveFileName(this, tr("Save as..."), QString(), tr("EXCEL files (*.xls *.xlsx);;HTML-Files (*.htm *.html);;")); QString Begin = QString::fromLocal8Bit("<html><head></head><body><table border=\"1\" >"); QString end = QString::fromLocal8Bit("</table></body></html>"); QList<QString> list; int row = ui.tableWidget->rowCount(); int col = ui.tableWidget->columnCount(); for (int i = 0; i < row; i++) { QString rowStr = "<tr>"; for (int j = 0; j < col; j++) { QString cel = ui.tableWidget->item(i, j)->text(); rowStr += QString("<td>%1</td>").arg(cel); } rowStr += "</tr>"; list.push_back(rowStr); } QString text = Begin; for (int i = 0; i < list.size(); ++i) { text.append(list.at(i)); } text.append(end); QTextEdit textEdit; textEdit.setText(text); QFile file(filepath); if (file.open(QFile::WriteOnly | QIODevice::Text)) { QTextStream ts(&file); ts.setCodec("UTF-8"); ts << textEdit.document()->toHtml("UTF-8"); } }
数据导出参考:博客地址传送门
三、效果展示