I have QTableView using a QSqlQueryModel (it fetches data from SQLite).
There is a QStyledItemDelegate subclass called MiniItemDelegate
that I use as a delegate for the items. I set up a sizeHint() method like this:
QSize MiniItemDelegate::sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
// just for testing...breakpoint shows this line never gets called
return QSize(256,256);
}
I'm not sure why this method isn't called when I run the following code:
m_pMiniItemDelegate = new MiniItemDelegate(this);
ui->PList_tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->PList_tableView->setSelectionMode(QAbstractItemView::SingleSelection);
ui->PList_tableView->setItemDelegate(m_pMiniItemDelegate);
ui->PList_tableView->setAlternatingRowColors(true);
ui->PList_tableView->setModel(ListMiniSqlModel::instance());
This also doesn't work:
ui->PList_tableView->resizeColumnsToContents();
ui->PList_tableView->resizeRowsToContents();
Nor does this:
QHeaderView* headerView = ui->PList_tableView->horizontalHeader();
headerView->setResizeMode(QHeaderView::ResizeToContents);
QStyledItemDelegate::sizeHint is useful only when QTableView::resizeRowsToContents, QTableView::resizeRowToContents, QTableView::resizeColumnsToContents and QTableView::resizeColumnToContents are called. or use
QHeaderView* headerView = tableView->horizontalHeader();
headerView->setResizeMode(QHeaderView::ResizeToContents);