QTableView QStyledItemDelegate sizeHint() not called
-
Qt 5.11/12, distro under Linux. I have a
QTableView
with aQStyledItemDelegate
, relevant :QStandardItemModel *board = new QStandardItemModel(9, 9, this); board->setData(board->index(1, 1), "hello"); // in case no data would affect behaviour QTableView *boardView = new QTableView(this); boardView->setModel(board); boardView->setItemDelegate(new BoardCellItemDelegate); BoardCellItemDelegate::BoardCellItemDelegate(QObject *parent /*= nullptr*/) : QStyledItemDelegate(parent) { } void BoardCellItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const /*override*/ { QStyledItemDelegate::paint(painter, option, index); } QSize BoardCellItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const /*override*/ { Q_UNUSED(option); Q_UNUSED(index); return QSize(80, 80); }
override
attributes are uncommented in class declaration, so we know these are indeed overrides.
Data is shown in cells, but size of cells is unaffected. Debug breakpoints/qDebug()
showBoardCellItemDelegate::paint()
being hit but notBoardCellItemDelegate::sizeHint()
.Qt docs, https://forum.qt.io/topic/91658/how-to-set-cell-size-of-qtablewidget/3 ( @VRonin) and https://stackoverflow.com/a/63917132/489865 ( @eyllanesc) [is it relevant they are talking about
QTableWidget
not justQTableView
?] say this should work. Am I missing something? -
Hi Jon,
the sizeHint override is called, whenresizeRowToContents(int row)
orresizeColumnToContents(int column)
are invoked on the table view object. If you want to resize during programmatic content construction or user interaction, you have to include these methods in your code or in a respective slot.
Cheers
Axel -
@Axel-Spoerl
Oh, thanks, that is not what I want to do at all!What I want to do is have the delegate produce a fixed size (happens to be square) for all cells, and for the table view to respect that (so that it sets its size fixed to 9x9 x that fixed cell size). And I would like the table view to size itself to that total size, so that the whole table is shown, so scrolling.
What I do not expect is to have to do calculations/multiplications at the table view level to compute that! :) I wanted it to come from the cell size....
Any suggestions, please?
-
BoardCellItemDelegate::sizeHint()
returns the same size for every cell, whichQTableView
cannot (and should not) anticipate. It is designed to deal with cell items of different contents, leading to different size hints. The methods I have mentioned earlier, obtain the size hint of every single item and determine what would be the required width of a column or height of a row. They would then deal with the space available on the screen incl. horizontal and vertical scrolling. If the fixed 80x80 size is a given for yourQTableView
, you can just callsetColumnWidth(80)
andsetRowHeight(80)
when you create the table view object. You don't have to subclassQStyledItemDelegate
in that case, unless needed for something not shown in the code example. If there is no alternative to obtaining the size fromBoardCellItemDelegate::sizeHint()
, then I would subclassQTableView
and overridesetModel()
. In the re-implementation you can then obtain the size hint from your delegate and set the size of the table view accordingly. -
@Axel-Spoerl said in QTableView QStyledItemDelegate sizeHint() not called:
you can just call
setColumnWidth(80)
andsetRowHeight(80)
when you create the table view object.There aren't any such methods taking a single parameter! [Give me a doc reference if I am wrong!] They each take a row/column number. So can't be set at view creation time. And I didn't like the idea of having to specify the same width/height against each column/row when the whole thing is supposed to be fixed anyway --- offends my sensibilities!
I could swear I once saw a "set table view to have all rows of a fixed height for an optimization" method, but damned if I can find it now! [Unless it was for some other widget or
QGridLayout
, or I'm going senile.] Wouldn't help for column heights anyway.I've ended up having to calculate the overall size of the table view anyway to call
setFixedSize()
on it. I have gone forQHeaderView::setDefaultSectionSize()
, even though I don't show headers. The whole thing has become moot as it all fits now, and i have other code to get on with!Thank you for your time and interest, I'm marking this as solved.
-
You are right, you have to specify the column/row as an additional parameter, sorry about that.