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. Trying to set the colour for hyperlinks in QTextBrowser

Trying to set the colour for hyperlinks in QTextBrowser

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 2.2k Views 3 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.
  • PerdrixP Offline
    PerdrixP Offline
    Perdrix
    wrote on last edited by Perdrix
    #1

    I added the following at the start of the text browser content:

    <head><style>a:link {color: #e0ffff;}</style></head> <body bgcolor=#1e1e1e>

    using insertHTML().

    I expected it to change the colour of hyperlinks, but it doesn't seem to do so.

    What am I doing wrong please?

    JonBJ Gojir4G 2 Replies Last reply
    0
    • PerdrixP Perdrix

      I added the following at the start of the text browser content:

      <head><style>a:link {color: #e0ffff;}</style></head> <body bgcolor=#1e1e1e>

      using insertHTML().

      I expected it to change the colour of hyperlinks, but it doesn't seem to do so.

      What am I doing wrong please?

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @Perdrix
      My recommendation: first determine how links are being output as HTML in QTextBrowser, e.g. are they <a> elements, does it maybe insert a <span>? Then get whatever working directly on the HTML link. Finally try moving that to the <head><style>a:link ... global rule.

      Remember QTextBrowser is by no means full HTML, only certain things work.

      PerdrixP 1 Reply Last reply
      0
      • JonBJ JonB

        @Perdrix
        My recommendation: first determine how links are being output as HTML in QTextBrowser, e.g. are they <a> elements, does it maybe insert a <span>? Then get whatever working directly on the HTML link. Finally try moving that to the <head><style>a:link ... global rule.

        Remember QTextBrowser is by no means full HTML, only certain things work.

        PerdrixP Offline
        PerdrixP Offline
        Perdrix
        wrote on last edited by
        #3

        @JonB The links added to the text browser are simple <a href = "%1">%2</a> with .arg substitution.

        PerdrixP 1 Reply Last reply
        0
        • PerdrixP Perdrix

          @JonB The links added to the text browser are simple <a href = "%1">%2</a> with .arg substitution.

          PerdrixP Offline
          PerdrixP Offline
          Perdrix
          wrote on last edited by
          #4

          @Perdrix Should I instead be using setStyleSheet() on the text browser object? If so what should I specify as the string for that?

          JoeCFDJ 1 Reply Last reply
          0
          • PerdrixP Perdrix

            @Perdrix Should I instead be using setStyleSheet() on the text browser object? If so what should I specify as the string for that?

            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by JoeCFD
            #5

            @Perdrix QTextBrowser inherits QTextEdit

            #include <QApplication>
            #include <QTextEdit>
            #include <QTextDocument>
            
            int main(int argc, char *argv[]) {
                QApplication a(argc, argv);
            
                // Create a QTextEdit widget
                QTextEdit textEdit;
            
                // Set HTML content with color and style
                QString htmlContent = "<span style='color:red; font-weight:bold;'>Hello, <i>world!</i></span>";
                textEdit.setHtml(htmlContent);
            
                // Show the QTextEdit widget
                textEdit.show();
            
                return a.exec();
            }
            
            PerdrixP 1 Reply Last reply
            0
            • JoeCFDJ JoeCFD

              @Perdrix QTextBrowser inherits QTextEdit

              #include <QApplication>
              #include <QTextEdit>
              #include <QTextDocument>
              
              int main(int argc, char *argv[]) {
                  QApplication a(argc, argv);
              
                  // Create a QTextEdit widget
                  QTextEdit textEdit;
              
                  // Set HTML content with color and style
                  QString htmlContent = "<span style='color:red; font-weight:bold;'>Hello, <i>world!</i></span>";
                  textEdit.setHtml(htmlContent);
              
                  // Show the QTextEdit widget
                  textEdit.show();
              
                  return a.exec();
              }
              
              PerdrixP Offline
              PerdrixP Offline
              Perdrix
              wrote on last edited by
              #6

              @JoeCFD I don't think that's relevant to the colour of hyperlinks ...

              JoeCFDJ 1 Reply Last reply
              1
              • PerdrixP Perdrix

                @JoeCFD I don't think that's relevant to the colour of hyperlinks ...

                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by JoeCFD
                #7

                @Perdrix Try it out

                #include <QApplication>
                #include <QTextEdit>
                #include <QTextDocument>
                
                #include <iostream>
                
                int main(int argc, char *argv[]) 
                {
                    QApplication app(argc, argv);
                
                    // Create a QTextEdit widget
                    QTextEdit textEdit;
                    
                    QString htmlContent = QString( "<head></head><body><p>Visit the <a href=\"https://www.example.com\"><span style=\"color: blue;\">Example Website</span></a>.</p> </body>" );
                
                    // Set HTML content with color and style
                    //QString htmlContent = "<span style='color:red; font-weight:bold;'>Hello, <i>world!</i></span>";
                    textEdit.setHtml(htmlContent);
                
                    // Show the QTextEdit widget
                    textEdit.show();
                
                    return app.exec();
                }
                
                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  ChrisW67
                  wrote on last edited by
                  #8

                  @Perdrix Your CSS works for me on unvisited links (a:link selector).

                  #include <QApplication>
                  #include <QTextBrowser>
                  #include <QTextDocument>
                  
                  #include <iostream>
                  
                  int main(int argc, char *argv[])
                  {
                      QApplication app(argc, argv);
                      QTextBrowser textBrowser;
                      QString htmlContent = QString( R"(
                  <head>
                    <style>
                      a:link    {color: red; }
                    </style>
                  </head>
                  <body>
                    <p><a href="#link">Link</a></p>
                  </body>
                  )" );
                  
                      textBrowser.setHtml(htmlContent);
                      textBrowser.show();
                      return app.exec();
                  }
                  

                  4373974b-d3b4-4adf-b15c-85e92fbeb452-image.png

                  PerdrixP 1 Reply Last reply
                  0
                  • C ChrisW67

                    @Perdrix Your CSS works for me on unvisited links (a:link selector).

                    #include <QApplication>
                    #include <QTextBrowser>
                    #include <QTextDocument>
                    
                    #include <iostream>
                    
                    int main(int argc, char *argv[])
                    {
                        QApplication app(argc, argv);
                        QTextBrowser textBrowser;
                        QString htmlContent = QString( R"(
                    <head>
                      <style>
                        a:link    {color: red; }
                      </style>
                    </head>
                    <body>
                      <p><a href="#link">Link</a></p>
                    </body>
                    )" );
                    
                        textBrowser.setHtml(htmlContent);
                        textBrowser.show();
                        return app.exec();
                    }
                    

                    4373974b-d3b4-4adf-b15c-85e92fbeb452-image.png

                    PerdrixP Offline
                    PerdrixP Offline
                    Perdrix
                    wrote on last edited by Perdrix
                    #9

                    @ChrisW67 All working now I used:

                    <a style="font-size:16px; color:#e0ffff;" href="link" etc.

                    Thanks to all
                    David

                    JonBJ 1 Reply Last reply
                    0
                    • PerdrixP Perdrix

                      @ChrisW67 All working now I used:

                      <a style="font-size:16px; color:#e0ffff;" href="link" etc.

                      Thanks to all
                      David

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by
                      #10

                      @Perdrix
                      So why didn't what you had work in the first place? What did you change that made it work?

                      PerdrixP 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @Perdrix
                        So why didn't what you had work in the first place? What did you change that made it work?

                        PerdrixP Offline
                        PerdrixP Offline
                        Perdrix
                        wrote on last edited by
                        #11

                        @JonB I've no idea

                        1 Reply Last reply
                        0
                        • PerdrixP Perdrix

                          I added the following at the start of the text browser content:

                          <head><style>a:link {color: #e0ffff;}</style></head> <body bgcolor=#1e1e1e>

                          using insertHTML().

                          I expected it to change the colour of hyperlinks, but it doesn't seem to do so.

                          What am I doing wrong please?

                          Gojir4G Offline
                          Gojir4G Offline
                          Gojir4
                          wrote on last edited by
                          #12

                          @Perdrix Sorry to come later on this. But there is an interesting note about this in documentation of QPalette:

                          Note that we do not use the Link and LinkVisited roles when rendering rich text in Qt, and that we recommend that you use CSS and the QTextDocument::setDefaultStyleSheet() function to alter the appearance of links. For example:

                          QTextBrowser browser;
                          QColor linkColor(Qt::red);
                          QString sheet = QString::fromLatin1("a { text-decoration: underline; color: %1 }").arg(linkColor.name());
                          browser.document()->setDefaultStyleSheet(sheet);
                          
                          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