Hello. I previously reported a problem with drawing a rubberband and a
workaround was provided. I now find that I am unable to resize a
rubberband using a QRect which has been modified using setLeft etc.
Please see the attached files. The C++ version works whereas the Python
version does not.

// g++ -o rubberband2 rubberband2.cpp -I /usr/include/qt4/ -lQtCore -lQtGui

# include <QtCore/QtCore>
# include <QtGui/QtGui>

class rubberBandWidget : public QWidget
{
public :
	rubberBandWidget ( QWidget * parent = 0 ) ;
protected :
	void mousePressEvent ( QMouseEvent * event ) ;
	void mouseMoveEvent ( QMouseEvent * event ) ;
	void mouseReleaseEvent ( QMouseEvent * event ) ;
private :
	QRubberBand * rubberBand ;
	bool tweaking ;
	QPoint origin ;
	QString tweakingpart ;
	QPoint tl, tr, bl, br ;
} ;

rubberBandWidget :: rubberBandWidget ( QWidget * parent ) : QWidget ( parent )
{
	rubberBand = new QRubberBand ( QRubberBand :: Rectangle, this ) ;
	tweaking = false ;
}

void rubberBandWidget :: mousePressEvent ( QMouseEvent * event )
{
	QPoint pt = event -> pos () ;
	QRect rg = rubberBand -> geometry () ;

	if ( rg . isValid () )
	{
		tl = rg . topLeft () ; tr = rg . topRight () ; bl = rg . bottomLeft () ; br = rg . bottomRight () ;
		const QPoint off ( 3, 3 ), offx ( 4, -3 ), offy ( -3, 4 ) ;
		
		if      ( QRect ( tl - off , tl + off  ) . contains ( pt ) )
			{ tweakingpart = "topLeft"     ; this -> setCursor ( Qt :: SizeFDiagCursor ) ; }
		else if ( QRect ( tr - off , tr + off  ) . contains ( pt ) )
			{ tweakingpart = "topRight"    ; this -> setCursor ( Qt :: SizeBDiagCursor ) ; }
		else if ( QRect ( bl - off , bl + off  ) . contains ( pt ) )
			{ tweakingpart = "bottomLeft"  ; this -> setCursor ( Qt :: SizeBDiagCursor ) ; }
		else if ( QRect ( br - off , br + off  ) . contains ( pt ) )
			{ tweakingpart = "bottomRight" ; this -> setCursor ( Qt :: SizeFDiagCursor ) ; }
		else if ( QRect ( tl + offx, tr - offx ) . contains ( pt ) )
			{ tweakingpart = "top"         ; this -> setCursor ( Qt :: SizeVerCursor   ) ; }
		else if ( QRect ( bl + offx, br - offx ) . contains ( pt ) )
			{ tweakingpart = "bottom"      ; this -> setCursor ( Qt :: SizeVerCursor   ) ; }
		else if ( QRect ( tl + offy, bl - offy ) . contains ( pt ) )
			{ tweakingpart = "left"        ; this -> setCursor ( Qt :: SizeHorCursor   ) ; }
		else if ( QRect ( tr + offy, br - offy ) . contains ( pt ) )
			{ tweakingpart = "right"       ; this -> setCursor ( Qt :: SizeHorCursor   ) ; }
		
		if ( tweakingpart != "" )
		{
			tweaking = true ;
			return ;
		}
	}

	origin = pt ;
	rubberBand -> setGeometry ( QRect ( origin, QSize () ) ) ;
	rubberBand -> show () ;
}

void rubberBandWidget :: mouseMoveEvent ( QMouseEvent * event )
{
	QPoint pt = event -> pos () ;
	if ( tweaking )
	{
		QRect rg = rubberBand -> geometry () ;
		if      ( tweakingpart == "topLeft"     ) rg . setTopLeft     ( pt ) ;
		else if ( tweakingpart == "topRight"    ) rg . setTopRight    ( pt ) ;
		else if ( tweakingpart == "bottomLeft"  ) rg . setBottomLeft  ( pt ) ;
		else if ( tweakingpart == "bottomRight" ) rg . setBottomRight ( pt ) ;
		else if ( tweakingpart == "top"         ) rg . setTop         ( pt . y () ) ;
		else if ( tweakingpart == "bottom"      ) rg . setBottom      ( pt . y () ) ;
		else if ( tweakingpart == "left"        ) rg . setLeft        ( pt . x () ) ;
		else if ( tweakingpart == "right"       ) rg . setRight       ( pt . x () ) ;
		rubberBand -> setGeometry ( rg ) ;
	}
	else
	{
		rubberBand -> setGeometry ( QRect ( origin, pt ) . normalized () ) ;
	}
}

