Strange behaviour of QTextCursor.insertImage() within a loop
-
wrote on 13 Nov 2018, 10:47 last edited by
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++) { //format.setFontPointSize(8); cursor.setCharFormat(format); cursor.insertText("some text"); cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 1); // format.setFontPointSize(8); cursor.setCharFormat(format); 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); // format.setFontPointSize(8); cursor.setCharFormat(format); QImage bild; bool geladen = bild.load(sp3.at(zdx)); // sp3 is of type QStringList and includes the path to the pictures if (geladen) { cursor.insertImage(bild.scaledToWidth(200, Qt::SmoothTransformation), "Text"); cursor.insertFragment(QTextDocumentFragment::fromHtml("<br>")); cursor.insertText(sp3.at(zdx)); } else { cursor.insertImage(tuxwork.scaledToWidth(200, Qt::SmoothTransformation), "Tux arbeitet"); // tuxwork of type QImage } cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 1); }
-
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++) { //format.setFontPointSize(8); cursor.setCharFormat(format); cursor.insertText("some text"); cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 1); // format.setFontPointSize(8); cursor.setCharFormat(format); 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); // format.setFontPointSize(8); cursor.setCharFormat(format); QImage bild; bool geladen = bild.load(sp3.at(zdx)); // sp3 is of type QStringList and includes the path to the pictures if (geladen) { cursor.insertImage(bild.scaledToWidth(200, Qt::SmoothTransformation), "Text"); cursor.insertFragment(QTextDocumentFragment::fromHtml("<br>")); cursor.insertText(sp3.at(zdx)); } else { cursor.insertImage(tuxwork.scaledToWidth(200, Qt::SmoothTransformation), "Tux arbeitet"); // tuxwork of type QImage } cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 1); }
wrote on 13 Nov 2018, 13:58 last edited by JonB@fmoerl
I would guess: you are re-using the same, singleQImage bild;
each time. Do you not need tonew
that? EDIT: Hmm, maybe not, http://doc.qt.io/qt-5/qimage.html#scaledToWidthReturns a scaled copy of the image.
-
wrote on 13 Nov 2018, 15:30 last edited by
Yes, the variable "bild" is created each iteration, but filled with a new picture each iteration.
-
Hi,
Something is not clear, what happens to the other cells ?
-
wrote on 14 Nov 2018, 09:09 last edited by fmoerl
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.
-
So if you just put the path to the image in the last column rather than the image, what do you get ?
-
wrote on 16 Nov 2018, 07:59 last edited by
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.
-
Can you provide a complete example that shows that behaviour ?
-
wrote on 19 Nov 2018, 10:42 last edited by
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.
-
wrote on 19 Nov 2018, 11:06 last edited by
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%)
-
wrote on 19 Nov 2018, 19:13 last edited by
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...
-
Sounds like a bug. Did you check the bug report system to see if there's something related ?
-
wrote on 20 Nov 2018, 10:01 last edited by
@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: https://bugreports.qt.io/browse/QTBUG-71901?filter=-2
-
wrote on 4 Dec 2018, 12:25 last edited by fmoerl 12 Apr 2018, 13:57
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");
by
QString htmlimage; // some text to provide html-code htmlimage.clear(); htmlimage.append("<img src="); htmlimage.append(sp3.at(zdx)); // 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 cursor.insertFragment(QTextDocumentFragment::fromHtml(htmlimage));
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.