[英]Pass formatted text from QML to C++
I have a simple pdf generator in my QML app, which uses C++ to receive a signal and write my PDF document to the device - when using a sample TestHtml.html
document in the resources prints as intended, but I'm struggling to be able直接從 QML 頁面發送自定義 HTML 格式的文本。
我想做的事
我的reportPage.qml
顯示不同的數據/信息,當我單擊“下載”按鈕時,它會將這些格式化的數據發送到 C++ 以打印到 PDF,(C++不是我的強項)
我試過的
QIODevice
write
.html文件,根據需要有效刪除/重寫數據reportPage.qml
作為字符串傳遞reportPage.qml
我唯一的問題是,c++ 不是我的強項,我得到的只是我的pdf.cpp
中的錯誤,例如未聲明的標識符,沒有匹配的成員 ZC1C425268E68385D1AB5074C17A94F14 等等
我的問題是
What is the best way to pass formatted html text using signals/slots form QML to C++ to print as a pdf using my pdf printer shown in the code below?
目前,我的代碼的簡化部分是:
reportPage.qml
import Felgo 3.0
import com.allbookd.pdf 1.0
import QtQuick.Controls 1.2
Page {
id: reportPage
MyPdf {
id: pdf
}
AppButton {
text: "download"
var htmlData = "<b>Hello</b> World
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>"
onClicked: { pdf.saveInvoice(htmlData); }
}
}
pdf.h
#ifndef PDF_H
#define PDF_H
#include <QObject>
#include <QDebug>
#include <QPainter>
#include <QTextDocument>
#include <QPdfWriter>
#include <QDate>
#include <QStandardPaths>
#include <QPrinter>
#include <QFile>
class pdf : public QObject
{
Q_OBJECT
public:
explicit pdf(QObject *parent = nullptr);
void setCurrentDate();
QString getCurrentDate() const;
signals:
void invoiceSaved(int error);
public slots:
QString getPdfPath() const;
void saveInvoice();
private:
QString currentDate;
QString pdfPath;
};
#endif // PDF_H
pdf.cpp
pdf::pdf(QObject *parent) : QObject(parent)
{
setCurrentDate();
QString path = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
pdfPath = path + "/invoice_" + getCurrentDate() + ".pdf";
}
void pdf::setCurrentDate()
{
QDate date(QDate::currentDate());
int day = date.day();
int month = date.month();
int year = date.year();
currentDate = QString::number(day) + "_" + QString::number(month) + "_" + QString::number(year);
}
QString pdf::getCurrentDate() const
{
return currentDate;
}
QString pdf::getPdfPath() const
{
return pdfPath;
}
void pdf::saveInvoice(QString htmlData)
{
int error = 0;
QPdfWriter pdfWriter(getPdfPath());
pdfWriter.setPageSize(QPageSize(QPageSize::A4));
QPainter painter(&pdfWriter);
painter.scale(15.0, 15.0);
QFile file(":/htmlcode.html"); //the HTML file from resources
if(file.open(QIODevice::ReadWrite)) {
QByteArray temp = file.readAll(); //read it all
QString html = temp; //convert it to QString
QTextDocument doc;
doc.setHtml(html); //set it as HTML
doc.drawContents(&painter);
} else {
qDebug() << "error: " << file.error();
error = 1;
}
emit invoiceSaved(error);
}
您可以將pdf class 暴露給 QML。 使函數 Q_INVOKABLE 能夠直接從 QML 調用它們,甚至更好地使用 Q_PROPERTY
Q_INVOKABLE的文檔Q_PROPERTY的文檔
這是一個例子:
在main.cpp中注冊/公開您的 pdf class 到 qml
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "pdf.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
qmlRegisterType<pdf>("com.allbookd.pdf", 1, 0, "PdfPrinter");
QQmlApplicationEngine engine;
............
return app.exec();
}
使saveInvoice function 在pdf.h中可調用:
#include <QObject>
class pdf : public QObject
{
Q_OBJECT
public:
explicit pdf(QObject *parent = nullptr);
void setCurrentDate();
QString getCurrentDate() const;
Q_INVOKABLE void saveInvoice(QString htmlData);
..............
並使用 QPdfWriter 將htmlData保存到 pdf:
void pdf::saveInvoice(QString htmlData)
{
int error = 0;
QPdfWriter pdfWriter(getPdfPath());
pdfWriter.setPageSize(QPageSize(QPageSize::A4));
QPainter painter(&pdfWriter);
painter.scale(15.0, 15.0);
QTextDocument doc;
doc.setHtml(htmlData);
doc.drawContents(&painter);
emit invoiceSaved(error);
}
您的 QML 可能看起來像這樣:
import Felgo 3.0
import com.allbookd.pdf 1.0
import QtQuick.Controls 1.2
Page {
id: reportPage
property PdfPrinter pdf: PdfPrinter{}
AppButton {
text: "download"
var htmlData = "<b>Hello</b> World
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>"
onClicked: { pdf.saveInvoice(htmlData); }
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.