Important: Please read the Qt Code of Conduct -

Strange behaviour of QTextCursor.insertImage() within a loop

  • I build a table within a QTextDocument and try to insert pictures into each line of the table. Whereas the path of the pictures is printed into the cells of the table correctly, QT puts only the last image into the first cell. Why?

    Here some code:

    // cursor is of type QTextCursor
    // format is of type QTextCharFormat
    QTextTable *tabelle = cursor.insertTable(sp1.size(), 3);
    // sp1 is of type QStringList
                                for (int zdx = 0; zdx < sp1.size(); zdx++)
                                    cursor.insertText("some text");
                                    cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 1);
                                    cursor.insertFragment(QTextDocumentFragment::fromHtml("some text"));
                                    // actually "some text" comes from a variable, QString, 
                                    // with hard returns, return prompts the QTextCursor to change 
                                    // the cell in the table, thus 
                                    // cursor.insertFragment(QTextDocumentFragment::fromHtml())
                                    cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 1);
                                    QImage bild;
                                    bool geladen = bild.load(;
                                    // sp3 is of type QStringList and includes the path to the pictures
                                    if (geladen)
                                                      Qt::SmoothTransformation), "Text");
                                                              Qt::SmoothTransformation), "Tux arbeitet");
                                        // tuxwork of type QImage
                                    cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 1);

  • @fmoerl
    I would guess: you are re-using the same, single QImage bild; each time. Do you not need to new that? EDIT: Hmm, maybe not,

    Returns a scaled copy of the image.

  • Yes, the variable "bild" is created each iteration, but filled with a new picture each iteration.

  • Lifetime Qt Champion


    Something is not clear, what happens to the other cells ?

  • I try to build a table of 3 columns, the number of lines is variable, that's why i use the for-loop. The first column shows just one line of text, the second column gives more text with line breaks. The third column should show a picture. sp1, sp2 and sp3 are of type QStringList and include the information to show, sp3 includes the paths to the pictures.

    When doing it that way, only the last picture is shown in the third column of the first line. The text in the other cells works fine.

  • Lifetime Qt Champion

    So if you just put the path to the image in the last column rather than the image, what do you get ?

  • If you mean simply the text of the path, that's what i tested too and is given in my example above. The path (text) is written in each cell and differs in each cell.

  • Lifetime Qt Champion

    Can you provide a complete example that shows that behaviour ?

  • My example given above should include all information. The for-loop iterates the number of elements in sp1 (first column), sp2 (second column) and sp3 (third column, includes paths to images) the same size. Whereas I can fill some QStringList's with phrases, the path to images on your computer is unknown to me. When I can provide other information, please ask.

  • Here is an example of the output, a *.odf-file (in german) including a table. The path (text) is written correctly, the picture is in all lines the last one, the "Rollhocker".

    [0_1542625373820_2018-Sep.-20 14:37:30_Empfehlungen.odf](Uploading 100%)

  • I looked at QTextCursor's cource code. The problem is the same name you give the images "Text" - because of that they all are the same resource, that is, the last one...

  • Lifetime Qt Champion

    Sounds like a bug. Did you check the bug report system to see if there's something related ?

  • @mvuori : to my understanding, "Text" is optional, e.g. a subline, whereas the image is loaded above from different paths within sp3.

    @SGaist : thanks, I reported a new bug:

  • Until QTextCursor::InsertImage() is still buggy, I found a partial solution for filling the table with the right images. I replaced

    cursor.insertImage(bild.scaledToWidth(200, Qt::SmoothTransformation), "Text");


    QString htmlimage; // some text to provide html-code
    htmlimage.append("<img src=");
    htmlimage.append(; // sp3 including the path to the specific image
    htmlimage.append(" width=\"200\" height=\"150\">"); // no scaling, just setting to fix height and width
    // now insert an image via html-code

    note1: there seems to be no scaling via html, thus, the images may be distorted.

    note2: html-code don't like gaps. Beware of blanks within the path to the images.

Log in to reply