我正在尝试使用QTextTable在QT中创建一个表。我设置背景和文本时第一行工作正常,但第二行仅应用格式化,但insertText函数似乎对我不起作用。我也尝试过insertHTML,但在第2行似乎没什么用。
QTextTableFormat channelBankFormat;
channelBankFormat.setAlignment(Qt::AlignHCenter);
//channelBankFormat.setHeight(8);
channelBankFormat.setColumnWidthConstraints(constraints);
channelBankFormat.setBorder(0);
ChannelBank->clear();
QTextCursor cursor = ChannelBank->textCursor();
cursor.beginEditBlock();
QTextTable *table = cursor.insertTable(2, 5, channelBankFormat);
QTextCharFormat headerFormat = cursor.charFormat();
headerFormat.setFontWeight(QFont::Bold);
headerFormat.setFontPointSize(8);
QTextCharFormat dataFormat = cursor.charFormat();
dataFormat.setFontPointSize(12);
QTextBlockFormat centerAlignment;
centerAlignment.setAlignment(Qt::AlignCenter);
for (int cellNumber = 1; cellNumber <= 5; ++cellNumber) {
QTextTableCell cell = table->cellAt(0, cellNumber-1);
QTextCharFormat headerCellFormat = cell.format();
QTextCursor cellCursor = cell.firstCursorPosition();
headerCellFormat.setBackground(QColor(0 + (cellNumber * 50), 222, 222, 127));
cell.setFormat(headerCellFormat);
cellCursor.clearSelection();
cellCursor.insertText("---",headerFormat);
}
for (int cellNumber = 1; cellNumber <= 5; ++cellNumber) {
QTextTableCell cell = table->cellAt(1, cellNumber-1);
QTextCharFormat headerCellFormat = cell.format();
QTextCursor cellCursor = cell.lastCursorPosition();
headerCellFormat.setBackground(QColor(0 + (cellNumber * 50), 222, 222, 127));
cell.setFormat(headerCellFormat);
cellCursor.clearSelection();
cellCursor.insertText("---",headerFormat);
}
cursor.endEditBlock();
答案 0 :(得分:0)
找到了罪魁祸首: channelBankFormat.setHeight(8);