Qt将JSON和PDF文件保存到路径

时间:2017-01-17 10:23:12

标签: json qt file save

我有两个申请文件。一个是用QJSON保存数据文件,另一个是pdf,包括应用程序的信息。

我想将这两个文件保存到路径中(由用户选择)。如何获取用户的选择文件路径? Fallowing代码块有我的pdf保存方法和QJSON方法。 - 保存PDF;

void MainWindow::saveAsPdf(){

   QString html=
           "<!DOCTYPE html><html><body><b>Sample : </b><a>"+ui->sampleText->text()+"</a><br><b>Operator : </b> <a>"+ui->operatorText->text()+"</a><br><b>Method : </b> <a>"+ui->evalText->text()+" </a><br>"+
           "<b>Date : </b> <a>"+ui->date->text()+"</a><br><br>"+
           "<b>Mixer : </b> <a>300 </a>"+
           "<b>Moisture Content : </b> <a>"+ui->moistureText->text()+"</a><br>"+
           "<a>Consistency </a><b></b><a>with waterabsorption </a><a>"+ui->absorptionText->text()+"</a><br><br>"+
           "<b>Waterabsorption (correct for 500 FU) : </b> <a></a><br>"+
           "<b>Waterabsorption (correct for 14.0 %) : </b> <a></a><br>"+
           "<b>DevelopmentTime : </b> <a></a><br>"+
           "<b>Stability : </b> <a></a><br>"+
           "<b>Degree of Softening (10 min after begin) : </b> <a></a><br>"+
           "<b>Degree of Softening (ICC/ 12 min after max) : </b><a></a><br>"+
           "<b>Farinograph quality number : </b> <a></a><br>"+
           "<b>Remarks : </b><a>"+ui->remaskText->text()+"</a><br>"+
           "</body>"+
           "</html>";

   QTextDocument parent;
   parent.setHtml(html);
   QPrinter printer(QPrinter::HighResolution);
   printer.setOutputFormat(QPrinter::PdfFormat);
   printer.setColorMode(QPrinter::Color);
   //printer.getPageMargins(*left);
   printer.setOutputFileName(ui->operatorText->text());
   QPrintDialog*dlg = new QPrintDialog(&printer, this);
   dlg->setWindowTitle(QObject::tr("Print Document"));

   if(dlg->exec() == QDialog::Accepted) {

       QPainter painter(&printer);
       painter.drawPixmap(500, 5500, QPixmap(ui->qcustom->toPixmap(800,500,10.0)));
       parent.setDefaultFont(QFont("Times", 200));
       parent.drawContents(&painter);
       painter.end();

    }
   delete dlg;

}

- 在其他类方法中保存JSON文件,它只获取文件路径:

 formula.savejson("/home/elif/Desktop/"+ui->operatorText->text()+".txt");

1 个答案:

答案 0 :(得分:0)

如果您希望用户选择路径,请查看QFileDialog::getExistingDirectory()