Monday, March 12, 2012

[QT] CREATE AN ODT FILE



/*
 **************     CREATING A ODT FILE              ****************
 **************     INCLUDE THE NECESSARY FILES     ********/
{
    ...
    // Text Document Object to hold formatted document
    QTextDocument *m_document = new QTextDocument();

    // Cursor for traversing the document
    QTextCursor m_cursor = QTextCursor(m_document);

    // TextTableFormat object for table formatting
    QTextTableFormat tableFormat;
    tableFormat.setAlignment(Qt::AlignCenter);
    tableFormat.setCellPadding(5);
    tableFormat.setCellSpacing(50);
    tableFormat.setHeaderRowCount(0);
    tableFormat.setBorderStyle( QTextFrameFormat::BorderStyle_Solid);
    tableFormat.setWidth(QTextLength(QTextLength::PercentageLength, 60));

    // Create table to hold the text and it's easy to format

    QTextTable *m_table= m_cursor.insertTable(10,3,tableFormat);
    m_table->mergeCells(0,0,1,2);
    m_table->mergeCells(1,0,1,2);

    //Lets Insert the text and Pictures
    m_cursor.insertText(QObject::tr("Welcome to Point Element\n"));

    m_cursor.movePosition(QTextCursor::NextRow);

    // Insert Photo
    QImage photo("/home/user/myphoto.jpg");
    photo =photo.scaledToHeight(300,Qt::SmoothTransformation);
    photo =photo.scaledToWidth(100,Qt::SmoothTransformation);
    m_cursor.insertImage(photo,"Photo");
    m_cursor.movePosition(QTextCursor::NextRow);

    m_cursor.insertText(QObject::tr("User Name:"));
    m_cursor.movePosition(QTextCursor::NextCell);
    m_cursor.insertText(QObject::tr(ui->LE_name->text().toAscii()));
    //m_cursor.insertText(QObject::tr("Anay"));
    m_cursor.movePosition(QTextCursor::NextRow);
    m_cursor.insertText(QObject::tr("Age:"));
    m_cursor.movePosition(QTextCursor::NextCell);
    m_cursor.insertText(QObject::tr(ui->LE_age->text().toAscii()));
    //m_cursor.insertText(QObject::tr("45"));
    m_cursor.movePosition(QTextCursor::NextRow);
    m_cursor.insertText(QObject::tr("Gender:"));
    m_cursor.movePosition(QTextCursor::NextCell);
    m_cursor.insertText(QObject::tr(ui->CMB_gender->currentText().toAscii()));
    //m_cursor.insertText(QObject::tr("MALE:"));
    m_cursor.movePosition(QTextCursor::NextRow);
    m_cursor.insertText(QObject::tr("Employer Name:"));
    m_cursor.movePosition(QTextCursor::NextCell);
    m_cursor.insertText(QObject::tr(ui->LE_cname->text().toAscii()));
    //m_cursor.insertText(QObject::tr("Aniket"));

    //Leave 3-4 empty lines and then add signature
    m_cursor.movePosition(QTextCursor::NextRow,QTextCursor::MoveAnchor, 4);

    m_cursor.insertText(QObject::tr("Sign"));
    m_cursor.movePosition(QTextCursor::NextCell);
    m_cursor.insertText(QObject::tr("___________________________"));

    // FILE NAME
    QString docfilename = ui->LE_name->text();
    docfilename.append(".odt");
    docfilename.prepend("/home/user/");

    // Write the Document to the file on Disk
    QTextDocumentWriter writer;
    writer.setFormat("odf");
    writer.setFileName(docfilename.toAscii());
    writer.write(m_document);

/************************   DONE   ****************************/

}

No comments:

Post a Comment