qt 下tablewidget 中要单独使一列不能编辑怎么办

2024-12-15 11:57:52
推荐回答(1个)
回答1:

修改该列item的flags. 例如:

#include
#include
#include
int main(int argc, char **argv)
{
QApplication app(argc, argv);
int col = 0;
QTableWidget *table = new QTableWidget(5, 3);
table->setItem(0, col, new QTableWidgetItem(QString("test")));
int i;
for(i = 0; i < table->rowCount(); i++){
QTableWidgetItem *item = table->item(i, col);
if(item) {
item->setFlags(item->flags() & (~Qt::ItemIsEditable));
} else {
item = new QTableWidgetItem;
item->setFlags(item->flags() & (~Qt::ItemIsEditable));
table->setItem(i, col, item);
}
}
table->show();
return app.exec();
}