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. Why my QTextTable aren't insert with sized correctly?
Forum Updated to NodeBB v4.3 + New Features

Why my QTextTable aren't insert with sized correctly?

Scheduled Pinned Locked Moved Solved General and Desktop
qtexteditrich textqtexttableinsert
4 Posts 2 Posters 556 Views 1 Watching
  • 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.
  • J Offline
    J Offline
    JackMyson
    wrote on 26 Nov 2022, 13:02 last edited by JackMyson
    #1

    I wrote a rich text program with some margin- and pagination-handling functions with QTextEdit. Now I'm dealing with some table problems. I know that I can insert a table by calling richTextEditor->textCursor().insertTable(int rows, int cols), and that's the problem.
    I have the following code, for inserting the table with a dialog:

    void DialogInsertTable::insertTable(/*int rows, int columns*/) {
        int row = rowsEdit->value();
        int column = columnsEdit->value();
    
        // ed is the subclassed QTextEdit
        QTextCursor cursor = ed->textCursor();
        QTextTable* table = cursor.insertTable(row, column);
        QTextTableFormat tableForm;
        tableForm.setWidth(QTextLength(QTextLength::Type::PercentageLength, 100));
        qreal v = (((ed->viewport()->width()) / column - (ed->marginLeft()) - (ed->marginRight())));
        qreal rawValue = tableForm.width().value(v);
        QList<QTextLength> list;
        for (int i=1; i<=column; i++) {
            list << QTextLength(QTextLength::Type::FixedLength, rawValue);
        }
        tableForm.setColumnWidthConstraints(list);
        tableForm.setBorderCollapse(true);
        table->setFormat(tableForm);
        
        close();
    }
    

    3 columns is the only perfect columns number for inserting this table. If I insert the table with 1 column or 2 columns or 4 or more, things go crazy.
    1.png
    Can somebody tells me why my table become smaller and smaller? I want something like this:
    2.png

    Environment

    • Operating System: macOS Big Sur 11.7.1;
    • Model: MacBook Air (11-inch, Mid 2013) running 64-bit Intel processor x86_64;
    • Qt version: 6.3.2 (Clang 13.0);
    • Qt Creator version: 8.0.2;

    Any help will be appreciate! Thanks!!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 26 Nov 2022, 15:26 last edited by Chris Kawa
      #2

      You've got your column width calculation wrong. You're multiplying margins for each column.
      It should rather be

      qreal v = (ed->viewport()->width() - ed->marginLeft() - ed->marginRight()) / columns;
      

      and make sure to center your table via tableFormat.setAlignment(Qt::AlignCenter);.

      Btw. QList has a constructor that can fill it with multiple copies of the same value, so you can avoid the for loop like this:

      QList<QTextLength> list {columns, QTextLength{QTextLength::FixedLength, rawValue}};
      
      1 Reply Last reply
      1
      • J Offline
        J Offline
        JackMyson
        wrote on 27 Nov 2022, 00:40 last edited by JackMyson
        #3

        Hello,

        With your code,

        void DialogInsertTable::insertTable(/*int rows, int columns*/) {
            int row = rowsEdit->value();
            int column = columnsEdit->value();
        
            QTextCursor cursor = ed->textCursor();
            QTextTable* table = cursor.insertTable(row, column);
            QTextTableFormat tableForm;
            tableForm.setWidth(QTextLength(QTextLength::Type::PercentageLength, 100));
            qreal v = (ed->viewport()->width() - ed->marginLeft() - ed->marginRight()) / column;
            qreal rawValue = tableForm.width().value(v);
            QList<QTextLength> list {column, QTextLength{QTextLength::FixedLength, rawValue}};
            tableForm.setAlignment(Qt::AlignCenter);
            tableForm.setColumnWidthConstraints(list);
            tableForm.setBorderCollapse(true);
            table->setFormat(tableForm);
        
            close();
        }
        

        It's still unsolved. The table is still exceed the margin.
        Screenshot 2022-11-27 at 8.35.15 AM.png
        How can I implement a QTextTable fitting the margin?
        2.png

        1 Reply Last reply
        0
        • J Offline
          J Offline
          JackMyson
          wrote on 27 Nov 2022, 08:30 last edited by
          #4

          Oh.. I figured it out.
          That's just a simple unit conversion error.

          I used millimeter instead of pixel...

          >>> # Python
          >>> __editor.mmMarginLeft()
          20
          >>> __editor.pxMarginLeft()
          110
          >>> __editor.mmMarginLeft() == __editor.pxMarginLeft()
          False
          

          Now it's solved! THANKS a lot!
          Screenshot 2022-11-27 at 4.29.45 PM.png

          1 Reply Last reply
          1

          3/4

          27 Nov 2022, 00:40

          • Login

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