Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to make a line break in a table?
Forum Updated to NodeBB v4.3 + New Features

How to make a line break in a table?

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 140 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AlexeyGolubev92
    wrote on last edited by
    #1

    Adding a table to QTextEdit. To do this, I use the QTextEdit ---> QTextCursor ---> QTextTable bundle. To format the QTextTableFormat table. To format a QTextTableCellFormat table cell. Then I create a report.odt document. I open the document and I don't see line breaks. As I understand it, the transfer works in QTextEdit and works in QTextDocument. But when the document is created, the transfer stops working. I need to move the text or just create a cell with two or more empty lines. I tried to add the character '\n' failed. Added '\n\r' failed.

    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);
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved