Skip to content

Instantly share code, notes, and snippets.

@jarib
Created August 21, 2009 11:38
Show Gist options
  • Save jarib/171890 to your computer and use it in GitHub Desktop.
Save jarib/171890 to your computer and use it in GitHub Desktop.
#include "htmlsnap.h"
#include <QPainter>
#include <QDebug>
#include <QWebFrame>
HtmlSnap::HtmlSnap()
{
connect(&page, SIGNAL(loadFinished(bool)), this, SLOT(render()));
}
void HtmlSnap::loadHtml(char* html)
{
qDebug() << "loading HTML";
page.mainFrame()->setHtml(QString(html), QUrl());
}
void HtmlSnap::render()
{
qDebug() << "rendering";
page.setViewportSize(page.mainFrame()->contentsSize());
QImage image(page.viewportSize(), QImage::Format_ARGB32);
QPainter painter(&image);
page.mainFrame()->render(&painter);
painter.end();
QImage thumbnail = image.scaled(400, 400);
thumbnail.save("thumbnail.png");
qDebug() << "done";
emit finished();
}
#ifndef HTMLSNAP_H
#define HTMLSNAP_H
#include <QObject>
#include <QWebPage>
class HtmlSnap : public QObject
{
Q_OBJECT
QWebPage page;
public:
HtmlSnap();
void loadHtml(char* html);
private slots:
void render();
signals:
void finished();
};
#endif // HTMLSNAP_H
#include <QApplication>
#include "htmlsnap.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
HtmlSnap snap;
QObject::connect(&snap, SIGNAL(finished()), &app, SLOT(quit()));
snap.loadHtml("<h1>Hello World</h1>");
return app.exec();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment