125 lines
4.2 KiB
C++
125 lines
4.2 KiB
C++
|
|
#include "TearingDataTableWidget.h"
|
||
|
|
#include <QVBoxLayout>
|
||
|
|
#include <QHeaderView>
|
||
|
|
#include <QTableWidgetItem>
|
||
|
|
#include <QBrush>
|
||
|
|
#include <QColor>
|
||
|
|
|
||
|
|
TearingDataTableWidget::TearingDataTableWidget(QWidget *parent)
|
||
|
|
: QWidget(parent)
|
||
|
|
, m_tableWidget(nullptr)
|
||
|
|
{
|
||
|
|
setupUI();
|
||
|
|
setupTable();
|
||
|
|
}
|
||
|
|
|
||
|
|
TearingDataTableWidget::~TearingDataTableWidget()
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
void TearingDataTableWidget::setupUI()
|
||
|
|
{
|
||
|
|
QVBoxLayout *layout = new QVBoxLayout(this);
|
||
|
|
layout->setContentsMargins(0, 0, 0, 0);
|
||
|
|
|
||
|
|
m_tableWidget = new QTableWidget(this);
|
||
|
|
layout->addWidget(m_tableWidget);
|
||
|
|
|
||
|
|
setLayout(layout);
|
||
|
|
}
|
||
|
|
|
||
|
|
void TearingDataTableWidget::setupTable()
|
||
|
|
{
|
||
|
|
// 设置表头标签
|
||
|
|
QStringList headers;
|
||
|
|
headers << "ID" << "等级" << "状态" << "类型" /*<< "起始行" << "结束行" */<< "深度" << "宽度" << "长度" << "老化";
|
||
|
|
|
||
|
|
// 设置表格属性
|
||
|
|
m_tableWidget->setColumnCount(headers.size());
|
||
|
|
m_tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
|
||
|
|
m_tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
|
||
|
|
m_tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||
|
|
m_tableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
|
||
|
|
m_tableWidget->verticalHeader()->setVisible(false);
|
||
|
|
m_tableWidget->setHorizontalHeaderLabels(headers);
|
||
|
|
|
||
|
|
#if 0
|
||
|
|
m_tableWidget->horizontalHeader()->setStretchLastSection(true);
|
||
|
|
#else
|
||
|
|
// 启用交互式列宽调整
|
||
|
|
m_tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
|
||
|
|
|
||
|
|
// 设置列宽
|
||
|
|
m_tableWidget->setColumnWidth(0, 52); // ID列
|
||
|
|
m_tableWidget->setColumnWidth(1, 45); // 等级列
|
||
|
|
m_tableWidget->setColumnWidth(2, 80); // 状态列
|
||
|
|
m_tableWidget->setColumnWidth(3, 45); // 类型列
|
||
|
|
m_tableWidget->setColumnWidth(4, 80); // 深度列
|
||
|
|
m_tableWidget->setColumnWidth(5, 80); // 宽度列
|
||
|
|
m_tableWidget->setColumnWidth(6, 80); // 长度列
|
||
|
|
m_tableWidget->setColumnWidth(7, 80); // 老化列
|
||
|
|
#endif
|
||
|
|
// 设置样式表
|
||
|
|
m_tableWidget->setStyleSheet(
|
||
|
|
"QTableWidget {"
|
||
|
|
" background-color: rgb(37,38,42);"
|
||
|
|
" gridline-color: rgb(60,60,60);"
|
||
|
|
" selection-background-color: rgb(80,80,80);"
|
||
|
|
"}"
|
||
|
|
"QTableWidget::item {"
|
||
|
|
" color: white;"
|
||
|
|
"}"
|
||
|
|
"QHeaderView::section {"
|
||
|
|
" background-color: rgb(45,45,45);"
|
||
|
|
" color: white;"
|
||
|
|
" padding: 4px;"
|
||
|
|
"}"
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
void TearingDataTableWidget::addData(const TearingData &data)
|
||
|
|
{
|
||
|
|
// 将数据显示到QTableWidget上
|
||
|
|
int row = m_tableWidget->rowCount();
|
||
|
|
m_tableWidget->insertRow(row);
|
||
|
|
|
||
|
|
// 使用结构体数据填充表格
|
||
|
|
m_tableWidget->setItem(row, 0, new QTableWidgetItem(data.id));
|
||
|
|
m_tableWidget->setItem(row, 1, new QTableWidgetItem(data.level));
|
||
|
|
m_tableWidget->setItem(row, 2, new QTableWidgetItem(data.tearStatus));
|
||
|
|
m_tableWidget->setItem(row, 3, new QTableWidgetItem(data.tearType));
|
||
|
|
// m_tableWidget->setItem(row, 4, new QTableWidgetItem(data.statLineIdx));
|
||
|
|
// m_tableWidget->setItem(row, 5, new QTableWidgetItem(data.endLineIdx));
|
||
|
|
m_tableWidget->setItem(row, 4, new QTableWidgetItem(data.tearDepth));
|
||
|
|
m_tableWidget->setItem(row, 5, new QTableWidgetItem(data.tearWidth));
|
||
|
|
m_tableWidget->setItem(row, 6, new QTableWidgetItem(data.tearLength));
|
||
|
|
m_tableWidget->setItem(row, 7, new QTableWidgetItem(data.older));
|
||
|
|
|
||
|
|
// 设置item的文本颜色
|
||
|
|
for (int i = 0; i < m_tableWidget->horizontalHeader()->count(); i++) {
|
||
|
|
QTableWidgetItem *item = m_tableWidget->item(row, i);
|
||
|
|
if (item) {
|
||
|
|
item->setForeground(QBrush(Qt::white));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void TearingDataTableWidget::addData(const std::vector<TearingData> &dataList)
|
||
|
|
{
|
||
|
|
// 遍历vector中的所有数据并添加到表格
|
||
|
|
for (const auto &data : dataList) {
|
||
|
|
addData(data);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void TearingDataTableWidget::addData(const QJsonObject &json)
|
||
|
|
{
|
||
|
|
TearingData data = TearingData::fromJsonObject(json);
|
||
|
|
addData(data);
|
||
|
|
}
|
||
|
|
|
||
|
|
void TearingDataTableWidget::clearData()
|
||
|
|
{
|
||
|
|
m_tableWidget->setRowCount(0);
|
||
|
|
}
|