开发者

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

开发者 https://www.devze.com 2023-01-15 10:16 出处:网络
From this screenshot you can see a lot of space inside开发者_如何学编程 the rows: I\'ve used these functions to get resizing:

From this screenshot you can see a lot of space inside开发者_如何学编程 the rows:

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

I've used these functions to get resizing:

resizeRowsToContents();
resizeColumnsToContents();

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


Try these:

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


Try this:

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


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);


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);


QTimer::singleShot(1, ui->tableView, SLOT(resizeRowsToContents()));
0

精彩评论

暂无评论...
验证码 换一张
取 消