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. Better way to elide multiline text?
Forum Update on Monday, May 27th 2025

Better way to elide multiline text?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
12 Posts 3 Posters 3.7k 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.
  • D Diracsbracket
    16 Aug 2018, 13:22

    @SeDi
    What about writing your own QML Text type, e.g. based on the following C++ example:
    http://doc.qt.io/qt-5/qtwidgets-widgets-elidedlabel-example.html

    S Offline
    S Offline
    SeDi
    wrote on 17 Aug 2018, 22:55 last edited by
    #3

    Thank you, @Diracsbracket, you are absolutely right, that's a much more elegant way! I'll have a look into that!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SeDi
      wrote on 17 Aug 2018, 23:27 last edited by
      #4

      Uh. :-( Seems to me that the QML Text Type headers are private. Is that correct?

      I am not sure if this is the correct header: qquicktext_p.h

      It would be sad.

      D 1 Reply Last reply 18 Aug 2018, 04:10
      0
      • S SeDi
        17 Aug 2018, 23:27

        Uh. :-( Seems to me that the QML Text Type headers are private. Is that correct?

        I am not sure if this is the correct header: qquicktext_p.h

        It would be sad.

        D Offline
        D Offline
        Diracsbracket
        wrote on 18 Aug 2018, 04:10 last edited by
        #5

        @SeDi
        Can't you derive your class from QLabel as in the example? I wouldn't know how to reimplement the paintEvent using the private QQuickText class.

        1 Reply Last reply
        1
        • S Offline
          S Offline
          SeDi
          wrote on 19 Aug 2018, 09:38 last edited by SeDi
          #6

          As QLabel belongs to the QWidget world, that wouldn't work in QML, or would it? I am not really sure here. At least I'd think it strange to include widgets in my project.

          Looking around, I've found this thread that actually kind of discourages me a bit:

          QML uses SceneGraph for painting, where QWidgets use a raster engine for painting. So they unfortunately dont play very well together.
          [...] You would need to render the widget as a texture on every paint event.
          

          But, on the other hand: by now Qt is 3 years older. I am not sure what to do.

          I have looked out for non-private headers in the folder QT\5.11.1\Src\qtdeclarative\src\quick\items and found these:

          • qquickframebufferobject.h
          • qquickitem.h
          • qquickitemgrabresult.h
          • qquickpainteditem.h
          • qquickrendercontrol.h
          • qquicktextdocument.h
          • qquickview.h
          • qquickwindow.h

          I think I've seen the use of QQuickItem and QQuickPaintedItem before. QQuickTextDocument seems interesting, but I don't understand, if it is private or not, because of the use of Q_DECLARE_PRIVATE here:

          public:
              QQuickTextDocument(QQuickItem *parent);
              QTextDocument *textDocument() const;
          
          private:
              Q_DISABLE_COPY(QQuickTextDocument)
              Q_DECLARE_PRIVATE(QQuickTextDocument)
          

          Has anyone done a similar thing?
          Any (perhaps different) ideas?

          D 1 Reply Last reply 20 Aug 2018, 06:06
          1
          • S SeDi
            19 Aug 2018, 09:38

            As QLabel belongs to the QWidget world, that wouldn't work in QML, or would it? I am not really sure here. At least I'd think it strange to include widgets in my project.

            Looking around, I've found this thread that actually kind of discourages me a bit:

            QML uses SceneGraph for painting, where QWidgets use a raster engine for painting. So they unfortunately dont play very well together.
            [...] You would need to render the widget as a texture on every paint event.
            

            But, on the other hand: by now Qt is 3 years older. I am not sure what to do.

            I have looked out for non-private headers in the folder QT\5.11.1\Src\qtdeclarative\src\quick\items and found these:

            • qquickframebufferobject.h
            • qquickitem.h
            • qquickitemgrabresult.h
            • qquickpainteditem.h
            • qquickrendercontrol.h
            • qquicktextdocument.h
            • qquickview.h
            • qquickwindow.h

            I think I've seen the use of QQuickItem and QQuickPaintedItem before. QQuickTextDocument seems interesting, but I don't understand, if it is private or not, because of the use of Q_DECLARE_PRIVATE here:

            public:
                QQuickTextDocument(QQuickItem *parent);
                QTextDocument *textDocument() const;
            
            private:
                Q_DISABLE_COPY(QQuickTextDocument)
                Q_DECLARE_PRIVATE(QQuickTextDocument)
            

            Has anyone done a similar thing?
            Any (perhaps different) ideas?

            D Offline
            D Offline
            Diracsbracket
            wrote on 20 Aug 2018, 06:06 last edited by Diracsbracket
            #7
            This post is deleted!
            1 Reply Last reply
            0
            • G Offline
              G Offline
              GrecKo
              Qt Champions 2018
              wrote on 20 Aug 2018, 13:14 last edited by
              #8

              Why don't you use a Text item for each line and wrap them in a Column ?

              S 1 Reply Last reply 22 Aug 2018, 23:32
              3
              • G GrecKo
                20 Aug 2018, 13:14

                Why don't you use a Text item for each line and wrap them in a Column ?

                S Offline
                S Offline
                SeDi
                wrote on 22 Aug 2018, 23:32 last edited by
                #9

                @GrecKo: Thank you! This is exactly that kind of idea I have been to stupid to come up with by myself. Now that you write it, it seems kind of obvious. Works like a charm and is shorter, faster, smarter and so much better readable . As a bonus: the TextMetric does not elide styledText correctly - Text does :-)).

                For posterity, here's my code:

                Column {
                                    anchors.fill: parent
                                    Repeater {
                                        id: textRepeater
                                        property var modelVar: styleData.value.includes('<br>') ?
                                                                   styleData.value.split('<br>')
                                                                 : styleData.value.split('\n')
                                        model: modelVar
                                        Text {
                                            id: textItem
                                            width: parent.width
                                            height: textRepeater.count > 0 ? parent.height / textRepeater.count : parent.height
                                            text: textRepeater.modelVar[index].trim()
                                            verticalAlignment: Text.AlignVCenter
                                            horizontalAlignment: Text.AlignHCenter 
                                            anchors.leftMargin: 5
                                            color: textColor
                                            textFormat: Text.StyledText
                                            elide: Text.ElideMiddle 
                                        }
                                    }
                                }
                
                1 Reply Last reply
                1
                • G Offline
                  G Offline
                  GrecKo
                  Qt Champions 2018
                  wrote on 23 Aug 2018, 07:36 last edited by
                  #10

                  If you are using height: parent.height / textRepeater.count, you might want to use ColumnLayout instead of Column, it will handle the height of its children automatically.

                  S 1 Reply Last reply 24 Aug 2018, 21:30
                  1
                  • G GrecKo
                    23 Aug 2018, 07:36

                    If you are using height: parent.height / textRepeater.count, you might want to use ColumnLayout instead of Column, it will handle the height of its children automatically.

                    S Offline
                    S Offline
                    SeDi
                    wrote on 24 Aug 2018, 21:30 last edited by
                    #11
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SeDi
                      wrote on 24 Aug 2018, 21:34 last edited by SeDi
                      #12

                      Thank you, @GrecKo, for that additional idea!

                      I've checked it out, but I always have problems with the layouts, because QML types just behave so differently when inside a layout. From looking this way when living inside a Column:
                      0_1535146429339_löschen2.png

                      it changes to this when sitting inside a ColumnLayout
                      0_1535146439920_löschen.png

                      I did get it better, eventually, by setting Layout.maximumWidth instead of width, but still the horizontal centering doesn't work:
                      0_1535146449909_löschen3.png

                      Finally I could solve that by also setting Layout.preferredWidth, but then I returned to just take the Column approach.

                      Having had many problems in the past with obviously not correctly understanding how Layouts actually work in QML, I have lately started to avoid them quite generally.

                      Here, Column works just fine :-)

                      1 Reply Last reply
                      0

                      12/12

                      24 Aug 2018, 21:34

                      • Login

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