Solved QTextBrowser tutorial?
-
Hello,
I couldn't find any tutorial for QTextBrowsers, so I have some questions:
- 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.
- 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!
-
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.)
-
++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?
-
@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
orx
but never something like4144357970
. Also,foo.toString().toStdString()
gives the same result. -
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 + -
-
@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
whenQUrl::isValid()
is called? Because that's what I get and I don't understand why4144357970
is considered "valid." -
@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.