如何在 Qt 中为 QTableView 对象获得正确的行高?

发布于 2024-09-18 06:26:00 字数 222 浏览 3 评论 0原文

从这个屏幕截图中,您可以看到行内有很多空间:

alt text

我使用了这些函数来调整大小:

resizeRowsToContents();
resizeColumnsToContents();

如何更好地适应单元格/行的大小?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

夏九 2024-09-25 06:26:00

尝试这些:

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

Try these:

verticalHeader()->setDefaultSectionSize(int size)
horizontalHeader()->setDefaultSectionSize(int size)
心是晴朗的。 2024-09-25 06:26:00

试试这个:

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

Try this:

void QHeaderView::setResizeMode(QHeaderView::ResizeToContents);
幽梦紫曦~ 2024-09-25 06:26:00

当您在带有隐藏垂直标题的空表的 tableView 上调用 resizeRowsToContents 时,Qt 中似乎存在一个错误,它不会准确地调整行的大小。考虑到表通常是空的,这确实是一个麻烦的问题。幸运的是,我在 qtcentre 线程 上找到了解决方法,如下所示:

如果表/模型为空,请使用:

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

否则,这里有一个解决方法:

        // workaround for Qt empty table auto-row-sizing problem
        const int rowHeight = tableView->verticalHeader()->minimumSectionSize();
        tableView->verticalHeader()->setDefaultSectionSize(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);
-柠檬树下少年和吉他 2024-09-25 06:26:00

我正在使用 Qt 4.7,这对我在 QTableView 上有效:

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

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

tableView->resizeColumnsToContents();
tableView->resizeRowsToContents();
tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
み青杉依旧 2024-09-25 06:26:00

我有同样的问题,似乎其他许多人也有同样的问题:

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

我对一个只有几行的简单表格的快速黑客工作(假设所有行具有相同的文本高度,这可能仅适用于某些字体,可能仅适用于 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);

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);
凑诗 2024-09-25 06:26:00
QTimer::singleShot(1, ui->tableView, SLOT(resizeRowsToContents()));
QTimer::singleShot(1, ui->tableView, SLOT(resizeRowsToContents()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文