Important: Please read the Qt Code of Conduct -

QTextDocument Anchors and links not working?

  • I'm having problems getting links and anchors to work within a QTextdocument.

    Qt 5.12.3 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 5.3.1 20160406 (Red Hat 5.3.1-6)) on "xcb"
    OS: Linux Mint 19.1 [linux version 4.15.0-51-generic]
    Qt Creator 4.9.1
    Based on Qt 5.12.3 (GCC 5.3.1 20160406 (Red Hat 5.3.1-6), 64 bit)
    From revision 168e91b618
    Built on May 27 2019 00:17:25

    I use the KDE desktop on Mint:
    plasmashell 5.12.7
    Qt: 5.9.5
    KDE Frameworks: 5.47.0
    kf5-config: 1.0

    For fun, I'm writing an application to build an epub from a collection of simple markdown files (each containing a poem).

    The book works as an epub, with a word cloud for the cover page, a table of contents, a poem on each page and an index at the end. The table of content and index include page numbers, but also should link to the poem using a link pointing to an anchor.

    The problem I have is that I also generate a pdf and odt version of the book and the links and anchors do not work within these.

    Initially I took all the html files that the epub was built from and created the QTextdocument from those and that gave me the same result.

    Here are the qDebug outputs for the strings to be added to the book, that are added as html:

    Anchors or targets for links (I tried both versions):

    • Poem: "\n\n\n\n\t<a name="poem1"></a>A Child"
    • Poem: "\n\n\n\n\t<a name="poem1">A Child</a>"

    Links to Anchors in the same document

    • TOC: "<a href="#poem1">A Child</a>"
    • Index: "<a href="#poem1">7</a>"

    The code that adds the anchor to the poem title on each page:

    if(lines == 1) {  // Lines that are the title
                        QString anchor("\n\n\n\n\t<a name=\"poem" + QString("%1").arg(index) + "\">" + line + "</a>");
                        qDebug() << "Poem: " << anchor;
                    } else { // Standard lines
                        myCursor->insertText("\t" + line);


    The code that adds the index entry:

    for(auto word : m_word_poem.keys()) {
                QStringList files(m_word_poem.value(word));
                int entries(0);
                foreach(QString file, files) {
                    int poem_number = m_poem_page[m_location_poem[file]];
                    int page = increase + poem_number;
                    if(entries > 0) {
                        list += ", ";
                    list += "<a href=\"#poem" + QString("%1").arg(poem_number) + "\">" + QString("%1").arg(page) + "</a>";
                qDebug() << "Index: " << list;

    I have look at the QTextDocument properties and can't see anything I need to change. I can't see what I am doing wrong, can anyone make any suggestions, any help would be appreciated.

  • Moderators

    what widget are you using the QTextDocument with?
    QTextBrowser? See it's anchorClicked signal

  • I'm not viewing any of the created documents within the application, it's a QDialog, that lets me select paths and name the book but the QTextDocument is just the container for the documents whilst they are created. The links need to work within the PDF and the ODT file in their environments.

Log in to reply