Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Image cannot occupy full cell in QTextTable if the cell is a result of cell merge. What can I do?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 1.3k 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.
  • P Offline
    P Offline
    Pauly
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

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

      By the way, what size is the image ?

      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
      • P Offline
        P Offline
        Pauly
        wrote on last edited by Pauly
        #3

        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!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          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
          • P Offline
            P Offline
            Pauly
            wrote on last edited by
            #5

            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;
            }
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              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

              • Login

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