qt - 如何在应用程序启动时将 QML TableView 列的大小调整为其内容?
问题描述
我在 Ubuntu 18.04 上使用 Qt 5.9.4。
当我的应用程序启动时,我想TableView
根据其内容自动调整 ' 列大小。模型在开始时有一些数据。
我知道这个resizeColumnToContents
函数,但我不知道在哪里调用它。
onDataChange
不起作用TableView
:QML 引擎说这个信号不存在。但是智能型允许我在代码中输入它。
如何做到这一点?
解决方案
编辑 18/09/18
如果你使用 StackView 或者你可以预加载你的 TableView
// main.qml
Loader {
id: tableViewLoader
active: true
sourceComponent: TableView { id: tableView }
}
StackView {
id: stackView
initialItem: listViewLoader
function onContentReceived()
{
stackView.push(tableViewLoader);
tableViewLoader.item.resizeColumnsToContents()
}
function onContentClosed()
{
swipeView.pop()
}
}
编辑 18 年 9 月 17 日
你是对的丹尼尔。
在 TableView.qml 中指定
Depending on how the model is populated, the model may not be ready when TableView Component.onCompleted is called. In that case you may need to delay the call to positionViewAtRow by using a \l {QtQml::Timer}{Timer}
对我来说,这是有效的
Component.onCompleted: resizeColumnsToContentsTimer.start()
Timer {
id: resizeColumnsToContentsTimer
interval: 50
running: false
repeat: false
onTriggered: parent.resizeColumnsToContents()
}
你也可以看到这个关于它的讨论
http://lists.qt-project.org/pipermail/interest/2016-June/023018.html
也许,您可以在设置模型时调用的 onModelChanged 中调用它(您的模型必须先填充)。
onModelChanged: tableView.resizeColumnToContents()
否则,您可以在数据准备就绪时使用信号/插槽。
但要小心这个函数:如果你有委托,你必须指定implicitWidth,否则这将不起作用。
headerDelegate: Rectangle {
id: headerDelegate
height: 36
implicitWidth: textItem.implicitWidth + textItem.padding * 2
color: Style.lightColor
Text {
id: textItem
anchors.fill: parent
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignLeft
padding: 10
text: styleData.value
elide: Text.ElideRight
color: Style.darkColor
font.pixelSize: Style.bigFontPixelSize
}
}
推荐阅读
- javascript - 显示不带括号的对象数组,但元素(包括逗号)必须保留
- c# - WPF 中的 DatePicker 显示 01.01.0001 而不是今天
- reactjs - Typescript:React Context 高阶组件类型错误
- android - FloatingActionButton 高于 BottomAppBar
- java - Intellij - 多个位置的同一个 jar
- gcc - 基于 Solaris Sparc 构建的 gcc 4.9 调试版本
- scala - 当我们在创建 Spark 会话时通过设置元组“spark.some.config.option”,“config-value”时,我们的意思是什么?
- excel - 删除空行 (ROWS)
- excel - VBA 转置循环源问题
- pandas - 使用 loc 选择列会导致行值中出现一些 NaN