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 MSVC2013

    Background:
    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 is debugConsole that translates into qDebug() and PBglobals::docsRepository() which translates in

    QString PBglobals::docsRepository()
    {
        return QCoreApplication::applicationDirPath() + "/DocsRepo";
    }
    

    To test it just drop a file in the textedit and try opening it


  • Moderators

    Setting read only to false switches interaction flags from Qt::TextBrowserInteraction to Qt::TextEditorInteraction which disables interactive links.
    To enable them back set the Qt::LinksAccessibleByMouse flag:

    setOpenLinks(false);
    setOpenExternalLinks(false);
    setReadOnly(false);
    setTextInteractionFlags(textInteractionFlags() | Qt::LinksAccessibleByMouse);
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.