I'm using QTableView
with a subclass of QAbstractTableModel
as its model. By implementing data()
and headerdata()
in the subclassed model, it is feasible to control many properties of the table like data, header values, font, and so on.
In my case, I want the model to set the width of each table column. How can this be done?
There are two ways:
In your model's data method you can return the role SizeHintRole.
A better way would be to subclass QItemDelegate
and override the method.
See here (qitemdelegate.html#sizeHint)
Example -
QSize ItemDelegate::sizeHint( const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
QSize sz;
if(index.column()==2)
{
return QSize(128, option.rect().height());
}
return QSize();
}
Here I am setting the width of column 2 to 128 pixels and I am filling in the height from the item rectangle held in QStyleOptionViewItem
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With