PDA

View Full Version : insert hyperlink(URL) inside the TextEdit..



rleojoseph
4th January 2011, 14:26
Hi, i can add the path to textedit area, but i want that url to be like a hyperlink. Does any1 got any idea?? All i need is that , the URL which i am inserting sholud be Clickable format.
Even i tried with the QTextBrowser and QTextCharFormat. i couldnt find out the solution.

I will be very much thankful. Pleassseeeeeeeee.

Lykurg
4th January 2011, 15:58
What about inserting HTML instead of plain text?

rleojoseph
5th January 2011, 06:15
textedit.insertHtml will insert only the link/path. but the path is not highlighted like a URL. please reply if u know the answer.

Lykurg
5th January 2011, 08:05
I was talking about interting HTML, not inserting plain text via the setHtml function. You might also want have a look at the textInteractionFlags.
(If you would have post some code what you have tried, it would be easier to help.)

rleojoseph
5th January 2011, 09:02
void TextPad::inserturl()
{

bool ok;
QString baseUrl= QInputDialog::getText(this, tr("URL"),tr("Link:"), QLineEdit::Normal, "", &ok);
ui->textEdit->setHtml(baseUrl);

}

This is like, a dialog will open and i will enter the URL in that. aftr that the url is displayed in the textedit, but not as a hyperlink.

Waiting for ur reply

Lykurg
5th January 2011, 09:11
Yeah, I guessed that... look:

ui->textEdit->setHtml("http://qtcentre.org");
inserts plain text even if you use setHtml. But with that
ui->textEdit->setHtml("<a href=\"http://qtcentre.org\">http://qtcentre.org</a>"); you insert HTML. You might also want to check the user input first.

rleojoseph
5th January 2011, 10:57
Thank u very much! That is working but the link is not like a clickable format .

This is what am going to do. Insert a URL in a TextEdit and click the link that will open in a default browser or Webview.

But my work is to load the the URL from the Dialog Box.
QString baseUrl= QInputDialog::getText(this, tr("URL"),tr("Link:"), QLineEdit::Normal, "", &ok);
ui->textEdit->setHtml(???????);

Lykurg
5th January 2011, 14:30
Well I won't write the program for you. See textInteractionFlags(), check for a signal which is emitted when someone clicks the link and react on that bay opening the default browser etc. See also QDesktopServices.

And NOTICE the [CODE] tags!

rleojoseph
6th January 2011, 05:29
Thanks a lot again!! Am working on it. I have another question,If you wish you can reply. is it possible to embed a link to a image. I mean using the image as a link. Its like , embedding the link/path of a file to that image.


Thanks in Advance!!!!

Lykurg
6th January 2011, 06:44
Yes it is possible and all related the HTML. See the img tag: http://www.w3schools.com/tags/tag_img.asp. (Also handy: the Qt resource System)

rleojoseph
6th January 2011, 07:15
Am workin on QT4.7. Lets say am selecting a file like *.doc and instead of opening it i am goin to insert a image/icon in a TEXTEDIT.Now when i click that IMAGE the *.doc file should open. Thats why i thought of inserting an image when a file is selected, and when i click the image the file should open.