void rubberBandWidget :: mouseReleaseEvent ( QMouseEvent * event )
{
	tweaking = false ;
	tweakingpart = "" ;
	this -> unsetCursor () ;
}

int main ( int argc, char * argv [] )
{
	QApplication app ( argc, argv ) ;
	rubberBandWidget * widget = new rubberBandWidget ;
	widget -> setGeometry ( 300, 300, 300, 300 ) ;
	widget -> show () ;
	return app . exec () ;
}

#! /usr/bin/env python

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

class rubberBandWidget ( QWidget ) :
	def __init__ ( self, parent = None ) :
		QWidget . __init__ ( self, parent )
		self . rubberBand = QRubberBand ( QRubberBand . Rectangle, self )
		self . tweaking = False
		self . tweakingpart = ""

	def mousePressEvent ( self, event ) :
		pt = self . mapFromGlobal ( event . globalPos () )
		rg = self . rubberBand . geometry ()
		if rg . isValid () :
			tl, tr, bl, br = rg . topLeft (), rg . topRight (), rg . bottomLeft (), rg . bottomRight ()
			off, offx, offy = QPoint ( 3, 3 ), QPoint ( 4, -3 ), QPoint ( -3, 4 )

			if   QRect ( tl - off , tl + off ) . contains ( pt ) :
				self . tweakingpart = "topLeft"     ; self . setCursor ( Qt . SizeFDiagCursor )
			elif QRect ( tr - off , tr + off ) . contains ( pt ) :
				self . tweakingpart = "topRight"    ; self . setCursor ( Qt . SizeBDiagCursor )
			elif QRect ( bl - off , bl + off ) . contains ( pt ) :
				self . tweakingpart = "bottomLeft"  ; self . setCursor ( Qt . SizeBDiagCursor )
			elif QRect ( br - off , br + off ) . contains ( pt ) :
				self . tweakingpart = "bottomRight" ; self . setCursor ( Qt . SizeFDiagCursor )
			elif QRect ( tl + offx, tr - offx ) . contains ( pt ) :
				self . tweakingpart = "top"         ; self . setCursor ( Qt . SizeVerCursor   )
			elif QRect ( bl + offx, br - offx ) . contains ( pt ) :
				self . tweakingpart = "bottom"      ; self . setCursor ( Qt . SizeVerCursor   )
			elif QRect ( tl + offy, bl - offy ) . contains ( pt ) :
				self . tweakingpart = "left"        ; self . setCursor ( Qt . SizeHorCursor   )
			elif QRect ( tr + offy, br - offy ) . contains ( pt ) :
				self . tweakingpart = "right"       ; self . setCursor ( Qt . SizeHorCursor   )
			
			if self . tweakingpart != "" :
				self . tweaking = True
				return
		
		self . origin = pt
		self . rubberBand . setGeometry ( QRect ( self . origin, QSize () ) )
		self . rubberBand . show ()

	def mouseMoveEvent ( self, event ) :
		pt = self . mapFromGlobal ( event . globalPos () )
		if self . tweaking :
			rg = self . rubberBand . geometry ()
			if   self . tweakingpart == "topLeft"     : rg . setTopLeft     ( pt )
			elif self . tweakingpart == "topRight"    : rg . setTopRight    ( pt )
			elif self . tweakingpart == "bottomLeft"  : rg . setBottomLeft  ( pt )
			elif self . tweakingpart == "bottomRight" : rg . setBottomRight ( pt )
			elif self . tweakingpart == "top"         : rg . setTop         ( pt . y () )
			elif self . tweakingpart == "bottom"      : rg . setBottom      ( pt . y () )
			elif self . tweakingpart == "left"        : rg . setLeft        ( pt . x () )
			elif self . tweakingpart == "right"       : rg . setRight       ( pt . x () )
			self . rubberBand . setGeometry ( rg )
		else :
			self . rubberBand . setGeometry ( QRect ( self . origin, pt ) . normalized () )
	
	def mouseReleaseEvent ( self, event ) :
		self . tweaking = False
		self . tweakingpart = ""
		self . unsetCursor ()

app = QApplication ( sys . argv )
widget = rubberBandWidget ()
widget . setGeometry ( 300, 300, 300, 300 )
widget . show ()
app . exec_ ()

_______________________________________________
PyQt mailing list    [email protected]
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to