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.
Asked
Active
Viewed 7.6k times
69
![](../../users/profiles/4284627.webp)
Donald Duck
- 6,488
- 18
- 59
- 79
![](../../users/profiles/163394.webp)
Narek
- 35,407
- 69
- 202
- 359
4 Answers
100
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.
![](../../users/profiles/163394.webp)
Narek
- 35,407
- 69
- 202
- 359
-
21It would be better to use `item->setFlags(item->flags() ^ Qt::ItemIsEditable);`. This will leave the other flags intact – priomsrb Jul 01 '12 at 11:01
-
5@priomsrb Why do you use an exclusive or (XOR) instead of `item->flags() & ~Qt::ItemIsEditable` as suggested by user2923436? The result of XOR depends on the default state of the flags. – m7913d Aug 28 '17 at 20:07
-
@m7913d I think you're right. My solution just toggles the editable state. So it wouldn't work if the item was already read only. – priomsrb Aug 30 '17 at 00:38
50
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.
![](../../users/profiles/4904417.webp)
Angie Quijano
- 3,308
- 3
- 20
- 30
![](../../users/profiles/2923436.webp)
user2923436
- 791
- 6
- 7
-
I tried this solution of yours and it didn't work: the compiler complained about the '~'. – Momergil Apr 12 '16 at 12:22
-
3
2
I came to a better suggestion, just overwrite the cellDoubleClicked signal with a new SLOT. This is, if you want none of the cells to be modified
![](../../users/profiles/3891784.webp)
Damdidam
- 21
- 1
-
No need to do that, that's why we have item flags. Just one line to fix his problem. – reggie Jul 31 '14 at 09:09
-
The difference is you would have to exclude the flag from the bitmap on all his child items?. My suggestion was to exclude writing 20 lines with &~ bitmap flag if you want the entire widget to be readonly. Actually my comment was wrong anyway, it should say disconnect the cellDoubleClicked signal – Damdidam Aug 11 '14 at 15:03
0
To apply @Narek's code to rows or columns, simply use a simple for loop and put a condition in to include the flags for rows/columns you do not want to be editable.
The following code reads a csv file into a QTableWidget:
if(!rowOfData.isEmpty()){
for (int x = 0; x < rowOfData.size(); x++)
{
rowData = rowOfData.at(x).split(",");
if(ui->table_Data->rowCount() <= x) ui->table_Data->insertRow(x);
for (int y = 0; y < rowData.size(); y++)
{
QTableWidgetItem *item = new QTableWidgetItem(rowData[y],QTableWidgetItem::Type);
if( y < 3 ) item->setFlags(item->flags() ^ Qt::ItemIsEditable); // Only disables the first three columns for editing, but allows the rest of the columns to be edited
ui->table_Data->setItem(x,y,item);
ui->table_Data->repaint();
}
}
}
![](../../users/profiles/3723803.webp)
Tim Hutchison
- 2,865
- 5
- 33
- 67