QML中Textarea中文本的行高

问题描述

我是 Qt/Qml 的新手。 实际上,我陷入了一种情况,即我有 line-Height 条件,并且无法在 QML 的 TextArea 代码中使用它。 我的 Textarea 位于 Flickable 内,后者位于 Rectangle 内。 我希望我的 Rectangle 是 288 和 78 的宽度和高度,这应该是固定的。并且 line-Height 为 22px。 我曾尝试使用下面的代码,但它为我提供了不需要的矩形的动态大小。 我在处理这个问题时遇到了麻烦。所以请尽量帮助我。 代码中提到了填充。 所以请帮助我。提前致谢。

TextWidget.qml

 import QtQuick 2.12
 import QtQuick.Controls 2.12
 import QtQml 2.12



Rectangle {
    id: parentrect

    property alias textContent:textarea.text
      property alias fieldlabel:childrecttext.text
        property  int paintedHeight:22

    width:288
    height:paintedHeight*3
       

    border.color: "green" 
    color: "gray"
   
    Flickable {
        id:flickitem
        contentWidth: textarea.width
        contentHeight:textarea.height
        anchors.fill: parent
        TextArea.flickable:  TextArea {
            id:textarea
            focus: true
           // height: 22*textarea.lineCount
            hoverEnabled: true
            //anchors.fill:parent

            wrapMode: TextArea.WrapAtWordBoundaryOrAnywhere
            font.pixelSize: 16
            font.family: "Times New Roman"
            leftPadding: 12
            rightPadding: 12
            topPadding:6
            bottomPadding: 6


            onTextChanged: {
            parentrect.height=(lineCount*parentrect.paintedHeight)+parentrect.paintedHeight

            }
            onReadOnlyChanged: { btextreadonly=textarea.readOnly }
            selectByMouse: true
            textformat: TextArea.AutoText

           Component.onCompleted: { console.log(textarea.paintedHeight)
                                    console.log(parentrect.width)
                                    console.log(parentrect.height)
                                    console.log(childrecttext.paintedHeight)
                                    console.log(textarea.lineCount)
           }
     ScrollBar.vertical: ScrollBar{
              active: hovered || pressed
              policy: ScrollBar.AlwaysOn

             }
    }



    Rectangle {
        id:labelrect
        height: childrecttext.contentHeight
        width: childrecttext.contentWidth
        color: "white"
        anchors{
            bottom: parentrect.top
            left: parentrect.left
            right: parentrect.right
            bottomMargin: 8
        }
        Text {
        id: childrecttext
        anchors.fill: parent
        lineHeight: 22
        lineHeightMode: Text.FixedHeight
        font{
        pixelSize: 14
        family:"Times New Roman"
        }
        wrapMode: Text.WordWrap
        color: "black"
    }

    }}

Main.qml

import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Window 2.12
Window{
width: 800
height: 400
visible: true
title: qsTr("TextBox")

Rectangle{
id:rect1
width : parent.width/2
height: parent.height
color: "White"

Row{
    id:lightThemerow1
     x:10
     anchors.top:  parent.top
     anchors.topMargin: 200
     spacing: 20
TextWidget{
      id:textarea1

      fieldlabel:"Lorem ipsum dolor sit amet,conse ctetur adipiscing elit,sed do eiusmod tempor."

      textContent:"Lorem ipsum dolor sit amet,sed do eiusmod tempor incididunt ut labor."

}
}
}

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)