On Sun, 16 Sep 2012 22:40:29 +0530, Shriramana Sharma <samj...@gmail.com>
wrote:
> Hello. I'm back on this list after quite some time.
> 
> I'm working on some stuff involving bezier curves and wanted to write
> my own bezier display applet to keep in handy, so that I can adjust
> the nodes and handles and it would show me real-time what the
> resultant curve looks like.
> 
> However even a minimal Bezier drawing (bezierview-0) throws errors on
> my main 64 bit system with PyQt 4.9.1 with Qt 4.8.1 on Kubuntu
> Precise. So I tested on a 32 bit Kubuntu Oneiric (older) VM
> installation I have which has PyQt 4.8.5 and Qt 4.7.4 and it worked OK
> enough. But once I got around to programming the important points
> (i.e. tweaking the points, see bezierview-1) it once more segfaulted
> even on that older system and I'm back to square one.
> 
> Help please! (FWIW the code is GPLed.) The minimal stuff if written in
> C++ doesn't segfault on my latest Qt 4.8.1. (I was hoping to do
> everything in PyQt so I only wrote the minimal thing in C++ so far.)
> 
> Thanks!

Your paintEvent() is broken because you are keeping references to the
painter (and most other things) when paintEvent() returns. Basically you
need to remove all the "self." from paintEvent() except those that
reference the genuine instance attributes. Attached is a fixed version of
your second example.

Phil
#! /usr/bin/env python

from PyQt4 . QtCore import *
from PyQt4 . QtGui import *

class BezierWidget ( QWidget ) :
	
	def __init__ ( self, parent = None ) :
		
		super ( BezierWidget, self ) . __init__ ( parent )
		self . setFixedSize ( 400, 400 )
		
		self . p1 = QPointF ( 100, 150 )
		self . c1 = QPointF ( 166, 250 )
		self . c2 = QPointF ( 234, 250 )
		self . p2 = QPointF ( 300, 150 )
		
		self . tweaking = False
	
	def paintEvent ( self, event ) :
		
		painter = QPainter ( self )
		painter . setRenderHint ( QPainter . Antialiasing )
		painter . translate ( 0, 400 )
		painter . scale ( 1, -1 )
		
		palette = QApplication . palette ()
		bezierPen = QPen ( palette . text (), 1 )
		handlePen = QPen ( palette . highlight (), 2, Qt . DashLine )
		
		bezier = QPainterPath ()
		bezier . moveTo ( self . p1 )
		bezier . cubicTo ( self . c1, self . c2, self . p2 )
		painter . strokePath ( bezier, bezierPen )
		
		diamond = QPainterPath ()
		diamond . moveTo (  2,  0 )
		diamond . lineTo (  0,  2 )
		diamond . lineTo ( -2,  0 )
		diamond . lineTo (  0, -2 )
		diamond . closeSubpath ()
		
		handle1 = QPainterPath ()
		handle1 . moveTo ( self . p1 )
		handle1 . lineTo ( self . c1 )
		painter . strokePath ( handle1, handlePen )
		
		handle2 = QPainterPath ()
		handle2 . moveTo ( self . p2 )
		handle2 . lineTo ( self . c2 )
		painter . strokePath ( handle2, handlePen )
		
		for x in ( self . p1, self . c1, self . c2, self . p2 ) :
			painter . save ()
			painter . translate ( x )
			painter . scale ( 2, 2 )
			painter . fillPath ( diamond, palette . highlightedText () )
			painter . restore ()
			
	def mousePressEvent ( self, event ) :
		pt = event . posF ()
		pt . setY ( 400 - pt . y () )
		print ( pt )
		offset = 5
		hotspot = QRectF ( - offset, - offset, offset * 2, offset * 2 )
		for x in ( self . p1, self . c1, self . c2, self . p2 ) :
			print ( hotspot . translated ( x ) )
			if hotspot . translated ( x ) . contains ( pt ) :
				print ( hotspot . translated ( x ), "contains", x )
				self . tweaking = True
				if   x is self . p1 : self . tweakedPoint = "p1"
				elif x is self . c1 : self . tweakedPoint = "c1"
				elif x is self . c2 : self . tweakedPoint = "c2"
				elif x is self . p2 : self . tweakedPoint = "p2"
				return
	
	def mouseMoveEvent ( self, event ) :
		if self . tweaking :
			pt = event . posF ()
			pt . setY ( 400 - pt . y () )
			print ( "moving " + self . tweakedPoint + " to", pt )
			if   self . tweakedPoint == "p1" : self . p1 = pt
			elif self . tweakedPoint == "c1" : self . c1 = pt
			elif self . tweakedPoint == "c2" : self . c2 = pt
			elif self . tweakedPoint == "p2" : self . p2 = pt
			self . update ()
	
	def mouseReleaseEvent ( self, event ) :
		self . tweaking = False

#class MainWindow ( QtGui . QWidget ) :
	#def __init__ ( self ) :
		#QWidget . __init__ ( self )
		#self . bezierWidget = BezierWidget ()

app = QApplication ( [] )
bezierWidget = BezierWidget ()
bezierWidget . show ()
app . exec_ ()
_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to