//----------------------------------------------------------------- void Exporter::exportDoc() { QTextDocument *document = buildFinalDoc(); if(ui->fileTypeComboBox->currentIndex() == 0) format = "html"; if(ui->fileTypeComboBox->currentIndex() == 1) format = "odt"; if(ui->fileTypeComboBox->currentIndex() == 2){ format = "txt"; QMessageBox::information(this, tr("Project Exporter"), tr("You have selected the .txt format. There is no formatting !"), QMessageBox::Ok); } QTextDocumentWriter writer; QByteArray array; QString fileName(ui->pathLineEdit->text() + "/" + ui->fileNameLineEdit->text() + "." + format); writer.setFormat(array.append(format)); writer.setFileName(fileName); writer.setCodec(QTextCodec::codecForName("UTF-8")); writer.write(document); if(format=="html"){ QFile *textFile = new QFile; textFile->setFileName(fileName); bool opened = textFile->open(QFile::ReadWrite | QFile::Text); // qDebug() << opened ; QTextStream textFileStream( textFile ); QString textString(textFileStream.readAll()); if (textFile->resize(0)) { QRegExp regExpCss("<style type=\x0022text/css\x0022>.*</style>"); regExpCss.setMinimal(true); textString = textString.replace( regExpCss, "<style type=\x0022text/css\x0022>p, li { white-space: pre-wrap; } p{line-height: 2em; font-family:'Liberation Serif'; font-size:12pt;margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:72px;}</style>"); // qDebug() << textString; QByteArray array; array.append(textString); textFile->write(array); } textFile->close(); } QApplication::restoreOverrideCursor(); QMessageBox::information(this, tr("Project exported"), tr("This project was successfully exported !"), QMessageBox::Ok); }
bool CppTimerInterval::saveFile() { p_csvheader = "Event,Duration,DurationSec,Interval,IntervalSec,Start,End\n"; p_document->undo(); // remove previous info on CSV header p_document->undo(); // remove previous info on data log p_cursor.insertText(p_csvheader); // insert CSV header p_cursor.insertText(p_dataLog); // insert data log QTextDocumentWriter writer; writer.setFormat("plaintext"); writer.setFileName(p_filename); bool success = writer.write(p_document); return success; }
int main(int argc, char *argv[]) { QApplication a(argc, argv); LineEdit l; l.show(); QTextEdit* text = new QTextEdit; text->setPlainText("Simple text2"); QTextDocumentWriter writer; writer.setFileName("teeeext.txt"); writer.write(text->document()); QTextEdit* pdf = new QTextEdit("PDF text"); QPrinter printer(QPrinter::HighResolution); printer.setOutputFormat(QPrinter::PdfFormat); printer.setOutputFileName("output.pdf"); pdf->document()->print(&printer); return a.exec(); }