【问题标题】:How to resize QML TextField whenever parent form is resized每当调整父表单的大小时如何调整 QML TextField 的大小
【发布时间】:2021-11-07 23:54:05
【问题描述】:

我有一个表格和一个TextField。无论表单大小如何,TextField 的大小都是固定的。我希望在调整表单大小时调整它的大小(特别是在表单最大化时变大)。如何实现这一点(两者之间的固定边距是可以的)。

import QtQuick 2.0
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.3
import org.kde.plasma.plasmoid 2.0
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.components 3.0 as PlasmaComponents

Item
{
  property alias cfg_mainText:       mainText.text

  anchors.centerIn:     parent

  ColumnLayout
   {

   RowLayout
    {
      Label { text: "Main" }
      TextField
      {
        id:               mainText
        Layout.fillWidth: true
        placeholderText:  "important text here"
        text:             ""
      }
    }

   }

}

【问题讨论】:

    标签: forms qml controls anchor


    【解决方案1】:

    你有一个 Item 包含你的 TextField 首先你应该为它和锚设置 id,然后告诉你的 TextField 它的宽度应该和 Item 一样。

    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtQuick.Layouts 1.1
    import QtQuick.Controls 2.12
    
    Window {
        id: window
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
    
    
        Item
        {
            id:txtField
            property alias cfg_mainText:       mainText.text
            anchors.fill: parent
            anchors.topMargin: 240
            anchors.bottomMargin: 200
            anchors.leftMargin: 320
            anchors.rightMargin: 91
    
    
            ColumnLayout
            {
    
                RowLayout
                {
                    Label { text: "Main" }
                    TextField
                    {
                        id:               mainText
                        width: txtField.width
    
                        Layout.fillWidth: true
                        placeholderText:  "important text here"
                        text:             ""
                    }
                }
    
            }
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      • 1970-01-01
      相关资源
      最近更新 更多