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!

-- 
Shriramana Sharma
#! /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 )
		
	def paintEvent ( self, event ) :
		
		self . painter = QPainter ( self )
		self . painter . setRenderHint ( QPainter . Antialiasing )
		self . painter . translate ( 0, 400 )
		self . painter . scale ( 1, -1 )
		
		self . bezier = QPainterPath ()
		self . bezier . moveTo ( self . p1 )
		self . bezier . cubicTo ( self . c1, self . c2, self . p2 )
		self . painter . strokePath ( self . bezier, self . painter . pen () )
		
app = QApplication ( [] )
bezierWidget = BezierWidget ()
bezierWidget . show ()
app . exec_ ()
#! /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 ) :
		
		self . painter = QPainter ( self )
		self . painter . setRenderHint ( QPainter . Antialiasing )
		self . painter . translate ( 0, 400 )
		self . painter . scale ( 1, -1 )
		
		self . palette = QApplication . palette ()
		self . bezierPen = QPen ( self . palette . text (), 1 )
		self . handlePen = QPen ( self . palette . highlight (), 2, Qt . DashLine )
		
		self . bezier = QPainterPath ()
		self . bezier . moveTo ( self . p1 )
		self . bezier . cubicTo ( self . c1, self . c2, self . p2 )
		self . painter . strokePath ( self . bezier, self . bezierPen )
		
		self . diamond = QPainterPath ()
		self . diamond . moveTo (  2,  0 )
		self . diamond . lineTo (  0,  2 )
		self . diamond . lineTo ( -2,  0 )
		self . diamond . lineTo (  0, -2 )
		self . diamond . closeSubpath ()
		
		self . handle1 = QPainterPath ()
		self . handle1 . moveTo ( self . p1 )
		self . handle1 . lineTo ( self . c1 )
		self . painter . strokePath ( self . handle1, self . handlePen )
		
		self . handle2 = QPainterPath ()
		self . handle2 . moveTo ( self . p2 )
		self . handle2 . lineTo ( self . c2 )
		self . painter . strokePath ( self . handle2, self . handlePen )
		
		for x in ( self . p1, self . c1, self . c2, self . p2 ) :
			self . painter . save ()
			self . painter . translate ( x )
			self . painter . scale ( 2, 2 )
			self . painter . fillPath ( self . diamond, self . palette . highlightedText () )
			self . 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