When I push horizontal header section item in QTableWidget
the upper cell in this column becomes ready to be edited.
How can I prevent it?
Asked
Active
Viewed 139 times
-2

Ufx
- 2,595
- 12
- 44
- 83
-
By default, when a column's header is clicked, the whole column is selected. Maybe it is something that has to do with your code/properties you have set on the `QTableWidget`. Please provide all the properties you have changed in your `QTableWidget` and/or the relevant part of code ( preferably in an [MCVE](https://stackoverflow.com/help/mcve) ). – Mike Oct 01 '16 at 12:50
-
1If you want us to help you, you must provide a [mcve], if you do not do it for me this question should be closed as it falls on the off-topic: **Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers.** – eyllanesc Dec 03 '17 at 20:16
-
What kind of behaviour are you looking for? Should a column be selected when you click on a header? Should editing automatically start when you select a cell? Please include all relevant information in your question (as text), instead of adding a screenshot. – m7913d Dec 04 '17 at 09:25
2 Answers
3
The problem is related to the interference of
- selecting a column when clicking on the column header and
- immediately start editing when an item is selected (
editTriggers > CurrentChanged
)
So, basically, you should disable one or both behaviours.
- To disable selecting a column when its header is clicked, you can have a look at the question Disable selecting row or column by clicking the header in QTableWidget containing many alternative solutions.
- Or do not immediately start editing by disabling
CurrentChanged
.

Toby Speight
- 27,591
- 48
- 66
- 103

m7913d
- 10,244
- 7
- 28
- 56
-
1`disconnect(horizontalHeader(), SIGNAL(sectionPressed(int)),this, SLOT(selectColumn(int)));` works for me. – Ufx Dec 07 '17 at 16:02
0
Looking in the QTableWidget documentation i found a way to fix your problem. You have to edit the every single vertical header item and modify their flags to be not editable.
const int size = ui->tableWidget->columnCount();
for (int i=0; i<size; i++) {
QTableWidgetItem * item = ui->tableWidget->verticalHeaderItem(i);
item->setFlags(...every single flag you want);
}
You have a list of the available flags in this link.

mohabouje
- 3,867
- 2
- 14
- 28
-
1Its working fine for me: QTableWidgetItem *item = new QTableWidgetItem(); item->setFlags(item->flags() ^ Qt::ItemIsEditable); – mohabouje Oct 02 '16 at 20:53
-