1

我创建了 QTextTable:

QTextDocument *document=new QTextDocument(this);
QTextCursor cursor(document);
cursor.movePosition(QTextCursor::Start);

QTextTableCellFormat cellFormat;
cellFormat.setLeftPadding(7);
cellFormat.setRightPadding(7);

QBrush blackBrush(Qt::SolidPattern);
QTextTableFormat tableFormat;
tableFormat.setAlignment(Qt::AlignCenter);
tableFormat.setBorderBrush(blackBrush);
tableFormat.setBorder(0.5);
tableFormat.setCellSpacing(0);
tableFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Solid);
tableFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100));
QTextTable *table = cursor.insertTable(10,10,tableFormat);

QTextBlockFormat centerAlignment;
centerAlignment.setAlignment(Qt::AlignCenter);

table->mergeCells(0,0,10,5);
cursor = table->cellAt(0, 0).firstCursorPosition();
cursor.setBlockFormat(centerAlignment);
cursor.insertText("text");

我想在单元格的中间垂直和水平地写文本但是我的文本在水平的中间但它不在垂直的中间。Qt::AlignVCenter 和 Qt::AlignBottom 也不起作用。我试过这个答案https://stackoverflow.com/a/10329809/1997790但它不起作用。

4

1 回答 1

1

您可以尝试使用 QTextCharFormat::setVerticalAlignment(),如下所示:

QTextTableCell   cell = table->cellAt(0, 0);
QTextCharFormat  cellFormat = cell.format();

cellFormat.setVerticalAlignment(QTextCharFormat::AlignMiddle);
cell.setFormat(cellFormat);

如果这不起作用,您可能想尝试使用样式表来自定义表格。

于 2013-02-05T21:05:44.413 回答