Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Make a line break in a layout or container

Make a line break in a layout or container

Scheduled Pinned Locked Moved Solved General and Desktop
25 Posts 4 Posters 4.5k 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.
  • E Offline
    E Offline
    EL-jos
    wrote on last edited by
    #1

    Hello, everyone,

    I have a problem with my QHBoxLayout and QLabel;
    here's my problem: I have a widget that uses the QHBoxLayout layout and in this layout I want to put a QLabel containing text, so I want every time the size of a line of text touches the border of my Widget, that it has a line return for the following lines and so on.

    Note: in HTML and CSS, we use the property:
    p{

    word-wrap: break-word;
    }

    here's an image to be a little clearer:
    0_1541124864575_myCapture.PNG

    Please tell me how can I do it in my QHBoxLayout?

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Did you try to use the setWordWrap(..) function ?

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      5
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi
        Just as a note.
        QLabel seems to wordwrap at \n and will not break a line in other places like
        QTextEdit ->QTextOption::WrapMode. with
        QTextOption::WrapAtWordBoundaryOrAnywhere

        so i not sure it will help here as it seems to be URLS and they are contain no line breaks to break on

        i had a looked at QLabel source but its all hidden away in private classes so
        it does not seem possible to change the used QTextOption in any easy way.

        So one option is to use PlainTextWidget in readOnly mode
        and let it word break the URLS.
        However, if you use QLabels ability to click on links (setOpenExternalLinks(true);)
        you might need to respond to clicks on TextEdit and call QDesktopServices::openUrl()
        I did not test if TextEdit also can do that but its unlikely it will in readonly mode.

        Can you tell a bit about what you use the QLabels for, besides showing the URL ?

        1 Reply Last reply
        3
        • E Offline
          E Offline
          EL-jos
          wrote on last edited by
          #4

          in fact my widget will be used to post the message of each client or the message is in a QLabel, here is an example with whatsap:
          0_1541185726027_myCapture2.PNG

          mrjjM 1 Reply Last reply
          0
          • E EL-jos

            in fact my widget will be used to post the message of each client or the message is in a QLabel, here is an example with whatsap:
            0_1541185726027_myCapture2.PNG

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            @EL-jos
            Oh, so its not URLS at all.
            Do you need rich text stuff like bold, and images too ?
            or JUST text ?

            1 Reply Last reply
            0
            • E Offline
              E Offline
              EL-jos
              wrote on last edited by
              #6

              @mrjj said in Make a line break in a layout or container:

              Oh, so its not URLS at all.
              Do you need rich text stuff like bold, and images too ?
              or JUST text ?

              Yes, I want the rich text and images because I have to create an emoji system (smile)

              mrjjM 1 Reply Last reply
              0
              • E EL-jos

                @mrjj said in Make a line break in a layout or container:

                Oh, so its not URLS at all.
                Do you need rich text stuff like bold, and images too ?
                or JUST text ?

                Yes, I want the rich text and images because I have to create an emoji system (smile)

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @EL-jos
                Ok, so QTextEdit could be used as it support all. including word wrap at random positions.
                But QLabel also do when there are spaces in the text as there should be with chat text.

                1 Reply Last reply
                1
                • E Offline
                  E Offline
                  EL-jos
                  wrote on last edited by
                  #8

                  @mrjj said in Make a line break in a layout or container:

                  Ok, so QTextEdit could be used as it support all. including word wrap at random positions.
                  But QLabel also do when there are spaces in the text as there should be with chat text.

                  Thank you for the answer but how do I get the images?

                  Note: so insert the images for example in the text during a conversation as shown in the image above?

                  mrjjM 1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi,

                    Since it's going to be a chat like visualisation, you might want to consider using Qt's model view framework. Otherwise you might going to instantiate lots of widgets and finally hit a performance hit. A custom QStyledItemDelegate will likely give you the best result.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    3
                    • E EL-jos

                      @mrjj said in Make a line break in a layout or container:

                      Ok, so QTextEdit could be used as it support all. including word wrap at random positions.
                      But QLabel also do when there are spaces in the text as there should be with chat text.

                      Thank you for the answer but how do I get the images?

                      Note: so insert the images for example in the text during a conversation as shown in the image above?

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #10

                      @EL-jos
                      Hi
                      You can insert images via the supported subset of html.
                      Oh, SGaist beat me to it. It will fast become slow, however if u have long
                      conversations.

                      1 Reply Last reply
                      0
                      • E Offline
                        E Offline
                        EL-jos
                        wrote on last edited by
                        #11

                        @SGaist said in Make a line break in a layout or container:

                        Since it's going to be a chat like visualisation, you might want to consider using Qt's model view framework. Otherwise you might going to instantiate lots of widgets and finally hit a performance hit. A custom QStyledItemDelegate will likely give you the best result.

                        Hi sorry I don't know how it works the QStyledItemDelegate

                        1 Reply Last reply
                        0
                        • E Offline
                          E Offline
                          EL-jos
                          wrote on last edited by
                          #12

                          @mrjj said in Make a line break in a layout or container:

                          Hi
                          You can insert images via the supported subset of html.
                          Oh, SGaist beat me to it. It will fast become slow, however if u have long
                          conversations.

                          Thank you for the answer,
                          can you give me an example so I can do a simple simulation?

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            Are you already familiar with Qt's model view programming ?

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • E Offline
                              E Offline
                              EL-jos
                              wrote on last edited by
                              #14

                              @mrjj said in Make a line break in a layout or container:

                              You mean test of Qlabels ?

                              no the test with a QTextEdit and an HTML subset to display an image as in the conversation given as an example above

                              1 Reply Last reply
                              0
                              • E Offline
                                E Offline
                                EL-jos
                                wrote on last edited by
                                #15

                                @SGaist said in Make a line break in a layout or container:

                                Are you already familiar with Qt's model view programming ?

                                not just the basics too much

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by VRonin
                                  #16

                                  Then you should also take a look at @VRonin and @kshegunov's chat example project.

                                  [VRonin edited to proper attribute credit]

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  2
                                  • E Offline
                                    E Offline
                                    EL-jos
                                    wrote on last edited by
                                    #17

                                    @SGaist said in Make a line break in a layout or container:

                                    Then you should also take a look at @VRonin's chat example project.

                                    All right, I'll take a look later, since @mrjj is offering me a rather simple solution.

                                    mrjjM 1 Reply Last reply
                                    0
                                    • E EL-jos

                                      @SGaist said in Make a line break in a layout or container:

                                      Then you should also take a look at @VRonin's chat example project.

                                      All right, I'll take a look later, since @mrjj is offering me a rather simple solution.

                                      mrjjM Offline
                                      mrjjM Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on last edited by mrjj
                                      #18

                                      @EL-jos
                                      sorry but its not a good solution for a chat app :)
                                      Delegate would be much better. I was too quick to suggest QLabel/TextEdit.
                                      It will not scroll nice, very fast.

                                      Anyway, you can have images in Qlabel
                                      alt text

                                       label->setText("<html><head/><body><p>thi is the text that says heeelo hello</p><p><img src=\":/test.jpg\"/>and more hello</p></body></html>");
                                      

                                      Notice the syntax for the image
                                      ":/test.jpg"
                                      its in a resource file.

                                      There is an editor if you right click the QLabel on the form.
                                      alt text

                                      1 Reply Last reply
                                      0
                                      • E Offline
                                        E Offline
                                        EL-jos
                                        wrote on last edited by
                                        #19

                                        Okay, thank you very much for your example.
                                        but why do you say a Delegate is much better?
                                        what are the advantages of the Delegate?
                                        and what are the disadvantages of rich text with HTML image?

                                        mrjjM 1 Reply Last reply
                                        0
                                        • E EL-jos

                                          Okay, thank you very much for your example.
                                          but why do you say a Delegate is much better?
                                          what are the advantages of the Delegate?
                                          and what are the disadvantages of rich text with HTML image?

                                          mrjjM Offline
                                          mrjjM Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #20

                                          @EL-jos
                                          A Delegate is a custem draw for a ListView ( or any view)
                                          So instead of drawing the normal cell, you make a small class where you use
                                          QPainter to draw the cell how you want it.
                                          Its much faster than having a tons of QLabels in a layout as
                                          Views has been optimized for having many items.

                                          so the clear benefit is that it works very fast and it can draw however you like.
                                          (well you have to program that part)

                                          Using html , you would have to construct the right html to have it look how u want.
                                          Some thing be much harder. Like the clock/time in the whatapps sample.

                                          With Delegate you can simply paint it there.

                                          However, learning to use Views and Delegates will take some practice.

                                          1 Reply Last reply
                                          2

                                          • Login

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