如何在QTextTable中居中文本

gor*_*don 3 c++ qt

我正在使用qt框架开发应用程序,现在我想将表格数据保存为pdf.我正在使用QTextTable和QTextDocument类.但是我无法将文本居中在单元格中.我该怎么办?

感谢帮助.

d11*_*d11 5

如果要在插入文本时进行对齐,可以使用alignment = Qt:AlignHCenter调用此函数.您可以修改该函数以指定字符格式.

// Insert text with specified alignment in specified cell
void insertAlignedText(QTextTable *table, int row, int col, Qt::Alignment alignment, QString text)
{
    // Obtain cursor and current block format
    QTextCursor textCursor = table->cellAt(row,col).firstCursorPosition();    
    QTextBlockFormat blockFormat = textCursor.blockFormat();

    // Read vertical part of current alignment flags
    Qt::Alignment vertAlign = blockFormat.alignment() & Qt::AlignVertical_Mask;

    // Mask out vertical part of specified alignment flags
    Qt::Alignment horzAlign = alignment & Qt::AlignHorizontal_Mask;

    // Combine current vertical and specified horizontal alignment
    Qt::Alignment combAlign = horzAlign | vertAlign;

    // Apply and write
    blockFormat.setAlignment(combAlign);    
    textCursor.setBlockFormat(blockFormat);
    textCursor.insertText(text);
}
Run Code Online (Sandbox Code Playgroud)