html - 如何将 QT HTML 字符串添加到 CSS 文件?

标签 html css qt

我有这个代码来打印我的数据。但我无法将 html 字符串添加到 css。我需要将pdf文档添加到css。因为 html 字符串非常靠近纸张的右侧。

 QString html=
           "<!DOCTYPE html><html><body><b>"+json_map["Sample"].toString()+": </b><a>"+ui->sampleText->text()+"</a><br><b>"+json_map["Operator"].toString()+" : </b> <a>"+ui->operatorText->text()+"</a><br><b>"+json_map["Evalutaion"].toString()+" : </b> <a>"+ui->evalText->text()+" </a><br>"+
           "<b>"+json_map["Date"].toString()+" : </b> <a>"+ui->date->text()+"</a><br><br>"+
           "<b>"+json_map["Mixer"].toString()+" : </b> <a>300 </a>"+
           "<b>"+json_map["Moisture"].toString()+ ": </b> <a>"+ui->moistureText->text()+"</a><br>"+
           "<a>"+json_map["Consistency"].toString()+ ": </a><b></b><a>"+json_map["With"].toString()+" </a><a>"+ui->absorptionText->text()+"</a><br><br>"+
           "<b>"+json_map["Water500Fu"].toString()+" : </b> <a></a><br>"+
           "<b>"+json_map["Water14%"].toString()+" : </b> <a></a><br>"+
           "<b>"+json_map["DevelopmentTime"].toString()+" : </b> <a></a><br>"+
           "<b>"+json_map["Stability"].toString()+" : </b> <a></a><br>"+
           "<b>"+json_map["Softening10min"].toString()+" </b> <a></a><br>"+
           "<b>"+json_map["Softening12min"].toString()+" : </b><a></a><br>"+
           "<b>"+json_map["FQN"].toString()+" : </b> <a></a><br>"+
           "<b>"+json_map["Remarks"].toString()+" : </b><a>"+ui->remarkText->text()+"</a><br>"+
           "</body>"+
           "</html>";

   QTextDocument parent;
   parent.setHtml(html);
   QPrinter printer(QPrinter::HighResolution);
   printer.setOutputFormat(QPrinter::PdfFormat);
   printer.setColorMode(QPrinter::Color);
   printer.setOutputFileName(QDir::homePath()+"/Desktop/"+ui->operatorText->text());
   QPrintDialog *dlg = new QPrintDialog(&printer, this);
   dlg->setWindowTitle(QObject::tr("Bastak Test"));

   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();
       //formula.savejson(printer.outputFileName());

    }
   delete dlg;

最佳答案

我有点难以理解你的问题,所以我做了一些假设并等待你的回复:

  • 您想从 HTML 生成 PDF,
  • 您需要将 CSS 应用于 HTML

所以为了测试它,我做了以下最小示例:

#include <memory>
#include <QApplication>
#include <QPrinter>
#include <QString>
#include <QDebug>
#include <QTextDocument>
#include <QDir>
#include <QPrintDialog>
#include <QPainter>

int main( int argn, char **argc)
{
    QApplication app(argn, argc);
    QString html="<!DOCTYPE html><html><body style='color: blue;'>Hello World!</body></html>";

    QTextDocument parent;
    parent.setHtml(html);
    QPrinter printer(QPrinter::HighResolution);
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setColorMode(QPrinter::Color);
    printer.setOutputFileName("test.pdf");

    QPainter painter(&printer);
    parent.setDefaultFont(QFont("Times", 200));
    parent.drawContents(&painter);
    painter.end();

   return 0;
}

我得到以下结果:

enter image description here

关于html - 如何将 QT HTML 字符串添加到 CSS 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41893694/

相关文章:

javascript - CSS3 响应式下拉菜单不起作用

QtScript:如何重新加载当前脚本?

html - 如何在php回显字符串旁边的标题中添加一个按钮

html - 高度控制 - 没有绝对定位的多步形式

css - Material 设计菜单

c++ - Qt-追加到QList

qt - QDBusAbstractAdaptor 对象不导出已实现的 dbus 接口(interface)

html - 使用内部 css 更改下拉菜单属性

html - 如何使用html和css制作这个div?

html - 允许用户输入 HTML - 这明智吗?