How to get right row height in Qt for QTableView object?
Asked Answered
D

6

9

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?

Delgadillo answered 10/9, 2010 at 18:16 Comment(0)
V
11

Try these:

verticalHeader()->setDefaultSectionSize(int size)
horizontalHeader()->setDefaultSectionSize(int size)
Vulgarize answered 11/9, 2010 at 3:44 Comment(0)
P
5

Try this:

void QHeaderView::setResizeMode(QHeaderView::ResizeToContents);
Predecease answered 11/9, 2010 at 9:56 Comment(2)
That works well for columns, but doesn't seem to work with rows.Alyse
For me this does appear to work for rows... hmmm (PyQt5)Craner
N
2

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);
Nutritionist answered 13/9, 2019 at 1:39 Comment(4)
Hmmm... this seems to set every row to the same height.Craner
@mikerodent for an empty table, yes it does. in my case I wanted all the rows to be the same height though, so it was fine.Nutritionist
My tables aren't empty and the rows are different heights. In my system this imposes the same height for each row. There is therefore no point in calling resizeRowsToContents before setDefaultSectionSize.Craner
@mikerodent yes, there is an effect. resizeRowsToContents affects the existing rows with contents. if you allow word wrap for example, some could end up double height. setDefaultSectionSize affects the future rows which have not yet been inserted.Nutritionist
O
1

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

tableView->resizeColumnsToContents();
tableView->resizeRowsToContents();
tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
Oconnell answered 12/12, 2011 at 5:48 Comment(0)
S
0

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);
Silverweed answered 14/1, 2011 at 20:33 Comment(0)
S
-1
QTimer::singleShot(1, ui->tableView, SLOT(resizeRowsToContents()));
Sawyere answered 25/11, 2017 at 7:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.