lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tue, Apr 12, 2011 at 5:21 PM, Michal Kottman <k0mpjut0r@gmail.com> wrote:
On Tue, 2011-04-12 at 16:38 +0200, Bertrand Mansion wrote:

> I'd love to see a tutorial on how to take a webpage screenshot using
> lqt. It seems that QPainter + QWebview could be useful for that but I
> didn't try yet, Qt doc is big...

require 'qtcore'
require 'qtgui'
require 'qtwebkit'

-- create a graphical application for QWebView to work, no arguments
app = QApplication.new(0, {})
-- create the WebKit view
local web = QWebView()

-- this will be run when the page is loaded
web:connect('2loadFinished(bool)', function()
       -- create an image, choose your size
       local image = QPixmap(640,480)
       -- create a painter that will write to the image
       local painter = QPainter()
       painter:begin(image)

       -- actually render the main frame to the painter
       web:page():mainFrame():render(painter)

       -- 'end' is a keyword in Lua, so you have to use this syntax
       painter["end"](painter)
       image:save("screenshot.jpg")

       -- quit the event loop (static function)
       app.quit()
end)

web:load(QUrl("http://www.lua.org"))
app.exec()

Thank you for the example :)

I had to install Xvfb in order to have it run on my Linux server that has no GUI, otherwise I had a "cannot connect to X Server" error. The rendering is a bit ugly and shows scrollbars, but I guess this can be improved.

--
Bertrand Mansion
Mamasam