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开发者_C百科 disable this behavior?
Edit: The headers need to remain clickable, because the onClick-function is needed.
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)));
QTableWidget::setSortingEnabled(true);
This eliminates the column selection behavior you describe and trades it in for sorting by column!
There are several several ways to do it
- 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)
- 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)
- 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.
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
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"
I know the answer to this question.
disconnect(yourTableWidget->horizontalHeader(), SIGNAL(sectionPressed(int)),yourTableWidget, SLOT(selectColumn(int)));
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 );
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.
精彩评论