Ashot
Ashot

Reputation: 10959

How to disable whole column selection of QTableView?

void setSelectionBehavior ( QAbstractItemView::SelectionBehavior behavior )

This function accepts one of three values: for selecting items, for selecting rows and selecting cells.

Question:

I need the case when clicking a cell, it is selected, when clicking row index, row is selected but when clicking column header the whole column is not selected. As I understand this cant be done using this function.

I need the tableview to behave exactly same as when SelectionBehavior::selectItems is set.

But when user clicks on header the column should not be selected.

I am thinking about disabling column selection from QHeaderView but can't find how?

Upvotes: 3

Views: 5482

Answers (1)

re_things
re_things

Reputation: 709

From my application:

    // get header from QTableView tableView (replace with your widget name)
    QHeaderView *header = new QHeaderView(Qt::Horizontal, tableView);
#if QT_VERSION < 0x50000
// Qt 4.8.1
    header->setResizeMode(QHeaderView::ResizeToContents);
#else
// Qt 5.2.0
    header->setSectionResizeMode(QHeaderView::ResizeToContents);
#endif
    header->setHighlightSections(false); // this is what you want

setHighlightSections(bool) slot is valid for Qt 4 and Qt 5

EDIT: Excuse for carelessness! This only works if you use SelectRows or SelectItems with SingleSelection. You can find proof in the sources qheaderview.cpp and qtableview.cpp, slots voidQHeaderView::mousePressEvent(QMouseEvent *e); and voidQTableViewPrivate::selectColumn(int column, bool anchor);

For SelectItems can be used this slot:

    header->setClickable(false);

Upvotes: 3

Related Questions