如何在qtextttable中更改行高度

How to change row height in QTextTable

本文关键字:高度 qtextttable      更新时间:2023-10-16

我正在编写复杂的富文本编辑器,源自QTextEdit类。它必须能够插入、调整大小,并将各种格式应用于嵌入的表。

我找到了设置列宽度的函数(setColumnWidthConstraints)。但是没有人去change _rows_ heights

有办法做到这一点吗?

示例代码:

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);
}

似乎可以使用setHTML(QString)或insertHTML(QString)函数来插入样式表。

当对样式表使用此函数时,样式表只会应用于文档中的当前块。为了应用一个样式在整个文档中使用QTextDocument::setDefaultStyleSheet()相反。

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

除了使用垫片....根据http://harmattan-dev.nokia.com/docs/platform-api-reference/xml/daily-docs/libqt4/richtext-html-subset.html可以设置字体声明

Qt似乎已经针对CSS2.1规范,如下所示:http://www.w3.org/TR/CSS2/fonts.html propdef-font

您是否尝试在表行中指定字体。

使用insertHTML传递以下字符串,其中该字符串被声明为QString

<style>
table > tr {font-size: normal normal 400 12px/24px serif;}
</style>

如果你只是想使行比他们的文本高度需要,你可以尝试插入一个0xN透明图像在行的第一个单元格(或1xN如果Qt不让你做零宽度)。

也可能设置表格单元格的顶部padding with qtextttablecellformat::setTopPadding()或QTextBlockFormat::setTopMargin()设置顶部margin。但是据我所知,内边距和边距都是添加到文本布局高度的,所以它们都不适合设置绝对高度。

你看过Calligra吗?它的libs/kotext和libs/textlayout库实现了自定义QAbstractTextDocumentLayout,具有比QTextEdit更丰富的表支持。

使用this->document()->setDefaultStyleSheet("css goes here");插入样式表

看到http://qt project.org/doc/qt - 5.0 -/- qtwidgets/qtextedit.html # document-prop和http://qt——project.org/doc/qt - 5.0 -/- qtgui/qtextdocument.html # defaultStyleSheet-prop

(链接到Qt5文档,但这些功能在Qt4也可用)