PDA

View Full Version : unable to click the hyperlink in textedit



rleojoseph
6th January 2011, 12:07
hi ,
i want to get a url from the dialog box lik this
QString baseUrl= QInputDialog::getText(this, tr("URL"),tr("Link:"), QLineEdit::Normal, "", &ok);

after getting the url, i can only insert the url in the textedit, but it is not looking like a hyperlink.

How to make the link in a clickable format.??????

i have usd anchorclickd too

wysota
6th January 2011, 12:30
The easiest solution is to wrap your url into html link tag (<a>) and add that to the text edit (provided that it is in rich text mode).

Alternatively use QTextCursor::insertText() to insert your url with a proper text char format that contains the anchor.

rleojoseph
6th January 2011, 14:03
wrap my url?

Lets say that i am getting url from the above code
QString baseUrl= QInputDialog::getText(this, tr("URL"),tr("Link:"), QLineEdit::Normal, "", &ok);

now i cant insert the baseUrl into the <a href .......> tag ???

wysota
6th January 2011, 14:38
now i cant insert the baseUrl into the <a href .......> tag ???
Why not?

QString link = QString("<a href=\"%1\">%1</a>").arg(baseUrl);

rleojoseph
7th January 2011, 08:14
ya. its working now. But i got a NEW problem now. after inserting the link in the TextEdit, the text which i am typing now is looking like a hyperlink. i have inserted a link lik www.google.com , whatever the text am typing after that it is like Hyperlink. even i tried a lot with Qtextcursor.

Lykurg
7th January 2011, 08:28
Hey what's up with you? Please do not start multiple threads about the same topic, and I told you days ago to use HTML...

And as to your new problem, you probably didn't close the a tag correct. Please show us the code.

rleojoseph
7th January 2011, 08:41
Sorry for posting multiple threads


here is the code


bool ok;
QString baseUrl= QInputDialog::getText(this, tr("URL"),tr("Link:"), QLineEdit::Normal, "", &ok);
if (ok && !baseUrl.isEmpty())
{

QTextCursor cursor = ui->textEdit->textCursor();
//int i=cursor.position();
//qDebug("%d",i);
QString link = QString("<a href=\"%1\">%1</a>").arg(baseUrl);

cursor.insertHtml(link);

Whatever i am typing after inserting url , all the texts are becoming as hyperlink.

wysota
7th January 2011, 12:51
Make sure the cursor is positioned after the link is closed. For example insert a space after closing the "a" tag. For more advanced things you'll have to use the text document and text cursor api. Bottom line is you need to make sure the current text char format is not the one associated with the link. There are many one that are likely to make it work.

rleojoseph
10th January 2011, 13:15
Hi, So far i have used QImage, QTextCursor, QTextCharFormat to make the image as a link.
And i have used the QTextCharFormat::anchorRef also but when i am inserting the image using
format.setAnchorHref(QString("<a href=\"%1\"> %1 </a> ").arg(file)); , the image is only added not the url.

can any1 can fix this?

Added after 1 21 minutes:

cursor.insertImage will insert only the image, but i couldn't add the link to image.

:confused::confused::(

wysota
10th January 2011, 15:25
setAnchorHref() should only contain the href, not any html tags.

I didn't test this but try adjusting something like this to your needs:

QTextCursor addLink(QTextCursor cursor, const QString &url, QString title = QString()) {
if(title.isEmpty()) title = url;
QTextCharFormat original = cursor.charFormat();
QTextCharFormat format;
format.setAnchor(true);
format.setAnchorHref(url);
cursor.insertText(title, format);
cursor.setFormat(original);
return cursor;
}

QTextEdit *te = ...
addLink(te->textCursor(), "http://www.qtcentre.org", "QtCentre");

For adding an image instead of text you will probably want to use QTextCursor::insertImage() instead of insertText():

QTextImageFormat format;
format.setAnchor(true);
format.setAnchorHref(url);
format.setName(imageName);
cursor.insertImage(format);

rleojoseph
11th January 2011, 10:19
I have did something where we can click the URL in the TextBrowser.
But in TextEdit i can't do that.
There is no signal that can be emitted from the TextEdit when a URL is Clicked. But in QTextBrowser we have the option of anchorClicked.
My Question is how the TextBrowser is implemented in TexEdit, so that we can get a signal when the URL is Clicked in TextEdit.
In simple , i want to get a signal from textedit when a URL is clicked.

???

Lykurg
11th January 2011, 10:37
Oh man, still the same problem, so stick with this thread!

QTextBrowser inherits QTextEdit, not the other way around. If you what to know how the textbrowser can emit a signal like anchorClicked() have a look at the sources and find it out. Qt is open source! I haven't done it, but I guess they are reimplementing mousePressEvent(), check the current cursor at the position the event occur, and look if it is a link. if so, the signal is emitted.

rleojoseph
11th January 2011, 10:55
No sir. I have already done the clicking the URL in TextBrowser widget. But now am trying to implement it in the TextEdit Widget. ok?
The problem is the signals available in the TextEdit are
copyAvailable,currentCharFormatChanged,cursorPosit ionChanged (),redoAvailable,selectionChanged ,textChanged (),undoAvailable.
So whenever i click the URL in the TextEdit which Signal is emitted?? that is what i was asking.

Lykurg
11th January 2011, 11:11
No sir. I have already done the clicking the URL in TextBrowser widget. But now am trying to implement it in the TextEdit Widget. ok?
The title says: "unable to click the hyperlink in textedit". I mixed that up in the first place too.


The problem is the signals available in the TextEdit are
copyAvailable,currentCharFormatChanged,cursorPosit ionChanged (),redoAvailable,selectionChanged ,textChanged (),undoAvailable.
So whenever i click the URL in the TextEdit which Signal is emitted?? that is what i was asking.And the answer has given in my last post: the text edit has no such signal. You have to do it yourself.

rleojoseph
17th January 2011, 07:26
Hi.. This is wat i finally found out. This might be useful for others. The URL can be inserted into a TextEdit but those links cannot be clicked, if u want click events go for TextBrowser.
If you want the click event in the TextEdit, Creating Custom Signals and Slots might be the possible Solution.....:)