lua-users home
lua-l archive

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


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()