LINUX.ORG.RU

Как сделать перенос строки в таблице?

 


0

1

Добавляю таблицу в QTextEdit. Для этого использую связку QTextEdit —> QTextCursor —> QTextTable. Для форматирования таблицы QTextTableFormat. Для форматрования ячейки таблицы QTextTableCellFormat. После создаю документ report.odt. Открываю документ и не вижу переноса строк. Как понимаю перенос работает в QTextEdit и работает в QTextDocument. Но когда создается документ перенос перестает работать. Мне нужно перенести текст или просто создать ячейку с двумя или более пустыми строками. Попытался добавить символ ‘\n’ не получилось. Добавил ‘\n\r’ не получилось.

sourceTextEdit = new QTextEdit();
    editWidth=sourceTextEdit->width(); // ширина листа

    cursor = sourceTextEdit->textCursor();   // берем курсор
    cursor.movePosition(QTextCursor::Start); // устанавливаем курсор в начальное положение
    
    
    float textColumnFactor = 0.02;
    QVector<QTextLength> columnWidthConstraints;
    const QList<int> width
                         {
                         (int)(editWidth * textColumnFactor),
                         (int)(editWidth * textColumnFactor)
                         };
    for(const auto & w : width)
        columnWidthConstraints.append(QTextLength(QTextLength::FixedLength, w));

    cursor = sourceTextEdit->textCursor();

    tableFormat.setColumnWidthConstraints(columnWidthConstraints);
    tableFormat.setAlignment(Qt::AlignCenter);
    tableFormat.setBorderStyle(QTextTableFormat::BorderStyle_Solid);
    tableFormat.setBorderBrush(QBrush(Qt::SolidPattern));
    tableFormat.setBackground(Qt::white);
    tableFormat.setBorderCollapse(true);
    tableFormat.setCellPadding(0);
    tableFormat.setCellSpacing(0);
    tableFormat.setBorder(1);

    cellFormat.setTopPadding(1);
    cellFormat.setBottomPadding(1);

    orderTable = cursor.insertTable(3, 2, tableFormat);
    orderTable->setFormat(tableFormat);

    adjustCell(0, 0, textColumnFactor/20, Qt::AlignLeft, "Модель", "TimesNewRoman", 12, QFont::Normal);
    adjustCell(1, 0, textColumnFactor/20, Qt::AlignLeft, "Серийный номер", "TimesNewRoman", 12, QFont::Normal);
    adjustCell(2, 0, textColumnFactor/20, Qt::AlignLeft, "Протокол", "TimesNewRoman", 12, QFont::Normal);

    adjustCell(0, 1, textColumnFactor/20, Qt::AlignLeft, " \n\r ", "TimesNewRoman", 12, QFont::Normal); // хочу добавить перенос строки в это место чтобы в ячейки было две строки
    adjustCell(1, 1, textColumnFactor/20, Qt::AlignLeft, " \n\r ", "TimesNewRoman", 12, QFont::Normal); // хочу добавить перенос строки в это место чтобы в ячейки было две строки
    adjustCell(2, 1, textColumnFactor/20, Qt::AlignLeft, " \n\r ", "TimesNewRoman", 12, QFont::Normal); // хочу добавить перенос строки в это место чтобы в ячейки было две строки
    
    
  void Report::adjustCell(const int row, const int col, float columFactor, Qt::Alignment 
  aalignment, QString text, QString fontName, qreal size, int fontWeight)
  {
    QFont font;
    font.setFamily(fontName);
    font.setPointSizeF(size);
    QTextCharFormat boldFormat;
    boldFormat.setFont(font);
    boldFormat.setFontWeight(fontWeight);

    auto cell = orderTable->cellAt(row, col);
    cellFormat.setLeftPadding(editWidth * columFactor);
    cell.setFormat(cellFormat);
    cell.firstCursorPosition().insertText(text, boldFormat);
    cursor=cell.firstCursorPosition();
    cellAlign.setAlignment(aalignment);
    cursor.setBlockFormat(cellAlign);
    }
    /*            создание документа          */
   QPrinter printer(QPrinter::ScreenResolution);
   printer.setPaperSize(QPrinter::A4);
   QSizeF paperSize;
   paperSize.setWidth(printer.width());
   paperSize.setHeight(printer.height());
   textDocument = sourceTextEdit->document(); // создаем документ
   textDocument->setPageSize(paperSize);
   writer.setFormat("odf");
   writer.setFileName("/media/usb0/report.odt");
   writer.write(textDocument);

Попробуйте о вставить QTextBlockFormat в QTextCursor, установить его lineHeight с помощью setLineHeight(), и вставить текст с помощью insertText():

cellCursor = orderTable->cellAt(0, 1).firstCursorPosition();
blockFormat = cellCursor.blockFormat();
blockFormat.setLineHeight(50, QTextBlockFormat::FixedHeight);
cellCursor.setBlockFormat(blockFormat);
cellCursor.insertText("Текст 1\nТекст 2");
versetty777
()