I also have a QTableView and I am using a custom delegate that derives from QStyledItemDelegate.
I want to change the height of the QTableView row depending on the item that get's added to the model.
I've tried reimplementing the sizeHint() function of QStyledItemDelegate class but it's not getting invoked at all.
I would like to avoid to invoke resizeRowsToContents() each time something gets added to the model as the model does contain a lot of datas.
CommunicationView
::CommunicationView( QWidget* parent
) :{
// Model
m_model = CommunicationModel::instance(this);
setModel(m_model);
// Delegate
m_delegate = new CommunicationDelegate(this);
setItemDelegate(m_delegate);
...
CommunicationView::CommunicationView( QWidget* parent ) :
QTableView(parent)
{
// Model
m_model = CommunicationModel::instance(this);
setModel(m_model);
// Delegate
m_delegate = new CommunicationDelegate(this);
setItemDelegate(m_delegate);
...
To copy to clipboard, switch view to plain text mode
CommunicationDelegate::sizeHint( const QStyleOptionViewItem& option, const QModelIndex& i ) const
{
CommunicationModel* m_model = (CommunicationModel*)i.model();
CommunicationData::CommunicationDataType type = m_model->type(i.row());
if (type == CommunicationData::VAR_INFO_MESSAGE) {
int const w = QStyledItemDelegate::sizeHint( option, i ).width();
} else {
return QStyledItemDelegate::sizeHint( option, i );
}
}
QSize
CommunicationDelegate::sizeHint( const QStyleOptionViewItem& option, const QModelIndex& i ) const
{
CommunicationModel* m_model = (CommunicationModel*)i.model();
CommunicationData::CommunicationDataType type = m_model->type(i.row());
if (type == CommunicationData::VAR_INFO_MESSAGE) {
int const w = QStyledItemDelegate::sizeHint( option, i ).width();
return QSize( w, 34 );
} else {
return QStyledItemDelegate::sizeHint( option, i );
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks