After reading the comments, I have successfully solved the problem. Now, I will organize the solution to this issue in the hope that it can help others.
The reason is the setBorderCollapse
function. By setting its parameter to false
, the table borders will be displayed.
According to the official documentation, this function is used to set the border collapse mode of the table. It determines how the table borders and cell borders are rendered and affects the display of the table. It is worth noting that this function prioritizes the cell border style (set using QTextTableCellFormat). Therefore, if the cells have individual border style settings, the cell borders will override the table borders. So, if we set it to true
, we need to set the cell border style. The following code provides an example:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// get the cursor position
QTextCursor cursor = ui->textEdit->textCursor();
QTextTableFormat tableFormat;
tableFormat.setBorderCollapse(true);
//insert the table
QTextTable *table = cursor.insertTable(3, 3, tableFormat);
//set the border style of a cell
for (int row = 0; row < 3; ++row) {
for (int col = 0; col < 3; ++col) {
QTextTableCell cell = table->cellAt(row, col);
QTextTableCellFormat cellFormat;
cellFormat.setBorder(1); // Cell border width
cellFormat.setBorderBrush(Qt::blue); // Cell border widthcolor
cellFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Solid); // Cell border style
cell.setFormat(cellFormat);
}
}
}
Once again, I would like to express my gratitude to everyone, especially to @musicamante.