C++ QWebEngineView setHtml不更新html并将其保留为空
我试图打印一个png图像从随机html,我会收到。但是,无论何时调用C++ QWebEngineView setHtml不更新html并将其保留为空,c++,qt,html-rendering,qwebengineview,C++,Qt,Html Rendering,Qwebengineview,我试图打印一个png图像从随机html,我会收到。但是,无论何时调用QWebEngineView::setHtml,都会正确触发信号QWebEngine::loadFinished,但从未设置html。我尝试在下面创建一个最小版本: //main.cpp QApplication app(argc, argv); const QString html = "<!DOCTYPE html><html><body><div><h1&
QWebEngineView::setHtml
,都会正确触发信号QWebEngine::loadFinished
,但从未设置html。我尝试在下面创建一个最小版本:
//main.cpp
QApplication app(argc, argv);
const QString html = "<!DOCTYPE html><html><body><div><h1>Title</h1><p style=\"background-color:red;\">Some Text</p></div></body></html>";
DomCleaner cleaner(&app);
QObject::connect(&cleaner, &DomCleaner::finished, &app, &QApplication::quit);
cleaner.clean(html, QSize(800, 800));
return app.exec();
//main.cpp
QApplication应用程序(argc、argv);
const QString html=“Title一些文本
”;
DomCleaner&app;
QObject::connect(&cleaner,&DomCleaner::finished,&app,&QApplication::quit);
cleaner.clean(html,QSize(800800));
返回app.exec();
我班的班长
//domcleaner.h
//nevermind the name, it is because in the future I will do dom manipulation
class DomCleaner : public QObject
{
Q_OBJECT
private:
std::unique_ptr<QGraphicsScene> scene_;
std::unique_ptr<QWebEngineView> view_;
QImage image_;
public:
explicit DomCleaner(QObject *parent = nullptr);
private:
void traverseItem(QGraphicsItem *item);
public slots:
void clean(const QString& html, QSize size);
private slots:
void onHtmlLoaded();
signals:
void finished();
};
//domcleaner.h
//别管名字了,因为将来我会做dom操作
类:公共QObject
{
Q_对象
私人:
std::独特的ptr场景;
std::唯一的ptr视图;
齐玛格图像;
公众:
显式DomCleaner(QObject*parent=nullptr);
私人:
无效遍历项(QGraphicsItem*项);
公众时段:
void clean(常量QString&html,QSize大小);
专用插槽:
void onHtmlLoaded();
信号:
无效完成();
};
最后是类的定义
//domcleaner.cpp
DomCleaner::DomCleaner(QObject *parent) : QObject(parent)
{
scene_.reset(new QGraphicsScene());
view_.reset(new QWebEngineView());
scene_->addWidget(view_.get());
QObject::connect(view_.get(), &QWebEngineView::loadFinished, this, &DomCleaner::onHtmlLoaded);
}
void DomCleaner::clean(const QString& html, QSize size)
{
image_ = QImage(size, QImage::Format_ARGB32);
view_->resize(size);
view_->setHtml(html);
//this one is working
//view_.get()->load(QUrl("https://doc.qt.io/qt-5/qgraphicsitem.html"));
view_.get()->show();
}
void DomCleaner::onHtmlLoaded()
{
QPainter painter(&image_);
view_->render(&painter);
painter.end();
// prints an empty string
view_->page()->toHtml([](QString html){ std::cout << html.toStdString() << std::endl; });
image_.save("C:\\path\\to\\file\\test.png", "PNG");
emit finished();
}
//domcleaner.cpp
DomCleaner::DomCleaner(QObject*parent):QObject(parent)
{
场景重置(新的qgraphicscene());
视图.重置(新的QWebEngineView());
场景->添加小部件(view.get());
QObject::connect(view.get(),&QWebEngineView::loadFinished,this,&DomCleaner::onHtmlLoaded);
}
void DomCleaner::clean(常量QString&html,QSize大小)
{
图像=QImage(大小,QImage::格式为ARGB32);
查看->调整大小(大小);
查看->设置html(html);
//这个有效
//视图获取()->加载(QUrl(“https://doc.qt.io/qt-5/qgraphicsitem.html"));
查看。获取()->show();
}
void DomCleaner::onHtmlLoaded()
{
Q油漆工(和图像);
查看->渲染(&painter);
结束();
//打印空字符串
查看->页面()->toHtml([](QString html){std::cout