我想用QTextEdit
(在只读模式下)显示可点击的超链接,我曾经这样做过
QTextEdit *textEdit = new QTextEdit;
QTextCursor cursor(textEdit->document());
textEdit->setTextCursor(cursor);
cursor->insertHtml("<a href=\"www.google.com\" >Google</a>");
textEdit->show();
此代码会将 Google 显示为超链接,但无法单击。
如果我用过
QTextEdit *textEdit = new QTextEdit;
QTextCursor cursor(textEdit->document());
textEdit->setTextCursor(cursor);
QTextCharFormat linkFormat = cursor.charFormat();
linkFormat.setAnchor(true);
linkFormat.setAnchorHref("http://www.google.com");
linkFormat.setAnchorName("Google");
cursor.insertText("Google", linkFormat);
然后什么也没有发生。 “Google”只是普通文本。
请帮我插入可点击的超链接QTextEdit
.
Using QTextBrowser
更简单(正如另一个答案所建议的)。但是,如果由于某种原因你想使用QTextEdit
,尝试使用更改文本交互标志setTextInteractionFlags()
。
我认为你必须启用Qt::LinksAccessibleByMouse
flag.
See Qt::TextInteractionFlag and QTextEdit::textInteractionFlags
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)