Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a column in QTableWidget read only?

I would like to have one column in QTableWidget NOT editable.
In forums I have read a lot about some flags but could not manage to implement.

like image 963
Narek Avatar asked Apr 04 '10 10:04

Narek


People also ask

How do I make Qtablewidget not editable?

Normally you can press enter or F2? or double click to start editing a cell. this will disable that. It disables the users normal ways of start editing.


2 Answers

Insert into the QTableWidget following kind of items:

QTableWidgetItem *item = new QTableWidgetItem(); item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled); 

Works fine!

EDIT:

QTableWidgetItem *item = new QTableWidgetItem(); item->setFlags(item->flags() ^ Qt::ItemIsEditable); 

This is a better solution. Thanks to @priomsrb.

like image 109
Narek Avatar answered Sep 28 '22 17:09

Narek


The result of using XOR depends on what the current state is. I'd suggest using

item->setFlags(item->flags() &  ~Qt::ItemIsEditable); 

to make sure editing is turned off regardless of the current setting.

like image 36
user2923436 Avatar answered Sep 28 '22 17:09

user2923436