#include "CUIComponentMultiTableWidget.h" #include #include #include #include #include #include #include CUIComponentMultiTableWidget::CUIComponentMultiTableWidget( CUIConfig* conf, QVector& subCUI, QWidget* parent) : CUIComponentBaseWidget(parent) { this->initUI(subCUI); } void CUIComponentMultiTableWidget::initUI(QVector& subCUI) { QStringList headers; headers << tr("index") << tr("name") << tr("alias") << tr("view"); QList> items; QList item1 = {tr("quaility"), "quaility"}; QList item2 = {tr("x"), "x"}; QList item3 = {tr("y"), "y"}; QList 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 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); }