Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 525
DragonionS Avatar asked Sep 10 '10 18:09

DragonionS


2 Answers

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);
like image 105
Patrick Parker Avatar answered Sep 28 '22 05:09

Patrick Parker


Try this:

void QHeaderView::setResizeMode(QHeaderView::ResizeToContents);
like image 27
tibur Avatar answered Sep 28 '22 05:09

tibur