Why my QTextTable aren't insert with sized correctly?
-
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.
Can somebody tells me why my table become smaller and smaller? I want something like this:
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!!
-
You've got your column width calculation wrong. You're multiplying margins for each column.
It should rather beqreal 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}};
-
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.
How can I implement a QTextTable fitting the margin?
-
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!