4

I have QTableView using a QSqlQueryModel (it fetches data from SQLite).

There is a QStyledItemDelegate subclass called MiniItemDelegate that I use as a delegate for the items. I set up a sizeHint() method like this:

QSize MiniItemDelegate::sizeHint(const QStyleOptionViewItem &option,
                                 const QModelIndex &index) const
{
    // just for testing...breakpoint shows this line never gets called
    return QSize(256,256);  
}

I'm not sure why this method isn't called when I run the following code:

m_pMiniItemDelegate = new MiniItemDelegate(this);
ui->PList_tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->PList_tableView->setSelectionMode(QAbstractItemView::SingleSelection);
ui->PList_tableView->setItemDelegate(m_pMiniItemDelegate);
ui->PList_tableView->setAlternatingRowColors(true);
ui->PList_tableView->setModel(ListMiniSqlModel::instance());

This also doesn't work:

ui->PList_tableView->resizeColumnsToContents();
ui->PList_tableView->resizeRowsToContents();

Nor does this:

QHeaderView* headerView = ui->PList_tableView->horizontalHeader();
headerView->setResizeMode(QHeaderView::ResizeToContents);
user63898
  • 29,839
  • 85
  • 272
  • 514
  • 1
    Does a QTreeView behave as you would expect, and it is only a problem with QTableView? ( I take it you've already found this thread: http://www.qtforum.org/article/13421/qtableview-how-to-make-rows-size-smaller.html ) – HostileFork says dont trust SE Mar 20 '12 at 08:16
  • yeah i saw it , maybe i need to set the Qt::SizeHintRole somewhere but the fact im subclassing QStyledItemDelegate and there is no data() method to inherit there – user63898 Mar 20 '12 at 09:14

3 Answers3

4

QStyledItemDelegate::sizeHint is useful only when QTableView::resizeRowsToContents, QTableView::resizeRowToContents, QTableView::resizeColumnsToContents and QTableView::resizeColumnToContents are called. or use

QHeaderView* headerView = tableView->horizontalHeader();
headerView->setResizeMode(QHeaderView::ResizeToContents);
netawater
  • 15,214
  • 4
  • 24
  • 21
1

(Credit where credit is due.) In @HostileFork's comment about a Qt Forum discussion, there's a comment thread. Within that thread, a user mikhailt offers a good solution.

The verticalHeader has a DefaultSectionSize property that can be adjusted. It doesn't matter whether the vertical header (labels on the left side of the table) is actually being displayed or not, the size will still be used.

ui->PList_tableView->verticalHeader()->setDefaultSectionSize(34);

This just solved my problem with Qt 5.6, and saved me from adjusting each data row's height separately, or causing a resize on a table.

Based on the age of the comment thread where I found it, this was already working in Qt 4, too.

Community
  • 1
  • 1
1

Have you tried: setColumnWidth or setRowHeight and horizontalHeader()->setResizeMode(QHeaderView::Fixed) ?

leonardo
  • 41
  • 1
  • 6
  • how can i ? do i need to set each row size? its to much mybe im facing this bug ? https://bugreports.qt-project.org/browse/QTBUG-20298?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel – user63898 Mar 26 '12 at 06:26