Hi,
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/1885...ustom-delegate but i can't find the problem in my code.
The delegate it is:
class LineasDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
LineasDelegate
(QObject *parent
= 0) : QStyledItemDelegate
(parent
) {};
{
std::cout << "sizeHint method delegate" << std::endl;
}
class LineasDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
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;
}
To copy to clipboard, switch view to plain text mode
I create it and attach to the TableView with this code:
ui->tableView->setAlternatingRowColors(true);
ui->tableView->verticalHeader()->setDefaultSectionSize(ui->tableView->fontMetrics().lineSpacing() + 8);
lineasDelegate = new LineasDelegate();
ui->tableView->setItemDelegate(lineasDelegate);
ui->tableView->setAlternatingRowColors(true);
ui->tableView->verticalHeader()->setDefaultSectionSize(ui->tableView->fontMetrics().lineSpacing() + 8);
lineasDelegate = new LineasDelegate();
ui->tableView->setItemDelegate(lineasDelegate);
To copy to clipboard, switch view to plain text mode
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;
}
if (role == Qt::SizeHintRole) {
std::cout << "data method sizeHintRole" << std::endl;
}
To copy to clipboard, switch view to plain text mode
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).
Thanks.
Bookmarks