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. html text can not be justified on both(left and right) sides in qlabel, qtextbrowser and qtextedit

html text can not be justified on both(left and right) sides in qlabel, qtextbrowser and qtextedit

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 626 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.
  • J Offline
    J Offline
    JoeCFD
    wrote on 5 Dec 2023, 17:36 last edited by JoeCFD 12 May 2023, 17:57
    #1

    html text is aligned only to left or right, but not both in qlabel, qtextbrowser or qtextedit. Did anyone have the same problem before?
    I know QWebengineView works with html text for justfied alignment.

    #include <QApplication>
    #include <QTextEdit>
    
    int main(int argc, char *argv[]) {
        QApplication app(argc, argv);
    
        // Create a QTextEdit
        QTextEdit textEdit;
        textEdit.setAlignment( Qt::AlignJustify );
    
        // Set HTML content with justified text alignment
        QString htmlContent = "<html><body>"
                              "<p style='text-align:justify;'>This is a sample text with justified alignment.Additional paragraphs can be added as needed."
                              "Additional paragraphs can be added as needed.This is a sample text with justified alignment.</p>"
                              "</body></html>";
        textEdit.setHtml(htmlContent);
    
        // Remove the border using a style sheet
        textEdit.setStyleSheet("border: none;");
    
        // Show the QTextEdit
        textEdit.show();
    
        return app.exec();
    }
    

    Linux 22.04 and Qt 5.15.3

    P 1 Reply Last reply 5 Dec 2023, 19:18
    0
    • J JoeCFD
      5 Dec 2023, 19:46

      @Pl45m4 It is not a HTML thing. QWebEngineView justifies the html text well. I guess these widgets do not support enough html features. When I use QWebEngineView, it creates a spacing at its top and bottom and I can not reduce it.

      C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 6 Dec 2023, 00:02 last edited by
      #4

      @JoeCFD You can use plain HTML instead of CSS: <p align='justify'>.

      J 1 Reply Last reply 6 Dec 2023, 00:47
      2
      • J JoeCFD
        5 Dec 2023, 17:36

        html text is aligned only to left or right, but not both in qlabel, qtextbrowser or qtextedit. Did anyone have the same problem before?
        I know QWebengineView works with html text for justfied alignment.

        #include <QApplication>
        #include <QTextEdit>
        
        int main(int argc, char *argv[]) {
            QApplication app(argc, argv);
        
            // Create a QTextEdit
            QTextEdit textEdit;
            textEdit.setAlignment( Qt::AlignJustify );
        
            // Set HTML content with justified text alignment
            QString htmlContent = "<html><body>"
                                  "<p style='text-align:justify;'>This is a sample text with justified alignment.Additional paragraphs can be added as needed."
                                  "Additional paragraphs can be added as needed.This is a sample text with justified alignment.</p>"
                                  "</body></html>";
            textEdit.setHtml(htmlContent);
        
            // Remove the border using a style sheet
            textEdit.setStyleSheet("border: none;");
        
            // Show the QTextEdit
            textEdit.show();
        
            return app.exec();
        }
        

        Linux 22.04 and Qt 5.15.3

        P Offline
        P Offline
        Pl45m4
        wrote on 5 Dec 2023, 19:18 last edited by Pl45m4 12 May 2023, 19:19
        #2

        @JoeCFD

        What happens if you

        widget->setAlignment(Qt::AlignJustify)
        

        afterwards (after setting the text/html)?
        Does it work at all? If so, then it must be something with the HTML


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        J 1 Reply Last reply 5 Dec 2023, 19:46
        0
        • P Pl45m4
          5 Dec 2023, 19:18

          @JoeCFD

          What happens if you

          widget->setAlignment(Qt::AlignJustify)
          

          afterwards (after setting the text/html)?
          Does it work at all? If so, then it must be something with the HTML

          J Offline
          J Offline
          JoeCFD
          wrote on 5 Dec 2023, 19:46 last edited by JoeCFD 12 May 2023, 19:47
          #3

          @Pl45m4 It is not a HTML thing. QWebEngineView justifies the html text well. I guess these widgets do not support enough html features. When I use QWebEngineView, it creates a spacing at its top and bottom and I can not reduce it.

          C 1 Reply Last reply 6 Dec 2023, 00:02
          0
          • J JoeCFD
            5 Dec 2023, 19:46

            @Pl45m4 It is not a HTML thing. QWebEngineView justifies the html text well. I guess these widgets do not support enough html features. When I use QWebEngineView, it creates a spacing at its top and bottom and I can not reduce it.

            C Offline
            C Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on 6 Dec 2023, 00:02 last edited by
            #4

            @JoeCFD You can use plain HTML instead of CSS: <p align='justify'>.

            J 1 Reply Last reply 6 Dec 2023, 00:47
            2
            • C Chris Kawa
              6 Dec 2023, 00:02

              @JoeCFD You can use plain HTML instead of CSS: <p align='justify'>.

              J Offline
              J Offline
              JoeCFD
              wrote on 6 Dec 2023, 00:47 last edited by
              #5

              @Chris-Kawa Thanks for your reply, Chris. I tried it and only one line is displayed. I will look into this later.

              C 1 Reply Last reply 6 Dec 2023, 08:41
              0
              • J JoeCFD
                6 Dec 2023, 00:47

                @Chris-Kawa Thanks for your reply, Chris. I tried it and only one line is displayed. I will look into this later.

                C Offline
                C Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on 6 Dec 2023, 08:41 last edited by
                #6

                @JoeCFD I don't have Qt 5 at hand, but it works in 6.6.

                M J 2 Replies Last reply 6 Dec 2023, 10:10
                1
                • C Chris Kawa
                  6 Dec 2023, 08:41

                  @JoeCFD I don't have Qt 5 at hand, but it works in 6.6.

                  M Offline
                  M Offline
                  mpergand
                  wrote on 6 Dec 2023, 10:10 last edited by
                  #7

                  @Chris-Kawa
                  I can confirm it works in Qt 5.15.2.

                  1 Reply Last reply
                  1
                  • C Chris Kawa
                    6 Dec 2023, 08:41

                    @JoeCFD I don't have Qt 5 at hand, but it works in 6.6.

                    J Offline
                    J Offline
                    JoeCFD
                    wrote on 6 Dec 2023, 14:55 last edited by
                    #8

                    @Chris-Kawa Tested it. Works nicely. Many thanks.

                    1 Reply Last reply
                    0
                    • J JoeCFD has marked this topic as solved on 6 Dec 2023, 14:55

                    4/8

                    6 Dec 2023, 00:02

                    • Login

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