Чтобы изменения вступили в силу (т.е. текст стал синим и подчёркнутым), приходится перезагружать документ (textEdit->setHtml(textEdit->toHtml()).
// QTextCharFormat::setAnchorHref() does not work properly.
// When we select the text and apply setAnchorHref() the
// text format does not change to blue-colored, underlined.
// But QTextEdit::toHtml() show the presence of <a...> tag.
// To update text format we have to reload the document as:
// textEdit->setHtml(textEdit->toHtml())
// WinXP X64 (SP2) EN + Qt4.6.2 + Qt Creator 1.3.1 +
// gcc4.4 (from ftp://ftp.qt.nokia.com/misc/MinGW-gcc440_1.zip)
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextEdit* textEdit = new QTextEdit;
textEdit->setHtml("Hello World!");
textEdit->show();
textEdit->selectAll();
QTextCharFormat fmt = textEdit->currentCharFormat();
fmt.setAnchor(true);
fmt.setAnchorHref("http://qt.nokia.com/");
textEdit->setCurrentCharFormat(fmt);
qDebug() << textEdit->toHtml(); // there is <a href=... > tag in the debug output,
// but the text format does not change to blue-colored, underlined.
// if we uncomment the next line of code the hyperlink will be formatted properly
// textEdit->setHtml(textEdit->toHtml());
return a.exec();
}
Прошу пользователей форума посмотреть, повторяется ли у них этот баг или нет.