c++ - 如何在 QTextTable 中居中文本

标签 c++ qt

我正在使用 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);
}

关于c++ - 如何在 QTextTable 中居中文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5187484/

相关文章:

c++ - 对范围和堆内存生命周期感到困惑

c++ - 在 C++ 中防止整数 0 隐式转换为指针的最佳方法是什么

c++ - 保证复制省略如何工作?

c++ - 初始化错误

c++ - 使用带有 QStringList 指针的运算符<<

c++ - 将 GPIB 与 Qt 连接

c++ - 函数在 cpp 文件中时未声明的标识符

c++ - 如何在 Qt 中设计滑动/滑动功能来选择小部件?

c++ - QT - 现有插槽在 mainwidow.cpp 中不可见

c++ - 如何将Qt中的组合框元素与具有相同索引的静态 vector 中的预定义元素链接?