31

I'm writing the complicated rich text editor, derived from QTextEdit class. It must be able to insert, resize, and apply various formatting to embedded tables.

I found function for setup column widths (setColumnWidthConstraints). But there is no one to change _rows_ heights.

Is there any way to achieve this?

Example code:

void CustomTextEdit::insertTable (int rows_cnt, int columns_cnt)
{
    QTextCursor cursor = textCursor ();
    QTextTableFormat table_format;
    table_format.setCellPadding (5);

    // TODO: This call just changed the frame border height, not table itself.
    //table_format.setHeight (50);

    // Setup columns widths - all is working perfectly.
    QVector <QTextLength> col_widths;
    for (int i = 0; i < columns_cnt; ++i)
        col_widths << QTextLength (QTextLength::PercentageLength, 100.0 / columns_cnt);
    table_format.setColumnWidthConstraints (col_widths);

    // ...But there is no similar function as setRowHeighConstraints for rows!

    // Insert our table with specified format settings
    cursor.insertTable (rows_cnt, columns_cnt, table_format);
}
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
eraxillan
  • 1,552
  • 1
  • 19
  • 40
  • you could use QTextFrameFormat::setHeight(qreal height) – Cool_Coder Mar 20 '13 at 10:32
  • 1
    @Cool_Coder This just changed the height of _all_ the frame (i.e. where border will be shown). But i need to specify height for any separate row of the table. – eraxillan Mar 20 '13 at 11:24
  • can please show some code so that I can comment on that? – Cool_Coder Mar 20 '13 at 11:31
  • @Cool_Coder Sorry, i just forget it. The OP post was updated. – eraxillan Mar 20 '13 at 11:40
  • 5
    I've spent a bit of time digging the docs for this, there isn't any methods, or inherited methods to force the height. The best idea I can come up with is to force the `setCellPadding` to a large number to achieve the height, then apply `setColumnWidthConstraints` to bring the width back in. – Nicholas Smith Mar 22 '13 at 11:30
  • 2
    @NicholasSmith Thanks for your time. So, you think there is no _clear_ way to achieve what i want? – eraxillan Mar 22 '13 at 12:02
  • @Axilles: unfortunately not, it's an odd situation as it doesn't seem to inherit anything that does height setups, nor does it expose any functionality for it (as far as I can see, I scanned the source very quickly), but it must be calculating a height per row to know the area. – Nicholas Smith Mar 22 '13 at 12:07
  • 6
    @NicholasSmith Sad news :/ So, my 50 points of reputation are lost in useless way. But sad truth is better than nice lie. – eraxillan Mar 22 '13 at 13:28
  • I think they'll be readded to your total if it's not solved within 7 days. I'll have another look at the source again and see if I missed something, but I don't think I did. – Nicholas Smith Mar 22 '13 at 13:50
  • @TildalWave First of all, thanks for upvoting :) Bounty is very expensive thing for one's reputation. About subject - ok, i'm very patient man. And curious too - if Digia will "forgot" about bug/feature-request you specified, i will try to implement this myself. If my editor will not realizes itself to that moment :) – eraxillan Mar 30 '13 at 15:02
  • The [bug report](https://bugreports.qt-project.org/browse/QTBUG-3581) that @TildalWave spoke about has its status as _closed_ and resolution as _out of scope_. And considering it has been last updated back in November 2009, when Qt still belonged to Nokia, it's very likely that it has long been forgotten about… There aren't even any comments attached to the report. Weird. – Goksel Goktas May 27 '13 at 15:20
  • @GokselGoktas I lose the hope a long time ago. Now my company just switched to use another editor. – eraxillan May 28 '13 at 09:15

3 Answers3

1

it seems that you can use the setHTML(QString) or insertHTML(QString) functions to insert a stylesheet.

When using this function with a style sheet, the style sheet will only apply to the current block in the document. In order to apply a style sheet throughout a document, use QTextDocument::setDefaultStyleSheet() instead.

ref: http://harmattan-dev.nokia.com/docs/platform-api-reference/xml/daily-docs/libqt4/qtextedit.html#insertHtml

appart from using shims....according to http://harmattan-dev.nokia.com/docs/platform-api-reference/xml/daily-docs/libqt4/richtext-html-subset.html you can set the font declaration.

Qt seems to have targeted the CSS2.1 specification, which is as followed.. http://www.w3.org/TR/CSS2/fonts.html#propdef-font

have you tried specifying the font within the table row.

pass the following string using insertHTML, where this string is delcared as a QString

<style>
table > tr {font-size: normal normal 400 12px/24px serif;}
</style>
Brett Caswell
  • 1,486
  • 1
  • 13
  • 25
1

If you just want to make rows taller than their text height would require, you could try inserting a 0xN transparent image in the first cell of the row (or 1xN if Qt won't let you do zero-width).

It might also be possible to set the table cell's top padding with QTextTableCellFormat::setTopPadding() or maybe set the top margin with QTextBlockFormat::setTopMargin(). But both padding and margins are added to the text layout height AFAIK, so neither of them is very good for setting an absolute height.

Have you looked at Calligra? Its libs/kotext and libs/textlayout libraries implement a custom QAbstractTextDocumentLayout with much richer table support than QTextEdit.

bks
  • 1,360
  • 6
  • 7
0

Insert a stylesheet using this->document()->setDefaultStyleSheet("css goes here");

See http://qt-project.org/doc/qt-5.0/qtwidgets/qtextedit.html#document-prop and http://qt-project.org/doc/qt-5.0/qtgui/qtextdocument.html#defaultStyleSheet-prop

(links go to Qt5 docs, but these functions are available in Qt4 also.)

waddlesplash
  • 743
  • 1
  • 6
  • 25