How to enhace the left most button of QTableView with QAbstractTableModel to have a checkbox functionality
-
How to enhance the leftmost button of QTableView with QAbstractTableModel
I have QTableView and it is using QAbstractTableModel . When I click on LeftMost button it selects All the rows in QtableView like microsoft Excel .
Is it possible to enhance this corner most button as check box and when I click on this checkbox then it selects All the all the rows on this item and when I unlick it deselects all the items in the QtableView
-
Hi,
the most left button is the row header, where you can only set text, font, etc. in
QAbstractItemModel::headerData
. My suggestion is to add a column to you model withQt::ItemFlags QAbstractItemModel::flags(const QModelIndex &index) const { if(index.column() == 0) return (QAbstractTableModel::flags(index) | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable); return QAbstractTableModel::flags(index); }
to display a user clickable checkbox in the first column. You implementation of
data
has to return the checked stateQVariant QAbstractItemModel::data(const QModelIndex &index, int role) const { /* your return for Qt:DisplayRole */ if(role == Qt::CheckStateRole) { if(index.column() == 0) { return //your check state for index.row() } } } return QVariant(); }
You can than use
setData
of your model to select / unselect the entire row if the user checks / unchecks the checkboxbool QAbstractItemModel::setData(const QModelIndex &index, const QVariant &value, int role) { if(index.column() == 0 && role == Qt::CheckStateRole) { /* save checked state and select / deseclect row */ return true; } return false; }
-
QAbstractButton *cornerButton = tableWidget->findChild<QAbstractButton*>(); if (cornerButton) { cornerButton->disconnect(); connect(cornerButton, SIGNAL(clicked()), ..., ...); }
Source: http://www.qtcentre.org/threads/31156-Cutom-signal-on-mouse-click-at-top-left-corner-of-QTableWidget