Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML TextArea : Wrapping placeholder text
Forum Updated to NodeBB v4.3 + New Features

QML TextArea : Wrapping placeholder text

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 3 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tummon
    wrote on last edited by
    #1

    Hi all,

    Just wondering if there is a recommended way of getting the placeholder text of a TextArea to word wrap?
    I assume it will have to be some combination of TextMetrics and manually calculating where to add new line characters?
    (My google and forum/doc searches are not proving very fruitful)

    Thanks!

    1 Reply Last reply
    0
    • MarkkyboyM Offline
      MarkkyboyM Offline
      Markkyboy
      wrote on last edited by
      #2

      Try the following;

      import QtQuick 2.5    
      import QtQuick.Window 2.2    
      import QtQuick.Controls 2.15
              
      Window {    
          width: 300    
          height: 300    
          visible: true
             
          Text {    
              id: textEdit    
              width: 200    
              height: 50
         
              property string placeholderText: "Enter text here..."
              
              TextArea {
                  text: textEdit.placeholderText
                  color: "#aaa"
                  visible: !textEdit.text
                  wrapMode: Text.WordWrap    
                  width: parent.width // set width to be used in TextEdit    
              }    
          }    
      }
      

      Don't just sit there standing around, pick up a shovel and sweep up!

      I live by the sea, not in it.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Tummon
        wrote on last edited by
        #3

        Ah nice, will have to reimplement some functionality around clearing the placeholder text on input etc. but I think that's better than messing about with TextMetrics!
        Thanks!

        1 Reply Last reply
        0
        • T Offline
          T Offline
          Tummon
          wrote on last edited by
          #4

          A little tweaking and this works nicely:

          TextArea {
              id: textArea
              property alias customPlaceholderText: innerPlaceholderText.text
              property alias customPlaceholderTextColor: innerPlaceholderText.color
              anchors.fill: parent
              customPlaceholderText: qsTr("Place Holder Text Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong")
              //customPlaceholderTextColor: "#aaa" //this property alias doesn't seem to work? 5.15.2
              wrapMode: Text.WrapAnywhere
          
              Text{
                  id: innerPlaceholderText
                  visible: !textArea.text
          
                  anchors.leftMargin: textArea.leftPadding
                  anchors.rightMargin: textArea.rightPadding
                  anchors.topMargin: textArea.topPadding
                  anchors.bottomMargin: textArea.bottomPadding
                  anchors.fill: parent
                  horizontalAlignment: Text.AlignLeft
                  verticalAlignment: Text.AlignTop
                  wrapMode: textArea.wrapMode
                  color: "#aaa"
              }
          }
          
          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            Qml_beginner
            wrote on last edited by
            #5

            its works fine independently but this way we are not actually using placeholder text, is there some way we can wrap the placeholder text explicitly

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved