我正在使用 qt 框架开发应用程序,现在我想将表格数据保存为 pdf。我正在使用 QTextTable 和 QTextDocument 类。但是我无法将单元格中的文本居中。我该怎么做呢?
感谢帮助。
如果您想在插入文本时进行对齐,可以使用 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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)