qt - 无法在 QML 中设置文本字段以填充宽度
问题描述
我正在关注YouTube 上的本TextField
教程,该人设置以填充RowLayout
. 但是,它似乎对我不起作用。我尝试Layout.fillWidth
在. 这是我的代码:CheckBox
TextField
main.qml:
import QtQuick 2.9
import QtQuick.Controls 2.2
ApplicationWindow
{
visible: true;
width: 640;
height: 480;
title: qsTr("Tabs");
ToDoList
{
anchors.centerIn: parent;
}
}
ToDoList.qml:
import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
Frame
{
ListView
{
// Using implicit width and height allows the frame to automatically scale to the size of the list view
implicitWidth: 250
implicitHeight: 250
clip: true
model: 100
delegate: RowLayout {
width: parent.width
CheckBox {}
TextField
{
Layout.fillWidth: true
}
}
}
}
这是我的截图
我做错了什么?
我不知道这是否与它有关,但我制作了“Qt Quick Application - Swipe”而不是“Qt Quick Controls 2 Application”,因为该选项对我不可用。提前感谢您的帮助。
编辑:我已经写了一步一步的说明来复制下面的问题。
- 文件 > 新建文件或项目
- 在新窗口中确保选择“应用程序”,然后单击“Qt Quick Application - Swipe”并按“选择”
- 为项目设置任何名称,然后单击“下一步”
- 将构建系统设置为“qmake”,然后单击“下一步”
- 将所需的最低 Qt 版本设置为“Qt 5.9”,将 Qt 快速控件样式设置为“Material Dark”,然后单击“下一步”
- 选择“Desktop Qt 5.12.0 MSVC2017 64bit”作为套件,点击“下一步”
将选项设置为没有版本控制,然后单击“完成”
从“项目”窗格中删除“Page1Form.ui.qml”和“Page2Form.ui.qml”
- 将“main.qml”的内容替换为:
import QtQuick 2.9
import QtQuick.Controls 2.2
ApplicationWindow
{
visible: true;
width: 640;
height: 480;
title: qsTr("Tabs");
ToDoList
{
anchors.centerIn: parent;
}
}
- 右键单击根项目文件,然后单击“添加新”
- 在新窗口中确保选中“Qt”,然后单击“QML File (Qt Quick 2)”并按“Choose”
- 将文件命名为“ToDoList”,然后单击“下一步”
- 添加到项目“qml.qrc Prefix:/”然后将选项设置为没有版本控制并单击“完成”
- 将“ToDoList.qml”的内容替换为:
import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
Frame
{
ListView
{
// Using implicit width and height allows the frame to automatically scale to the size of the list view
implicitWidth: 250
implicitHeight: 250
clip: true
model: 100
delegate: RowLayout {
width: parent.width
CheckBox {}
TextField
{
Layout.fillWidth: true
}
}
}
}
- 运行项目
解决方案
宽度设置正确。问题在于TextField
风格。您可以通过设置背景来检查它
TextField
{
Layout.fillWidth: true
background: Rectangle {
color: "red"
}
}
或者只是开始在有和没有的字段中输入Layout.fillWidth: true
推荐阅读
- reactjs - 当平面列表中的某些项目在第二个屏幕中被删除时,无法在第一个屏幕上更新平面列表
- php - 如何将唯一的电子邮件插入 mysql 表
- python - subprocess.run 生成的进程仅运行单线程
- react-native - expo-camera 返回无效的 base-64 编码图像
- go - 使用会话 ID 存储用户 ID
- r - dput a long list - 缩短列表但保留结构
- python - Django外键未更新
- mongodb - 忽略 MongoDB 文本索引中的语言
- javascript - jest.config.js 失败 - 意外令牌“。” 在 module.exports 中
- python - 字典可能包含字符串或数组