qfedu-qt-level/Project02/operationallogwidget.cpp

93 lines
3.2 KiB
C++
Raw Normal View History

2023-08-13 11:47:00 +08:00
#include "operationallogwidget.h"
#include "ui_operationallogwidget.h"
OperationalLogWidget::OperationalLogWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::OperationalLogWidget)
{
ui->setupUi(this);
// ui->tableWidget->setLineWidth(500);
initTableWidgetFormat(); // 初始化表格
}
OperationalLogWidget::~OperationalLogWidget()
{
delete ui;
}
void OperationalLogWidget::initTableWidgetFormat()
{
ui->tableWidget->setColumnCount(2);
ui->tableWidget->setColumnWidth(0,505);
ui->tableWidget->setColumnWidth(1,505);
// 添加默认空行
ui->tableWidget->setRowCount(50); // 设置表格行数为50
// 获取水平标题栏
QHeaderView *table_header = ui->tableWidget->horizontalHeader();
// 设置标题栏的背景色
table_header->setStyleSheet("background-color: rgb(0, 255, 255);");
// 设置标题栏的字体
QFont font("黑体",14,QFont::Bold);
table_header->setFont(font);
// 设置标题栏的文本
table_header->setSectionResizeMode(QHeaderView::Stretch);
table_header->setSortIndicatorShown(true);
table_header->setSortIndicator(0, Qt::AscendingOrder);
table_header->setSectionsClickable(true);
table_header->setSectionsMovable(true);
table_header->setHighlightSections(true);
ui->tableWidget->setHorizontalHeader(table_header);
ui->tableWidget->setHorizontalHeaderLabels({"操作类型","操作时间"});
// 创建7行数据
QStringList operations = {"开机", "关机", "调试", "运行"};
// QStringList data = {"Data 1", "Data 2", "Data 3", "Data 4", "Data 5", "Data 6", "Data 7"};
// QDateTime currentTime = QDateTime::currentDateTime();
// 设置时间范围
QDateTime startTime = QDateTime::fromString("2019-01-01 00:00:00", "yyyy-MM-dd hh:mm:ss");
QDateTime endTime = QDateTime::fromString("2023-08-02 00:00:00", "yyyy-MM-dd hh:mm:ss");
for (int row = 0; row < 7; ++row) {
// 生成随机时间戳
qint64 randomTimestamp = QRandomGenerator::global()->generate64();
qint64 range = endTime.toSecsSinceEpoch() - startTime.toSecsSinceEpoch();
qint64 randomSeconds = randomTimestamp % range;
QDateTime randomTime = startTime.addSecs(randomSeconds);
// 将随机时间转换为文本格式
QString randomTimeString = randomTime.toString("yyyy-MM-dd hh:mm:ss");
// 设置操作时间
QTableWidgetItem *timeItem = new QTableWidgetItem(randomTimeString);
ui->tableWidget->setItem(row, 1, timeItem);
}
for (int row = 0; row < 7; ++row) {
// 设置操作类型
QTableWidgetItem *operationItem = new QTableWidgetItem(operations[row % operations.size()]);
ui->tableWidget->setItem(row, 0, operationItem);
// // 设置操作数据
// QTableWidgetItem *dataItem = new QTableWidgetItem(data[row]);
// ui->tableWidget->setItem(row, 1, dataItem);
// // 设置时间格式为"yyyy-MM-dd hh:mm:ss"
// QTableWidgetItem *timeItem = new QTableWidgetItem(currentTime.toString("yyyy-MM-dd hh:mm:ss"));
// ui->tableWidget->setItem(row, 2, timeItem);
// // 增加时间
// currentTime = currentTime.addSecs(60); // 增加60秒
}
}