[PyKDE] Trouble figuring out QCanvas and QCanvasView

David Boddie david at boddie.org.uk
Wed Oct 6 11:32:21 BST 2004


On Tue, 05 Oct 2004 23:28:39, Bryan O'Sullivan wrote: 
 
> I am unable to figure out how to draw into a QCanvas and have it show up 
> correctly in a QCanvasView.  I've tried looking at both C++ and Python 
> examples, but the examples are invariably so complex that it's 
> impossible to figure out what is actually important. 
 
That's disappointing to hear. 
 
> I've attached the simple example I'm trying to make work.  It ought to 
> draw a diagonal black line, but nothing shows up.  Clearly, I'm missing 
> something, but neither the documentation nor the examples suggest what 
> that might be. 
 
I think that the problem is with the ownership of the QCanvas, although 
it's very strange if that really is the case since you pass it to the 
QCanvasView. I "fixed" your example so that it would work by giving the 
QCanvas a parent when it is created. I've included the modified code below. 
 
Hope this helps, 
 
David 
 
 
import qtcanvas 
import qt 
import sys 
 
class Widge(qt.QWidget): 
    def __init__(self): 
        qt.QWidget.__init__(self, None) 
        self.setCaption('Line?') 
        c = qtcanvas.QCanvas(self) # <- gives the canvas a parent QObject 
        c.resize(100, 100) 
        v = qtcanvas.QCanvasView(self) 
        lay = qt.QVBoxLayout(self) 
        lay.addWidget(v) 
        v.setCanvas(c) 
        l = qtcanvas.QCanvasLine(c) 
        l.setPen(qt.QPen(qt.Qt.black)) 
        l.setPoints(10,10,90,90) 
        l.show() 
        c.update() 
        self.resize(self.sizeHint()) 
 
class App(qt.QApplication): 
    def __init__(self, args): 
        qt.QApplication.__init__(self, args) 
        self.w = Widge() 
        self.w.show() 
        self.setMainWidget(self.w) 
 
app = App(sys.argv) 
app.exec_loop() 
 


___________________________________________________________
$0 Web Hosting with up to 120MB web space, 1000 MB Transfer
10 Personalized POP and Web E-mail Accounts, and much more.
Signup at www.doteasy.com




More information about the PyQt mailing list