Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Image cannot occupy full cell in QTextTable if the cell is a result of cell merge. What can I do?



  • I created a QTextTable in QTextEdit. There are a few rows and columns in QTextTable and each column has a set width using QTextTableFormat. In one of the rows I merge all the cells and insert an image to that merged cell. The image will never occupy the full width of the merged cell, there is always a space between image to the right border. If I increase the image size, the right border of table moves too.

    I try to put the image in a non-merged cell, and the image will go wall to wall in that cell. So I guess the problem has something to do with cell merging...

    I save the QTextEdit as html and open that in a browser, the image is actually occupying full width of the merged cell.

    What can I do to get image to go wall to wall in a merged cell in QTextTable?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you provide a minimal sample code that reproduce this behaviour ?

    By the way, what size is the image ?



  • OK, please try this:

    First, save https://www.apple.com/ac/structured-data/images/knowledge_graph_logo.png to local (example below at c:/), then:

    QVector<QTextLength> columnWidth;
    for(int i = 0; i < 20; ++i)
        columnWidth.append(QTextLength(QTextLength::FixedLength, 50));
    
    QTextTableFormat *tableFormat = new QTextTableFormat();
    tableFormat->setColumnWidthConstraints(columnWidth);
    QTextTable* newTable=cursor.insertTable(1, 20, *tableFormat);
    
    newTable->mergeCells(0,1,1,7);
    newTable->mergeCells(0,12,1,4);
    
    QTextDocumentFragment fragment;
    fragment = QTextDocumentFragment::fromHtml("<img src='c:/knowledge_graph_logo.png'>");
    
    QTextCursor cursor2 = newTable->cellAt(0, 2).firstCursorPosition();
    cursor2.insertFragment(fragment);
    
    cursor2 = newTable->cellAt(0, 9).firstCursorPosition();
    cursor2.insertFragment(fragment);
    

    The first insert into a merged cell, there is always space to the right to the cell all. The second insert into a non-merged cell and image is wall to wall in cell. Please let me know if any other question. Thanks!


  • Lifetime Qt Champion

    Thanks but can you provide a more complete minimal sample ? i.e. here we are missing the QTextDocument
    setup, the widget used to show said text document etc.

    On a side note, there's no need to allocation tableFormat on the heap just do it on th stack.



  • How about this?

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QTextTableCell>
    #include <QTextEdit>
    #include <QTextCursor>
    #include <QTextDocumentFragment>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    

    main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        QTextEdit* textEdit = new QTextEdit(this);
        setCentralWidget(textEdit);
    
        QTextCursor cursor = textEdit->textCursor();
    
        QVector<QTextLength> columnWidth;
        for(int i = 0; i < 20; ++i)
            columnWidth.append(QTextLength(QTextLength::FixedLength, 50));
    
        QTextTableFormat *tableFormat = new QTextTableFormat();
        tableFormat->setColumnWidthConstraints(columnWidth);
        QTextTable* newTable=cursor.insertTable(1, 20, *tableFormat);
    
        newTable->mergeCells(0,1,1,7);
        newTable->mergeCells(0,12,1,4);
    
        QTextDocumentFragment fragment;
        fragment = QTextDocumentFragment::fromHtml("<img src='c:/knowledge_graph_logo.png'>");
    
        QTextCursor cursor2 = newTable->cellAt(0, 2).firstCursorPosition();
        cursor2.insertFragment(fragment);
    
        cursor2 = newTable->cellAt(0, 9).firstCursorPosition();
        cursor2.insertFragment(fragment);
    
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

  • Lifetime Qt Champion

    Thanks, it's likely a bug in the rendering of the html fragment. You should check the bug report system to see if there's anything related.


Log in to reply