[PyKDE] KHTML and so on...

Jim Bublitz jbublitz at nwinternet.com
Tue Apr 12 18:30:52 BST 2005

On Tuesday 12 April 2005 08:04, Carlo Truijllo wrote:
> Hi guys!
>    I'm trying to develope a simple automated browser able to print pages in
> ps files.
> The main goal in my approach are :
> 1 - render html page
> 2 - print in a file
> In the code below
> in line
> browser.paint(p,k,0,0)   #  KHTMLPart object .paint
> my appl goes in segfault.
> Thanks a lot
> Carlo
> import sys
> from qt import QObject,SIGNAL,SLOT
> from qt import *
> from kdecore import KApplication,KURL
> from kdeui import KMainWindow
> from khtml import KHTMLPart
> from kdeprint import KPrinter
> a = KApplication(['1'],"p4")
> vbox = QVBox()
> location = QLineEdit(vbox)
> location.setText("http://www.repubblica.it")
> browser = KHTMLPart(vbox)
> browser.openURL(KURL("http://www.repubblica.it"))
> l=dir(browser)
> k=QRect(0,0,800,600)
> p=QPainter()
> browser.paint(p,k,0,0)

There are a few problems with your program.

1. You don't have an event loop running  - you need to call 
KApplication.exec_loop ()  (or a.exec_loop () in your example) before you 
call browser.paint. That means you'll need some kind of event (button, menu 
entry, timer) to trigger the paint event. "exec_loop" in PyQt corresponds to 
"exec" in Qt - the name change is to avoid a conflict with the Python "exec" 

2. KHTMLPart is descended from KPart, so instead of KMainWindow you need to 
use KPart.MainWindow.  You'll probably need a .rc file too.

For (1) and (2) you can look at PyKDE/examples/pyKHTMLPart.py for an example 
of how to set up the main window and event loop and the .rc file. You can use 
pyKHTMLPart.py as a starting point - you'll need to add code to start 

3. Setting up a KPrinter/QPrinter to print is a little more involved than what 
your code shows. See, for example, the way printing is done in 
PyQt/examples3/application.py, and the Qt and KDE docs. KPrinter should 
function about the same way QPrinter does. You can get your app working with 
QPrinter first, and then switch to KPrinter. I've always found 
QPrinter/KPrinter a little tricky to set up and get working, so locating the 
docs is a good idea in this case.


More information about the PyQt mailing list