QTextBrowser::anchorClicked not emitted
-
Using a widget inheriting from QTextBrowser, and calling in the constructor:
setOpenLinks(false); setOpenExternalLinks(false); setReadOnly(false);
I add a link to a local file to the document this way:
// destinationPath is QString with the file path, e.g. C:/Temp/afile.dat QTextCharFormat fmt; fmt.setAnchor(true); fmt.setAnchorHref(QUrl::fromLocalFile(destinationPath).toString(QUrl::FullyEncoded)); fmt.setForeground(QApplication::palette().color(QPalette::Link)); fmt.setFontUnderline(true); QTextCursor cursor = textCursor(); const QTextCharFormat previousFormat = cursor.charFormat(); cursor.insertText(origFile.fileName(), fmt); cursor.insertText(QStringLiteral(" "), previousFormat);
In the constructor I also do the connection
connect(this, &QTextBrowser::anchorClicked, this, &MTextEdit::urlActivated);
but the slot is never called.
I checked the produced html code and it has
<a href=...>
in it so any idea why the anchor is never activated?Thanks in advance
System:
Qt 5.6 on Windows with MSVC2013Background:
I'm using in my project a modified version of MRichTextEditor the full code for the class is here: header source (the only thing defined in Globals.h isdebugConsole
that translates intoqDebug()
andPBglobals::docsRepository()
which translates inQString PBglobals::docsRepository() { return QCoreApplication::applicationDirPath() + "/DocsRepo"; }
To test it just drop a file in the textedit and try opening it
-
Setting read only to false switches interaction flags from
Qt::TextBrowserInteraction
toQt::TextEditorInteraction
which disables interactive links.
To enable them back set theQt::LinksAccessibleByMouse
flag:setOpenLinks(false); setOpenExternalLinks(false); setReadOnly(false); setTextInteractionFlags(textInteractionFlags() | Qt::LinksAccessibleByMouse);