在QTableWidget上添加框选,获取框选状态
添加选项框在表格中
//添加选择框
QTableWidgetItem* check0 = new QTableWidgetItem();
check0->setCheckState(Qt::Checked);
ui->tableWidget_TestResult->setItem(0, 0, check0);
ui->tableWidget_TestResult->item(0, 0)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
QTableWidgetItem* check1 = new QTableWidgetItem();
check1->setCheckState(Qt::Checked);
ui->tableWidget_TestResult->setItem(1, 0, check1);
ui->tableWidget_TestResult->item(1, 0)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
QTableWidgetItem* check2 = new QTableWidgetItem();
check2->setCheckState(Qt::Checked);
ui->tableWidget_TestResult->setItem(2, 0, check2);
ui->tableWidget_TestResult->item(2, 0)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
获取框选状态:
bool status1 = ui->tableWidget_TestResult->item(0, 0)->checkState();
qDebug() << "message_updateTest1Status status" << status1;
bool status2 = ui->tableWidget_TestResult->item(1, 0)->checkState();
qDebug() << "message_updateTest2Status status" << status2;
在QTableWidget上更改颜色显示NG和OK
举例:设置
int TestNum = ****;
QString Eye = ****;
QString result = ****;
int hang,lie;
if (Eye == "LEFT")
lie = 1;
else if (Eye == "RIGHT")
{
lie = 2;
}
else
{
lie = 0;
qDebug() << "haatData.data Eye error!";
return false;
}
QTableWidgetItem* item = new QTableWidgetItem();
if (result == "OK")
{
item->setBackgroundColor(QColor(0, 255, 0)); //背景绿色
item->setText("OK");
}
else
{
item->setBackgroundColor(QColor(255, 0, 0)); //背景红色
item->setText("NG");
}
item->setTextColor(QColor(240, 248, 255)); //白色文字
//item->setTextColor();
ui->tableWidget_TestResult->setItem(TestNum-1, lie, item);
ui->tableWidget_TestResult->item(TestNum-1, lie)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);