Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Strange behaviour of QTextCursor.insertImage() within a loop
Forum Update on Monday, May 27th 2025

Strange behaviour of QTextCursor.insertImage() within a loop

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
14 Posts 4 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fmoerl
    wrote on 13 Nov 2018, 10:47 last edited by
    #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);
                                }
    
    J 1 Reply Last reply 13 Nov 2018, 13:58
    0
    • F fmoerl
      13 Nov 2018, 10:47

      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);
                                  }
      
      J Offline
      J Offline
      JonB
      wrote on 13 Nov 2018, 13:58 last edited by JonB
      #2

      @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, http://doc.qt.io/qt-5/qimage.html#scaledToWidth

      Returns a scaled copy of the image.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fmoerl
        wrote on 13 Nov 2018, 15:30 last edited by
        #3

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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 13 Nov 2018, 22:18 last edited by
          #4

          Hi,

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fmoerl
            wrote on 14 Nov 2018, 09:09 last edited by fmoerl
            #5

            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.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 14 Nov 2018, 22:22 last edited by
              #6

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • F Offline
                F Offline
                fmoerl
                wrote on 16 Nov 2018, 07:59 last edited by
                #7

                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.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 17 Nov 2018, 22:09 last edited by
                  #8

                  Can you provide a complete example that shows that behaviour ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • F Offline
                    F Offline
                    fmoerl
                    wrote on 19 Nov 2018, 10:42 last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      fmoerl
                      wrote on 19 Nov 2018, 11:06 last edited by
                      #10

                      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%)

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mvuori
                        wrote on 19 Nov 2018, 19:13 last edited by
                        #11

                        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...

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 19 Nov 2018, 21:02 last edited by
                          #12

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

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • F Offline
                            F Offline
                            fmoerl
                            wrote on 20 Nov 2018, 10:01 last edited by
                            #13

                            @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

                            1 Reply Last reply
                            0
                            • F Offline
                              F Offline
                              fmoerl
                              wrote on 4 Dec 2018, 12:25 last edited by fmoerl 12 Apr 2018, 13:57
                              #14

                              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.

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved