View Full Version : delegate sizeHint not getting called

15th March 2011, 19:41
i have created a pretty simple delegate to fix the size of the last column that get too big when i call setStretchLastSection(true) in the header (curious that if i don't call this method the column get the correct size and have a good margin until the end of the table), i have read the related post http://www.qtcentre.org/threads/18855-QTableView-sizeHint()-not-getting-invoked-for-custom-delegate but i can't find the problem in my code.

The delegate it is:

class LineasDelegate : public QStyledItemDelegate

LineasDelegate(QObject *parent = 0) : QStyledItemDelegate(parent) {};
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;

QSize LineasDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
std::cout << "sizeHint method delegate" << std::endl;

I create it and attach to the TableView with this code:

ui->tableView->verticalHeader()->setDefaultSectionSize(ui->tableView->fontMetrics().lineSpacing() + 8);

lineasDelegate = new LineasDelegate();

And after attach my custom model i call ui->tableView->resizeColumnsToContents(), still the method get not called ever, i have tried to add too at the data method of my custom model:

if (role == Qt::SizeHintRole) {
std::cout << "data method sizeHintRole" << std::endl;

That does not get called either. The only way i have found to set the size of the column it is using ui->tableView->horizontalHeader()->resizeSection(section, size) but i would prefer to use the delegate.

The delegate it is the correct and works because i have in the delegate implemented createEditor too and works ok (I have not posted the method because i don't think should be related).


16th March 2011, 10:59
If you have the last column set to stretch, the sizeHint is totally ignored because the size of the element is calculated upon the header size and not the element size. Seems you are approaching the problem from a wrong end. What exactly are you trying to achieve?

16th March 2011, 11:03
I would like to have the last column stretched to get a table without gaps in the header and without horizontal scrollbar and have the columns resized correctly with his contents, I think that was possible in Qt 4.5 with the steps i have followed above, but i think they have "fixed" this in Qt 4.6 and above, what would be the correct way to achieve it?

16th March 2011, 11:49
So why don't you just use QHeaderView::setResizeMode() to set the resize mode of all but the last column to ResizeToContents and the last column to Stretch?

16th March 2011, 11:53
I will try it and post here the results but my original problem it is that the header size of the last column it is calculated badly when i use SetStretchLastSection(True), all other columns have a default ok size but the last one get a with about three or four times the needed for his header text and force a horizontal scrollbar.

16th March 2011, 12:06
If you stretch the last column then it takes all the remaining space by definition. It doesn't take into consideration the width of the data in the item.

16th March 2011, 12:08
It goes out of the remaining space (nearly the half of the last column goes out of the viewport of the tableview) and force a horizontal scrollbar, don't seem a good behavior for me.

16th March 2011, 12:25
It goes out of the remaining space (nearly the half of the last column goes out of the viewport of the tableview) and force a horizontal scrollbar, don't seem a good behavior for me.

It shouldn't be happening like that. You must have done something to the other columns.

16th March 2011, 12:39
Here you have photos taken in every case

without use setStretchLastSection(true):

using setStretchLastSection(true):

using setStretchLastSection(true) and later resizeSection to fix the size:

I am not doing anything special to the columns, maybe the problem it is that i have set a maximumWith of 320 to the mainwindow.

16th March 2011, 16:09
I don't find anything strange but anyway i have found a way to fix it using resizeSection after the stretch so i think it is solved (more or less).

Thanks wysota.