qt - qml ColumnLayout内的中心元素
问题描述
如何在qml ColumnLayout中居中元素?我尝试失败:
Layout.alignment: Qt.AlignCenter
代码:
import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Layouts 1.11
import QtQuick.Controls 1.4
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
ColumnLayout{
anchors.centerIn: parent
width: parent.width
Layout.preferredHeight: parent.height
visible: true
Text{
id: myText
text: "My Text"
Layout.preferredWidth: parent.width
Layout.preferredHeight: 25
Layout.alignment: Qt.AlignCenter
}
}
}
但是 myText 仍然不是水平居中的。任何想法?
解决方案
如果我们使用快速设计器进行审查,我们会获得以下信息:
正如我们所见,该项目居中。问题是 Layout 不处理文本项内文本的位置,为此您必须使用horizontalAlignment
:
import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Layouts 1.11
import QtQuick.Controls 1.4
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
ColumnLayout{
anchors.centerIn: parent
width: parent.width
Layout.preferredHeight: parent.height
visible: true
Text{
id: myText
text: "My Text"
Layout.preferredWidth: parent.width
Layout.preferredHeight: 25
horizontalAlignment: Text.AlignHCenter
}
}
}
推荐阅读
- java - 在Java中比较第一个文件的一个字符串和第二个文件的所有字符串
- c# - 如何获取数组中具有固定数量标志的所有组合
- numpy - np.array([args.width, args.width]) 和 np.zeros(5) 的语法
- python-3.x - 结果使用 neupy 得到 NaN
- python - 使用 Python 在 Selenium 中模拟鼠标移动
- vba - 在 Excel VBA 代码中键入未匹配错误
- angular - 如何在 Angular 中为动态组件注入提供者?
- ios - 带有委托参考的 Swift 默认参数值
- javascript - Html-css 响应式滑动反馈表
- javascript - HTML 页面上带有文本的交互式形状