0

我正在关注YouTube 上的TextField教程,该人设置以填充RowLayout. 但是,它似乎对我不起作用。我尝试Layout.fillWidth在. 这是我的代码:CheckBoxTextField

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”,因为该选项对我不可用。提前感谢您的帮助。

编辑:我已经写了一步一步的说明来复制下面的问题。

  1. 文件 > 新建文件或项目
  2. 在新窗口中确保选择“应用程序”,然后单击“Qt Quick Application - Swipe”并按“选择”
  3. 为项目设置任何名称,然后单击“下一步”
  4. 将构建系统设置为“qmake”,然后单击“下一步”
  5. 将所需的最低 Qt 版本设置为“Qt 5.9”,将 Qt 快速控件样式设置为“Material Dark”,然后单击“下一步”
  6. 选择“Desktop Qt 5.12.0 MSVC2017 64bit”作为套件,点击“下一步”
  7. 将选项设置为没有版本控制,然后单击“完成”

  8. 从“项目”窗格中删除“Page1Form.ui.qml”和“Page2Form.ui.qml”

  9. 将“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;
    }
}
  1. 右键单击根项目文件,然后单击“添加新”
  2. 在新窗口中确保选中“Qt”,然后单击“QML File (Qt Quick 2)”并按“Choose”
  3. 将文件命名为“ToDoList”,然后单击“下一步”
  4. 添加到项目“qml.qrc Prefix:/”然后将选项设置为没有版本控制并单击“完成”
  5. 将“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
            }
        }
    }
}
  1. 运行项目
4

1 回答 1

1

宽度设置正确。问题在于TextField风格。您可以通过设置背景来检查它

TextField
{
    Layout.fillWidth: true
    background: Rectangle {
        color: "red"
    }
}

或者只是开始在有和没有的字段中输入Layout.fillWidth: true

于 2019-01-25T18:00:42.780 回答