如何在 Qprinter 中控制 HTML 代码样式



我在设置用于打印的 html 代码样式时遇到问题

我有带有 CSS 代码的 HTML 文件

我是这样读这个文件的

void Vente::print_Facture()
{
    QString html;
    QFile reportfile("E:/apprendreQt/gestionstock6/includes/reports/facture.html");
    if ( ! reportfile.open(QIODevice::ReadOnly | QIODevice::Text) ) {
        return;
    }
    html = reportfile.readAll();
    replaceHtml(html);
}
然后替换HTML

函数替换HTML中的一些单词(女巫的意思是我正在使用该文件作为模板并用数据库中的数据填充它)

void Vente::replaceHtml(QString &html)
{
    // get vente id
    int num_bl = ui->numeroBLlineEdit->text().toInt() ;
    QString requette = "SELECT  produits.designation,produits_en_ventes.qte_out,produits_en_ventes.prix,(produits_en_ventes.qte_out * produits_en_ventes.prix) as montant from ventes left join produits_en_ventes on ventes.vente_id = produits_en_ventes.vente_id left join produits on produits_en_ventes.produit_id = produits.produit_id where ventes.vente_id = :vente_id ";
    if(!m_db->isOpen())
        QMessageBox::critical(this,tr("Inventoria Solution"),m_db->lastError().text()) ;
    else{
        m_query->clear();
        m_query->prepare(requette);
        m_query->bindValue(":vente_id",num_bl);
        if(!m_query->exec())
            QMessageBox::critical(this,tr("Inventoria Solution"),m_query->lastError().text()) ;
        else{
            html = html.replace("%entrepriseNumRegCommerce%","Numéro de registre de commece ")
                    .replace("%entrepriseNumIdenFiscale%","Numéro d'identifiant fiscale")
                    .replace("%numFac%","num facture")
                    .replace("%nomClient%","Nom de client")
                    .replace("%numRegCommerce%","client numéro de registre de commerce")
                    .replace("%numIdenFiscale%","client identifiant fiscale");
            while(m_query->next())
            {
                html +=  "<tr>"
                                 "<td>"+ m_query->value(0).toString() +"</td>"
                                 "<td>"+ QString::number(m_query->value(1).toInt())+"</td>"
                                 "<td>"+ QString::number(m_query->value(2).toInt())+"</td>"
                                 "<td>"+ QString::number(m_query->value(2).toInt())+"</td>"
                                 "</tr>" ;
            }
            html += "</tbody>"
                    "</table>"
                    "<!-- footer of facture -->"
                    "</div>"
                    "</body>"
                    "</html>";
                    print_Html(html);
        }
    }
}

然后我打印HTML

void Vente::print_Html( QString &html)
{
    QPrintDialog printDialog(m_printer, this);
    if (printDialog.exec()) {
        QTextDocument textDocument;
        textDocument.setHtml(html);
        textDocument.print(m_printer);
    }
}

问题是当出现打印对话框并打印HTML代码时,样式根本不起作用

我如何控制 HTML 元素的位置以及设置它们的样式

你的html应该在支持的html标签中,我也建议你使用xml解析,然后用qdomnode方法替换和更改html标签,还要小心打开和关闭html标签。

最新更新