setAnchorHref() should only contain the href, not any html tags.
I didn't test this but try adjusting something like this to your needs:
if(title.isEmpty()) title = url;
format.setAnchor(true);
format.setAnchorHref(url);
cursor.insertText(title, format);
cursor.setFormat(original);
return cursor;
}
addLink(te->textCursor(), "http://www.qtcentre.org", "QtCentre");
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");
To copy to clipboard, switch view to plain text mode
For adding an image instead of text you will probably want to use QTextCursor::insertImage() instead of insertText():
format.setAnchor(true);
format.setAnchorHref(url);
format.setName(imageName);
cursor.insertImage(format);
QTextImageFormat format;
format.setAnchor(true);
format.setAnchorHref(url);
format.setName(imageName);
cursor.insertImage(format);
To copy to clipboard, switch view to plain text mode
Bookmarks