1、在单元格添加控件(以QLabel 为例)
QWidget *pWidget = new QWidget(ui->TableWidget);
QHBoxLayout *pLayout = new QHBoxLayout(ui->TableWidget);
pLayout->setMargin(0);
QLabel *pLabel = new QLabel(ui->TableWidget);
pLabel->setObjectName("pLabel");
pLabel->setFixedSize(16, 16);
pLabel->setStyleSheet(QString("background:rgb(%1,%2,%3)").arg((devInfo->tSetInfo.u8Red == PTU_TEXT_COLOUR_USE) ? 255 : 0).arg((devInfo->tSetInfo.u8Green == PTU_TEXT_COLOUR_USE) ? 255 : 0).arg((devInfo->tSetInfo.u8Blue == PTU_TEXT_COLOUR_USE) ? 255 : 0));
pLayout->addWidget(pLabel);
pWidget->setLayout(pLayout);
ui->TableWidget->setCellWidget(ui->TableWidget->rowCount() - 1, 10, pWidget);
2、获取单元格控件(以QLabel 为例)
QWidget *pWidget = ui->TableWidget->cellWidget(i, 10);
if (pWidget)
{
QLabel *pLabel = pWidget->findChild<QLabel*>("pLabel");
if (pLabel) {
pLabel->setStyleSheet(QString("background:rgb(%1,%2,%3)").arg((devInfo->tSetInfo.u8Red == PTU_TEXT_COLOUR_USE) ? 255 : 0).arg((devInfo->tSetInfo.u8Green == PTU_TEXT_COLOUR_USE) ? 255 : 0).arg((devInfo->tSetInfo.u8Blue == PTU_TEXT_COLOUR_USE) ? 255 : 0));
}
}