void SaveForm::saveAs(const QString &file){ int rowsCount = ui->logView->model()->rowCount(); QTextDocument doc; QTextCursor cursor(&doc); QTextBlockFormat blockFormat; blockFormat.setAlignment(Qt::AlignHCenter); cursor.insertBlock(blockFormat); cursor.insertText("Архив " + ui->startDateTimeEdit->dateTime().toString("dd.MM.yyyy") + " по " +ui->finishDateTimeEdit->dateTime().toString("dd.MM.yyyy") + "\n"); cursor.movePosition(QTextCursor::End); //Формируем таблицу QTextTable *table; if (rowsCount != 0) { QVector<QTextLength> columnLength; columnLength.append(QTextLength(QTextLength::PercentageLength, 1)); columnLength.append(QTextLength()); columnLength.append(QTextLength()); columnLength.append(QTextLength()); columnLength.append(QTextLength(QTextLength::PercentageLength, 10)); columnLength.append(QTextLength()); QTextTableFormat tableFormat; tableFormat.setColumnWidthConstraints(columnLength); tableFormat.setBorder(1); table = cursor.insertTable(rowsCount+1, 6, tableFormat); //Формируем заголовок QStringList headers; headers << "Заголовок1" << " Заголовок2" << " Заголовок3" << " Заголовок4" << " Заголовок5" << " Заголовок6"; for (int i = 0; i<6; ++i) { QTextCursor cellCursor = table->cellAt(0, i).firstCursorPosition(); cellCursor.setBlockFormat(blockFormat); cellCursor.insertText(headers[i]); } //Поля for (int row = 0; row < rowsCount; ++row) { QTextCursor counterCellCursor = table->cellAt(row+1, 0).firstCursorPosition(); counterCellCursor.setBlockFormat(blockFormat); counterCellCursor.insertText(QString().number(row+1)); for (int col = 1; col < 6; ++col) { QVariant cellData = getCellData(row, col-1); qDebug() << cellData.toString(); QTextCursor cellCursor = table->cellAt(row+1, col).firstCursorPosition(); cellCursor.setBlockFormat(blockFormat); if (col != 1) cellCursor.insertText(cellData.toString()); else cellCursor.insertText(cellData.toDateTime().toString("h:mm dd.MM.yyyy")); } } } else { //Если данных нет table = cursor.insertTable(1, 1); QTextCursor cellCursor = table->cellAt(0, 0).firstCursorPosition(); cellCursor.insertText(tr("Нет данных, проверьте условия отсева")); } cursor.movePosition(QTextCursor::End); blockFormat.setAlignment(Qt::AlignLeft); cursor.insertBlock(blockFormat); QString tmpOperator = (currentName.isEmpty())?"--":currentName; cursor.insertText("Дата время выборки: " + QDateTime::currentDateTime().toString("h:mm dd.MM.yyyy") + "\n" + "Прораб: " + tmpOperator); QTextDocumentWriter writer(file, "odf"); writer.write(&doc);}
C++ (Qt)#include <QApplication>#include <QTextEdit>#include <QTextDocument>#include <QTextCursor>#include <QVector>#include <QTextLength>#include <QTextTableFormat> int main(int argc, char *argv[]){ QApplication app(argc, argv); QTextEdit tx; QTextDocument doc; tx.setDocument(&doc); QTextCursor cursor(&doc); QVector<QTextLength> tl; tl.append(QTextLength(QTextLength::PercentageLength, 10)); tl.append(QTextLength(QTextLength::PercentageLength, 20)); tl.append(QTextLength(QTextLength::PercentageLength, 40)); tl.append(QTextLength(QTextLength::PercentageLength, 30)); QTextTableFormat fmt; fmt.setColumnWidthConstraints(tl); cursor.insertTable(3, 4, fmt); tx.show(); return app.exec();}
QTextTable *table; QVector<QTextLength> columnLength; QTextTableFormat tableFormat; columnLength.append(QTextLength(QTextLength::PercentageLength, 1)); columnLength.append(QTextLength(QTextLength::PercentageLength, 5)); columnLength.append(QTextLength(QTextLength::PercentageLength, 10)); columnLength.append(QTextLength(QTextLength::PercentageLength, 15)); columnLength.append(QTextLength(QTextLength::PercentageLength, 20)); columnLength.append(QTextLength(QTextLength::PercentageLength, 25)); tableFormat.setColumnWidthConstraints(columnLength); table = cursor.insertTable(20, 6, tableFormat);