如何确保 QTableView 中的列大小调整为最大
我不知道如何提出这个问题,所以请随时询问更多信息。
似乎 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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我设法找到了解决此问题的方法,您只需在调用
resizeColumnsToContents()
之前隐藏表格即可。举个例子:
I managed to find a workaround for this issue, you just need to hide the table before calling
resizeColumnsToContents()
.For an example:
我认为这是因为 QSqlTableModel 按需加载数据,并且视图仅根据可用数据计算列宽。如果您不需要用户调整列的大小,您可以尝试以下操作:
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:
我使用了 amree 所描述的相同解决方法,该方法对于列宽非常有效,但是如果任何屏幕外列具有多行单元格,则
tableView->resizeRowsToContents()
无法正常工作,这应该会导致行的高度增加。我查看了 Qt 源代码,发现某些计算取决于视口几何形状。这似乎使列和行的一切都正常工作:
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: