Created
August 21, 2009 11:38
-
-
Save jarib/171890 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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