Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-73204

QTextTableFormat returns wrong width

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P2: Important P2: Important
    • None
    • 5.11.0, 5.12.0, 5.14.0 Alpha
    • GUI: Text handling
    • None

      I insert a table in a QTextDocument, and set a width for that table via a QTextTableFormat instance. Then I insert an image inside a cell of the table, and if the image is larger than the width of the table, the latter gets enlarged to contain the image.

      The problem is that when I ask the table its width - calling table->format().width().rawValue() - the result is the width I set initially.

      I include the minimum code to replicate this issue:

      #include <QApplication>
      #include <QTextEdit>
      #include <QTextTable>
      #include <QPainter>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          QTextEdit textEdit;
          QTextCursor c = textEdit.textCursor();
      
          QTextTableFormat tableFormat;
          tableFormat.setWidth(300); // set table width to 300 pixels
          QTextTable *table1 = c.insertTable(2, 3, tableFormat); // insert reference table
      
          c.setPosition(textEdit.document()->lastBlock().position());
          c.insertText(QString("\nTable 1 width: %1\n\n").arg(table1->format().width().rawValue()));
      
          QTextTable *table2 = c.insertTable(2, 3, tableFormat); // insert table that will contain the image
      
          QPixmap pixmap(400,200); // create a 400 pixels wide image
          QPainter p(&pixmap);
          p.fillRect(pixmap.rect(), Qt::green);
      
          textEdit.document()->addResource(QTextDocument::ImageResource, QUrl("image"), pixmap);
          QTextImageFormat imageFormat;
          imageFormat.setName("image");
          c.setPosition(table2->cellAt(0,1).firstPosition());
          c.insertImage(imageFormat); // insert image into a cell
      
          c.setPosition(textEdit.document()->lastBlock().position());
          c.insertText(QString("\nTable 2 width (after image insertion): %1").arg(table2->format().width().rawValue()));
      
          textEdit.show();
      
          return a.exec();
      }
      

      Executing the code above clearly shows that there is a difference between the effective width of the table in the document, and the width retrievable in the source code.

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            esabraha Eskil Abrahamsen Blomfeldt
            pe_albe Alberto Pellizzon
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:

                There are no open Gerrit changes