3

I have a QTableWidget and I want to disable the behavior that a row or column is selected when you click on a row or column header.

Does anyone know how to disable this behavior?

Edit: The headers need to remain clickable, because the onClick-function is needed.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
Oli
  • 1,221
  • 2
  • 12
  • 18

8 Answers8

3

You might want to disconnect the selectColumn slot from the sectionPressed signal of the header, something along the lines:

disconnect(horizontalHeader(), SIGNAL(sectionPressed(int)),this, SLOT(selectColumn(int)));
m7913d
  • 10,244
  • 7
  • 28
  • 56
3
QTableWidget::setSortingEnabled(true);

This eliminates the column selection behavior you describe and trades it in for sorting by column!

m7913d
  • 10,244
  • 7
  • 28
  • 56
2

tableWidget->setSelectionMode(QAbstractItemView::NoSelection); This property holds which selection mode the view operates in. SelectionMode

Or maybe you need tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows ) This property holds which selection behavior the view uses. SelectionBehavior

Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
Yanker
  • 429
  • 1
  • 6
  • 9
2

There are several several ways to do it

  1. The simple way that is not so good :) (and depends on Qt implementations as everything :): in the table view its horizontal header sectionPressed(int) is connected to table selectColumn(int), so you can simply disconnect them :( (the same sure for vertical header)
  2. You can ipmlement the table view virtual selectionCommand(const QModelIndex&, const QEvent* event) interface and return "no selection" if event is 0 (as it's a 0 then while clicking on header area)
  3. And finally the best and original solution: You can have and then set your own selectionModels both for table and its header (or headers) and re implement the selection behaviors as you want.
milyaaf
  • 136
  • 1
  • 4
0

I know the answer to this question.

disconnect(yourTableWidget->horizontalHeader(), SIGNAL(sectionPressed(int)),yourTableWidget, SLOT(selectColumn(int)));
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
developer0hye
  • 183
  • 3
  • 8
0

you can try setting false to the function setClickable

QTableWidget::horizontalHeader()->setClickable(false);

If this works, then you can do the same for [verticalHeader][2]

[2]: http://doc.qt.nokia.com/latest/qtableview.html#verticalHeader "verticalHeader"

yolo
  • 2,757
  • 6
  • 36
  • 65
  • Hi umar, thank you for the answer. But the header needs to be clickable. I forget to write this. – Oli Mar 24 '11 at 16:14
  • self.connect(self.myTableWidget.verticalHeader(), QtCore.SIGNAL('sectionClicked (int)'), self.myFunction) – Oli Mar 24 '11 at 16:36
  • try blocking http://doc.qt.nokia.com/latest/qheaderview.html#sectionPressed signal – yolo Mar 24 '11 at 16:46
  • I don't know how to block it. Found only blockSignals, but that blocks everything. I disconnected the signal too, but that didn't work neither. Some other ideas? – Oli Mar 24 '11 at 18:32
0

for example:

QTableWidgetItem * p_wgtitm = this->ui->tblwSome->item( 0, 0 );
int flags = p_wgtitm->flags();
flags &= ~(Qt::ItemIsUserCheckable | Qt::ItemIsSelectable);
p_tblitm->setFlags( (Qt::ItemFlags)flags );
Boe Boga
  • 1
  • 1
-2

If Qt for Python is acceptable, it worked for me by doing this:

def setModel(self, model): super().setModel(model) self.horizontalHeader().sectionPressed.disconnect()

Apparently the signal was getting connected in setModel. I just disconnected from everything.