如何确保 QTableView 中的列大小调整为最大

发布于 2024-09-13 04:53:39 字数 1315 浏览 1 评论 0原文

我不知道如何提出这个问题,所以请随时询问更多信息。

似乎 tableView->resizeColumnsToContents() 只会根据当前视图中的数据调整所有列的大小。这意味着如果我下面有更多数据(就单词数而言更长),这些单词将被包裹(如果 wordWrap 属性设置为 true)。

奇怪的是,如果我向下滚动到底部并刷新数据,tableView 将正确调整这些列的大小。似乎 tableView 不知道下面有更长的文本。

所以,我的问题是,如何确保根据所有数据将这些列的大小调整为最大值?

我的代码

QSqlTableModel *model = new QSqlTableModel;
model->setTable("item");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();    
tableResult->setModel(model);    
tableResult->setEditTriggers(QAbstractItemView::NoEditTriggers);    
tableResult->setSelectionBehavior(QAbstractItemView::SelectRows);
tableResult->setSelectionMode(QAbstractItemView::SingleSelection);    
tableResult->resizeColumnsToContents();
tableResult->resizeRowsToContents();

更新1

我尝试过tableResult->scrollToBottom(),它只会根据底部的项目调整大小。因此,如果中间有较长的单词,这些单词将被换行。

更新2

如果有人想了解我在说什么,只需下载这个例子。您将看到单击按钮将生成未正确调整大小的数据。

更新 3

可能是一个错误:https://bugreports.qt。 io/浏览/QTBUG-9352

I'm not sure how to ask this, so, feel free to ask for more information.

It seems that tableView->resizeColumnsToContents() will only resize all the columns based on data in current view. Which means that if I have more data below (which is longer in terms of counts of words), those words will be wrapped down (if the wordWrap property is set to true).

The weird thing is, if I scroll down to the bottom and refresh the data, tableView will resize those columns correctly. It seems as if tableView didn't know there are longer text below.

So, my question is, how can I make sure those columns are resized to the max based on all of the data?

My codes

QSqlTableModel *model = new QSqlTableModel;
model->setTable("item");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();    
tableResult->setModel(model);    
tableResult->setEditTriggers(QAbstractItemView::NoEditTriggers);    
tableResult->setSelectionBehavior(QAbstractItemView::SelectRows);
tableResult->setSelectionMode(QAbstractItemView::SingleSelection);    
tableResult->resizeColumnsToContents();
tableResult->resizeRowsToContents();

Update 1

I've tried tableResult->scrollToBottom() and it will only resize based on items at the bottom. So, if there are longer words in the middle, those words will get wrapped.

Update 2

If anyone would like to understand what I'm talking about, just download this example. You'll see that clicking the PushButton will generate a data that's not resized correctly.

Update 3

Possibly a bug: https://bugreports.qt.io/browse/QTBUG-9352

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

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

发布评论

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

评论(3

风轻花落早 2024-09-20 04:53:39

我设法找到了解决此问题的方法,您只需在调用 resizeColumnsToContents() 之前隐藏表格即可。

举个例子:

tableResult->setVisible(false);
tableResult->resizeColumnsToContents();
tableResult->setVisible(true);

I managed to find a workaround for this issue, you just need to hide the table before calling resizeColumnsToContents().

For an example:

tableResult->setVisible(false);
tableResult->resizeColumnsToContents();
tableResult->setVisible(true);
梦过后 2024-09-20 04:53:39

我认为这是因为 QSqlTableModel 按需加载数据,并且视图仅根据可用数据计算列宽。如果您不需要用户调整列的大小,您可以尝试以下操作:

tableResult->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);

I think that is because QSqlTableModel loads data on demand and the view calculates the column widths based only on data that is available. If you don't need your columns to be user-resizable, you can try this:

tableResult->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
萌能量女王 2024-09-20 04:53:39

我使用了 amree 所描述的相同解决方法,该方法对于列宽非常有效,但是如果任何屏幕外列具有多行单元格,则 tableView->resizeRowsToContents() 无法正常工作,这应该会导致行的高度增加。

我查看了 Qt 源代码,发现某些计算取决于视口几何形状。这似乎使列和行的一切都正常工作:

#include <limits>

tableView->setVisible(false);
QRect vporig = tableView->viewport()->geometry();
QRect vpnew = vporig;
vpnew.setWidth(std::numeric_limits<int>::max());
tableView->viewport()->setGeometry(vpnew);
tableView->resizeColumnsToContents();
tableView->resizeRowsToContents();
tableView->viewport()->setGeometry(vporig);
tableView.setVisible(true);

I used the same workaround as described by amree, which worked great for the column widths, but tableView->resizeRowsToContents() wasn't working correctly if any offscreen columns had multiline cells that should have caused a row's height to increase.

I looked into the Qt source and it appears that some of the calculations depend on the viewport geometry. This seems to make everything work correctly for both columns and rows:

#include <limits>

tableView->setVisible(false);
QRect vporig = tableView->viewport()->geometry();
QRect vpnew = vporig;
vpnew.setWidth(std::numeric_limits<int>::max());
tableView->viewport()->setGeometry(vpnew);
tableView->resizeColumnsToContents();
tableView->resizeRowsToContents();
tableView->viewport()->setGeometry(vporig);
tableView.setVisible(true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文