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. Image centering in QML TextArea not working
Forum Updated to NodeBB v4.3 + New Features

Image centering in QML TextArea not working

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 2 Posters 362 Views 1 Watching
  • 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.
  • F Offline
    F Offline
    Fausto01
    wrote on 17 Nov 2024, 22:35 last edited by
    #1

    Hello everyone, I have this code:

    import QtQuick 2.15
    import QtQuick.Window 2.15
    import QtQuick.Controls 2.15
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        TextArea {
            anchors.fill: parent
            textFormat: TextArea.RichText
            text: "<center>TEST TEXT<br>
                  <img src=\"https://cdn.pixabay.com/photo/2014/06/03/19/38/road-sign-361514_960_720.png\" height=300 width=300/><br>
                  OTHER TEST TEXT</center>"
            readOnly: true
            padding: 0
        }
    }
    

    The text is correctly centered while the image is not. Am I doing something wrong?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mzimmers
      wrote on 19 Nov 2024, 16:35 last edited by
      #2

      That's not how text and images are normally declared in QML. Use the horizontalAlignment and verticalAlignment properties of the TextArea. And, though I'm not sure what you're trying to do, I think you'd want to use an Image declaration, and set its properties to place it where you want.

      F 1 Reply Last reply 19 Nov 2024, 16:49
      0
      • M mzimmers
        19 Nov 2024, 16:35

        That's not how text and images are normally declared in QML. Use the horizontalAlignment and verticalAlignment properties of the TextArea. And, though I'm not sure what you're trying to do, I think you'd want to use an Image declaration, and set its properties to place it where you want.

        F Offline
        F Offline
        Fausto01
        wrote on 19 Nov 2024, 16:49 last edited by
        #3

        @mzimmers I'm just trying to render an HTML text. The text comes from a library and it can contains images. The problem is that if you put the html above in a file and open it, everything works fine and the image is centered, if you try to render it in a TextArea the image is not centered.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mzimmers
          wrote on 19 Nov 2024, 17:19 last edited by
          #4

          The text comes from a library? Do you mean you're automatically populating the text property of the TextArea, or are you just copying/pasting from the library into your QML?

          F 1 Reply Last reply 19 Nov 2024, 21:36
          0
          • M mzimmers
            19 Nov 2024, 17:19

            The text comes from a library? Do you mean you're automatically populating the text property of the TextArea, or are you just copying/pasting from the library into your QML?

            F Offline
            F Offline
            Fausto01
            wrote on 19 Nov 2024, 21:36 last edited by
            #5

            @mzimmers Sorry if I didn't explain it well, what I mean is that the text I've inserted in the TextArea is just for a minimal reproducible example. In my code that text comes from a library that populates it dynamically.
            Anyway, should the code I've posted work or is TextArea unable to handle images correctly like a browser? The fact that the image is displayed led me to think this should have worked.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mzimmers
              wrote on 19 Nov 2024, 21:46 last edited by
              #6

              It isn't that TextArea isn't able to "handle" Images; you can indeed place images within a TextArea and control their location. The problem is, your image is embedded in the text property of the TextArea. I've never seen this done before, but I think you'd have to use HTML/CSS constructs within the <img> in order to do anything (and I'm not sure this will work).

              1 Reply Last reply
              0

              2/6

              19 Nov 2024, 16:35

              topic:navigator.unread, 4
              • Login

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