You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AppFlow/CFDStruct/CUIProperty/CUIComponentMultiTableWidge...

73 lines
2.8 KiB
C++

#include "CUIComponentMultiTableWidget.h"
#include <QCheckBox>
#include <QHeaderView>
#include <QList>
#include <QPair>
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QVBoxLayout>
CUIComponentMultiTableWidget::CUIComponentMultiTableWidget(
CUIConfig* conf, QVector<CUIPropertyWidget*>& subCUI, QWidget* parent)
: CUIComponentBaseWidget(parent)
{
this->initUI(subCUI);
}
void CUIComponentMultiTableWidget::initUI(QVector<CUIPropertyWidget*>& subCUI)
{
QStringList headers;
headers << tr("index") << tr("name") << tr("alias") << tr("view");
QList<QList<QString >> items;
QList<QString> item1 = {tr("quaility"), "quaility"};
QList<QString> item2 = {tr("x"), "x"};
QList<QString> item3 = {tr("y"), "y"};
QList<QString> item4 = {tr("z"), "z"};
items.append(item1);
items.append(item2);
items.append(item3);
items.append(item4);
// 根据配置来初始化tableWidget
m_tableWidget = new QTableWidget(this);
m_tableWidget->setColumnCount(items[0].count() + 2);
m_tableWidget->setRowCount(items.count());
m_tableWidget->setHorizontalHeaderLabels(headers);
// 设置水平表头的背景颜色
m_tableWidget->setStyleSheet(
QString(u8"QHeaderView::section{background-color: rgb(226,234,243); "
u8"color: black; border:1px solid rgb(255, 255, 255);}"));
m_tableWidget->setEditTriggers(
QAbstractItemView::NoEditTriggers); // 禁止编辑
m_tableWidget->setFocusPolicy(Qt::NoFocus);
// 隐藏垂直表头
QHeaderView* verticalHeader = m_tableWidget->verticalHeader();
verticalHeader->setVisible(false);
for (int i = 0; i < items.count(); i++) {
QList<QString> item = items[i];
// 序号
QTableWidgetItem* indexItem =
new QTableWidgetItem(QString("%1").arg(i + 1));
indexItem->setFlags(indexItem->flags() & ~Qt::ItemIsSelectable);
indexItem->setTextAlignment(Qt::AlignCenter); // 设置水平居中
m_tableWidget->setItem(i, 0, indexItem);
// 内容
for (int j = 0; j < item.count(); j++) {
QTableWidgetItem* nameItem = new QTableWidgetItem(item[j]);
nameItem->setFlags(indexItem->flags() & ~Qt::ItemIsSelectable);
nameItem->setTextAlignment(Qt::AlignCenter); // 设置水平居中
m_tableWidget->setItem(i, j + 1, nameItem);
}
// 复选框
QTableWidgetItem* checkBoxItem = new QTableWidgetItem();
m_tableWidget->setItem(i, item.count() + 1, checkBoxItem);
QCheckBox* checkBox = new QCheckBox();
checkBox->setStyleSheet(
"margin-left:45%; margin-right:45%;"); // 设置水平居中
m_tableWidget->setCellWidget(i, item.count() + 1, checkBox);
}
m_layout = new QVBoxLayout;
m_layout->addWidget(m_tableWidget);
this->setLayout(m_layout);
}