[C++] 纯文本查看 复制代码 #include <QApplication>
#include <QWidget>
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QLabel>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QDebug>
#include <QObject>
#include <QStyledItemDelegate>
#include <QPainter>
#include <QMap>
// 创建自定义委托类
class CustomDelegate : public QStyledItemDelegate {
public:
CustomDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {}
// 设置特定单元格的边框颜色
void setCellBorderColor(const QModelIndex &index, const QColor &color) {
cellBorderColors[index] = color; // 使用字典存储每个单元格的颜色
}
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
// 绘制单元格内容
QStyledItemDelegate::paint(painter, option, index);
// 检查当前单元格是否有自定义边框颜色
QColor borderColor = QColor(0, 0, 0, 0); // 默认边框颜色为透明
if (cellBorderColors.contains(index)) {
borderColor = cellBorderColors[index]; // 获取自定义颜色
}
// 设置边框颜色和宽度
painter->setPen(QPen(borderColor, 2)); // 使用指定的边框颜色
// 获取矩形并绘制边框
QRect rect = option.rect; // 获取矩形
painter->drawRect(rect); // 绘制边框
}
private:
QMap<QModelIndex, QColor> cellBorderColors; // 存储每个单元格的边框颜色
};
class MyTableWidget : public QWidget {
public:
MyTableWidget(int _numOfChannel, int _column, QWidget *parent = nullptr)
: QWidget(parent), numOfChannel(_numOfChannel), column(_column), delegate(new CustomDelegate(this)) // 确保委托的生命周期
{
layout = new QVBoxLayout(this);
titleLabel = new QLabel("Title");
titleLabel->setAlignment(Qt::AlignCenter);
titleLabel->setStyleSheet("font-size: 24pt; background-color: #D3D3D3; font-weight: bold;");
layout->addWidget(titleLabel);
tableWidget = new QTableWidget(numOfChannel / column, column, this); // 设置行数和列数
layout->addWidget(tableWidget);
tableWidget->setFocusPolicy(Qt::NoFocus);
// 设置表格的行和列标题
QStringList rowLabels;
QStringList columnLabels;
for (int i = 1; i <= column; ++i) {
columnLabels << QString::number(i);
}
for (int i = 1; i <= numOfChannel / column; ++i) {
rowLabels << QString::number(i);
}
tableWidget->setVerticalHeaderLabels(rowLabels);
tableWidget->setHorizontalHeaderLabels(columnLabels);
// 填充表格数据
for (int row = 0; row < tableWidget->rowCount(); ++row) {
for (int column = 0; column < tableWidget->columnCount(); ++column) {
QTableWidgetItem *item = new QTableWidgetItem;
item->setText(QString::number(row * 16 + column + 1));
tableWidget->setItem(row, column, item);
}
}
// 设置表格样式
tableWidget->setStyleSheet("QTableWidget {"
" gridline-color: #FFFFFF;"
" border: 0px solid black;"
"}"
"QTableWidget::item {"
" padding: 2px;"
" background-color: #D3D3D3;"
" border: 1px solid #FFFFFF;"
"}"
"QTableWidget::item:selected {"
" background-color: #007ae6;"
"}");
// 设置委托
tableWidget->setItemDelegate(delegate);
// 连接信号与槽
connect(tableWidget, &QTableWidget::itemSelectionChanged, this, &MyTableWidget::onSelectionChanged);
}
void setLabelTitle(QString titleName) {
titleLabel->setText(titleName);
}
void setColumnNumber(int numOfChannel, int column) {
tableWidget->setRowCount(numOfChannel / column);
tableWidget->setColumnCount(column);
}
private slots:
void onSelectionChanged() {
QList<QTableWidgetItem*> selectedItems = tableWidget->selectedItems();
qDebug() << "Selected items:";
for (QTableWidgetItem *item : selectedItems) {
int row = item->row();
int column = item->column();
qDebug() << "Row:" << row << "Column:" << column;
delegate->setCellBorderColor(tableWidget->model()->index(row, column), QColor(32, 33, 36)); // 设置为绿色
}
tableWidget->update(); // 确保表格更新以重新绘制
}
private:
QTableWidget *tableWidget;
QLabel *titleLabel;
QVBoxLayout *layout;
int numOfChannel;
int column;
CustomDelegate *delegate; // 委托作为成员变量
};
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QWidget container;
QGridLayout *gridLayout = new QGridLayout(&container);
for (int i = 0; i < 8; ++i) {
MyTableWidget *myTableWidget = new MyTableWidget(128, 8);
gridLayout->addWidget(myTableWidget, i / 4, i % 4); // 将八个实例分布到2x4的网格中
}
container.setLayout(gridLayout);
container.setWindowTitle("Multiple Table Widgets");
container.resize(800, 600); // 设置窗口大小
container.show();
return a.exec();
}
|