Question

From this screenshot you can see a lot of space inside the rows:

alt text

I've used these functions to get resizing:

resizeRowsToContents();
resizeColumnsToContents();

How can I get a better fit for cells/rows sizes?

Was it helpful?

Solution

Try these:

verticalHeader()->setDefaultSectionSize(int size)
horizontalHeader()->setDefaultSectionSize(int size)

OTHER TIPS

Try this:

void QHeaderView::setResizeMode(QHeaderView::ResizeToContents);

I'm using Qt 4.7 and this worked for me on QTableView:

tableView->resizeColumnsToContents();
tableView->resizeRowsToContents();
tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);

I have the same problem, so do many others it seems:

http://www.qtforum.org/article/13421/qtableview-how-to-make-rows-size-smaller.html

My quick hack job for a simple table with a few rows only (assume all rows have same text height and this probably only works for some fonts maybe only on Windo):

int rowHeight = ui.tableView_Available->rowHeight(0) *1/2;
for (unsigned int i = 0; i < model->rowCount(); i++)
  ui.tableView_Available->verticalHeader()->resizeSection(i, rowHeight);

There seems to be a bug in Qt when you call resizeRowsToContents on the tableView of an empty table with a hidden verticalHeader, it does not accurately resize the rows. And considering that tables often start empty, this is a troublesome problem indeed. Fortunately I found a workaround on a qtcentre thread, as follows:

If table/model is not empty, use:

        tableView->resizeRowsToContents();
        const int rowHeight = tableView->verticalHeader()->sectionSize(0);
        tableView->verticalHeader()->setDefaultSectionSize(rowHeight);

Otherwise, here is a workaround:

        // workaround for Qt empty table auto-row-sizing problem
        const int rowHeight = tableView->verticalHeader()->minimumSectionSize();
        tableView->verticalHeader()->setDefaultSectionSize(rowHeight);
QTimer::singleShot(1, ui->tableView, SLOT(resizeRowsToContents()));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top