QTextBrowser tutorial?



  • 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!


  • Qt Champions 2016

    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



  • 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.)


  • Qt Champions 2016

    ++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.



  • <a href="foo">bar</a>
    

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


  • Qt Champions 2016

    @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



  • @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.


  • Qt Champions 2016

    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 +



  • @mrjj said:

    QString::number(ref)

    That was it! Thanks a bunch! :D


  • Qt Champions 2016

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



  • 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."


  • Qt Champions 2016

    @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.



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.