C++ (Qt)#include <QApplication>#include <QGraphicsScene>#include <QGraphicsView>#include <QGraphicsRectItem>#include <QMouseEvent> class RubberItem : public QGraphicsRectItem{public: RubberItem( const QRectF& r, QGraphicsItem* parent = 0 ) : QGraphicsRectItem( r, parent ) { QColor clr( Qt::blue ); setPen( clr ); clr.setAlphaF( .1 ); setBrush( clr ); } virtual ~RubberItem() {} QRectF boundingRect() const { qreal es = grip_size_ / 2; return rect().adjusted( -es, -es, es, es ); } void paint( QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0 ) { QGraphicsRectItem::paint( painter, option, widget ); QMatrix m = painter->matrix(); QRectF irect( QRectF( QPointF( 0, 0 ), m.mapRect( rect() ).size() ) ); qreal gs = grip_size_; QRectF r( -gs / 2, -gs / 2, gs, gs ); r.translate( m.map( QPointF( 0, 0 ) ) ); QColor clr( painter->brush().color() ); clr.setAlphaF( 1 ); painter->setBrush( clr ); painter->resetTransform(); painter->drawRect( r.translated( irect.topLeft() ) ); painter->drawRect( r.translated( QPointF( irect.width() / 2, 0 ) ) ); painter->drawRect( r.translated( irect.topRight() ) ); painter->drawRect( r.translated( QPointF( 0, irect.height() / 2 ) ) ); painter->drawRect( r.translated( irect.bottomLeft() ) ); painter->drawRect( r.translated( QPointF( irect.width(), irect.height() / 2 ) ) ); painter->drawRect( r.translated( irect.bottomRight() ) ); painter->drawRect( r.translated( QPointF( irect.width() / 2, irect.height() ) ) ); } Qt::WindowFrameSection frameSectionAt( const QPointF& p ) const { QRectF irect( rect() ); qreal gs = grip_size_; QRectF r( -gs / 2, -gs / 2, gs, gs ); r.translate( pos() ); Qt::WindowFrameSection section = Qt::NoSection; if( r.translated( irect.topLeft() ).contains( p ) ) section = Qt::TopLeftSection; else if( r.translated( QPointF( irect.width() / 2, 0 ) ).contains( p ) ) section = Qt::TopSection; else if( r.translated( irect.topRight() ).contains( p ) ) section = Qt::TopRightSection; else if( r.translated( QPointF( irect.width(), irect.height() / 2 ) ).contains( p ) ) section = Qt::RightSection; else if( r.translated( irect.bottomRight() ).contains( p ) ) section = Qt::BottomRightSection; else if( r.translated( QPointF( irect.width() / 2, irect.height() ) ).contains( p ) ) section = Qt::BottomSection; else if( r.translated( irect.bottomLeft() ).contains( p ) ) section = Qt::BottomLeftSection; else if( r.translated( QPointF( 0, irect.height() / 2 ) ).contains( p ) ) section = Qt::LeftSection; return section; } static qreal gripSize() { return grip_size_; } private: static const qreal grip_size_ = 5;}; class GraphicsView : public QGraphicsView{public: GraphicsView( QWidget* p = 0 ) : QGraphicsView( p ) { viewport()->setMouseTracking( true ); } void resizeEvent( QResizeEvent* e ) { fitInView( scene()->sceneRect() ); } void mouseMoveEvent( QMouseEvent* e ) { QGraphicsView::mouseMoveEvent( e ); QList< QGraphicsItem* > il = items( e->pos().x(), e->pos().y(), 1, 1, Qt::IntersectsItemBoundingRect ); RubberItem* ri; if( !il.isEmpty() && ( ri = dynamic_cast< RubberItem* >( il.first() ) ) ) updateRubberItemCursor( ri, e->pos() ); else viewport()->unsetCursor(); } void updateRubberItemCursor( RubberItem* ri, const QPoint& cpos ) { switch( ri->frameSectionAt( mapToScene( cpos ) ) ) { case Qt::TopLeftSection: case Qt::BottomRightSection: viewport()->setCursor( Qt::SizeFDiagCursor ); break; case Qt::TopRightSection: case Qt::BottomLeftSection: viewport()->setCursor( Qt::SizeBDiagCursor ); break; case Qt::TopSection: case Qt::BottomSection: viewport()->setCursor( Qt::SizeVerCursor ); break; case Qt::LeftSection: case Qt::RightSection: viewport()->setCursor( Qt::SizeHorCursor ); break; default: viewport()->unsetCursor(); break; } }}; int main( int argc, char** argv ){ QApplication app( argc, argv ); GraphicsView gv; QGraphicsScene scene; gv.setScene( &scene ); scene.setSceneRect( 0, 0, 300, 300 ); RubberItem item( QRectF( 0, 0, 200, 200 ) ); scene.addItem( &item ); item.setPos( 50, 50 ); gv.show(); return app.exec();}