本文介绍了如何中心文本在QTextTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I’m developing applications with qt framework and right now I want to save my tabular data to pdf. I'm using QTextTable and QTextDocument classes. However I can’t center text in cells. How should I do it?

感谢您的帮助。

推荐答案

如果要在插入文本时进行对齐,可以使用alignment = Qt:AlignHCenter调用此函数。

If you want to do the alignment as you are inserting the text, you can call this function with alignment = Qt:AlignHCenter. You can modify the function to also specify the character formatting.

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

这篇关于如何中心文本在QTextTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 12:30