例程 : 又是好风景 : http://blog.csdn.net/qiao_yihan/article/details/46413345
关键点:
1.QTableWidgetItem 的 setFlags() 和 flags()
2.QTableWidget 不能 设置 setEditTriggers() 为 QAbstractItemView::NoEditTriggers,否则1中的设置会失效
table_widget->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置不可编辑
代码:
int NumOfReg = 5; ui->tableWidget->setColumnCount(11); ui->tableWidget->setRowCount(NumOfReg); /* 设置 tableWidget */ ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "Id" << "Name" << "Unit" << "Value" << "Min" << "Max" << "Type" << "Mode" << "Enable" << "Last read" << "Last write"); ui->tableWidget->verticalHeader()->setVisible(false); // 隐藏水平header ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectItems); // 单个选中 ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection); // 可以选中多个 /* 设置 Id Name 字段的值不能修改 */ /* 设置不可修改的Id Name两列的背景颜色为灰色 */ for(int i = 0; i < NumOfReg; i++) { QTableWidgetItem *item = new QTableWidgetItem(); //item->setBackground(QBrush(QColor(Qt::lightGray))); item->setFlags(item->flags() & (~Qt::ItemIsEditable)); ui->tableWidget->setItem(i, 0, item); } for(int i = 0; i < NumOfReg; i++) { QTableWidgetItem *item = new QTableWidgetItem(); item->setBackground(QBrush(QColor(Qt::lightGray))); item->setFlags(item->flags() & (~Qt::ItemIsEditable)); ui->tableWidget->setItem(i, 1, item); }
工程文件:
http://download.csdn.net/detail/ewrest/9854319