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. QTextBrowser tutorial?
Forum Updated to NodeBB v4.3 + New Features

QTextBrowser tutorial?

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 2 Posters 4.3k Views 2 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.
  • P Offline
    P Offline
    Pippin
    wrote on last edited by
    #1

    Hello,

    I couldn't find any tutorial for QTextBrowsers, so I have some questions:

    1. How do I append an url link? For example:
    foo->append("http://www.google.com");
    

    The link wouldn't be automatically url, so clicking it wouldn't do anything.

    1. Other question: if I want a given function to be run whenever a word in my QTextBrowser is clicked, how should I do it? For example:
    foo->append("Change background to: Red, Orange, Yellow, Green, Blue, Purple.");
    

    I would like Qt to run a function (in this example, a function that would set the background to Qt::red) whenever the word "Red" is clicked, etc: how am I supposed to make that happen?

    Thanks in advance!

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

      hi
      Its HTML so you need to insert html with tags etc.

      You need to set the acceptRichText property to true.
      Then you can just pass in HTML using the setHTML() slot.
      tb.setHTML("<a href="http://foo>Bar</a>");

      to catch when user click on such link use the anchorClicked signal
      ala
      connect(textBrowser, SIGNAL(anchorClicked(QUrl)), this, SLOT(MyOwnSlot(QUrl)));

      and you might want to set openLinks to false
      http://doc.qt.io/qt-5.5/qtextbrowser.html#anchorClicked

      1 Reply Last reply
      1
      • P Offline
        P Offline
        Pippin
        wrote on last edited by
        #3

        Thank you @mrjj!

        @mrjj said:

        to catch when user click on such link use the anchorClicked signal
        ala
        connect(textBrowser, SIGNAL(anchorClicked(QUrl)), this, SLOT(MyOwnSlot(QUrl)));

        I am already using these shenanigans for real (Internet) Urls, but I'm not sure I know much about these issues. If we use the example I described in the OP, how would you make "Blue" or "Green" urls (ie. blue and underlined) ? How would you manage to determine which Url is clicked ? (Is it "Blue"? Is it "Green"? etc.)

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

          ++If we use the example I described in the OP, how would you make "Blue" or "Green" urls (ie. blue and underlined) ?
          Using the subset of html it supports.
          try with
          <font color="#0000FF">This will make your text blue.</font>

          ++How would you manage to determine which Url is clicked ? (Is it "Blue"? Is it "Green"? etc.)
          well you can string check the name. you get the url text so you must compared that as string.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            Pippin
            wrote on last edited by
            #5
            <a href="foo">bar</a>
            

            Is foo or bar the url "name" or "text", as you called it?

            mrjjM 1 Reply Last reply
            0
            • P Pippin
              <a href="foo">bar</a>
              

              Is foo or bar the url "name" or "text", as you called it?

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

              @Pippin
              well im not sure what will be in url.
              just try it
              i think foo.
              but just make function and qDebug() it and see

              P 1 Reply Last reply
              1
              • mrjjM mrjj

                @Pippin
                well im not sure what will be in url.
                just try it
                i think foo.
                but just make function and qDebug() it and see

                P Offline
                P Offline
                Pippin
                wrote on last edited by Pippin
                #7

                @mrjj Well if I put a quint32 as the url:

                foo->append("<a href=" + ref + ">" + q_string + "</a>");
                

                I never get ref back. When I run the slot:

                void MyInterface::openUrl(const QUrl& foo) const
                {
                	std::cout << foo.url().toStdString() << std::endl;
                }
                

                I print a character such as R or x but never something like 4144357970. Also, foo.toString().toStdString() gives the same result.

                mrjjM 1 Reply Last reply
                0
                • P Pippin

                  @mrjj Well if I put a quint32 as the url:

                  foo->append("<a href=" + ref + ">" + q_string + "</a>");
                  

                  I never get ref back. When I run the slot:

                  void MyInterface::openUrl(const QUrl& foo) const
                  {
                  	std::cout << foo.url().toStdString() << std::endl;
                  }
                  

                  I print a character such as R or x but never something like 4144357970. Also, foo.toString().toStdString() gives the same result.

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

                  hi
                  can you try to use use
                  foo->append("<a href=" + QString::number(ref) + ">" + q_string + "</a>");
                  as I have shot my foot off many times with + int +

                  P 1 Reply Last reply
                  1
                  • mrjjM mrjj

                    hi
                    can you try to use use
                    foo->append("<a href=" + QString::number(ref) + ">" + q_string + "</a>");
                    as I have shot my foot off many times with + int +

                    P Offline
                    P Offline
                    Pippin
                    wrote on last edited by
                    #9

                    @mrjj said:

                    QString::number(ref)

                    That was it! Thanks a bunch! :D

                    mrjjM 1 Reply Last reply
                    0
                    • P Pippin

                      @mrjj said:

                      QString::number(ref)

                      That was it! Thanks a bunch! :D

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

                      @Pippin
                      super. :)
                      i used to do that often myself as it do not complains so I forgot :)

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        Pippin
                        wrote on last edited by
                        #11

                        Just wondering, is it normal that such a QUrl (that is, a QUrl constructed with a quint32 alone) returns true when QUrl::isValid() is called? Because that's what I get and I don't understand why 4144357970 is considered "valid."

                        mrjjM 1 Reply Last reply
                        0
                        • P Pippin

                          Just wondering, is it normal that such a QUrl (that is, a QUrl constructed with a quint32 alone) returns true when QUrl::isValid() is called? Because that's what I get and I don't understand why 4144357970 is considered "valid."

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

                          @Pippin said:

                          QUrl::isValid()

                          it says
                          "the URL is run through a conformance test. Every part of the URL must conform to the standard encoding rules of the URI standard for the URL to be reported as valid."

                          So I do agree that its strange if isValid is true..
                          so I guess 4144357970 is following the rules.

                          1 Reply Last reply
                          0
                          • P Offline
                            P Offline
                            Pippin
                            wrote on last edited by
                            #13

                            So far any quint32 that I tried is considered a "valid" url. Well since this thread is marked as solved, I'll make a new one.

                            1 Reply Last reply
                            1

                            • Login

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