How to create of Viewport size on Rectangle selection
Unsolved
General and Desktop
-
Hi and welcome to devnet,
What view are you talking about ?
-
@Jaisan
Untested, so forgive any possible compile issues, but it should give you an idea:class MyGraphicsView : public QGraphicsVIew { public: MyGraphicsView( QWidget* parent = Q_NULLPTR ) : QGraphicsView(parent) { mRubberBand = new QRubberBand( QRubberBand::Rectangle, this->viewport() ); mRubberBand->hide(); } protected: QRubberBand* mRubberBand; QPoint mOrigin; virtual bool viewportEvent(QEvent * event) { switch( event->type() ) { case QEvent::MouseButtonPress: { QMouseEvent* me = static_cast<QMouseEvent*>( event ); if( me->button() == Qt::LeftButton ) { mOrigin = me->pos(); mRubberBand->setGeometry(QRect(origin, QSize())); mRubberBand->show(); } } break; case QEvent::MouseMove: { if( mRubberBand.isVisible() ) { QMouseEvent* me = static_cast<QMouseEvent*>( event ); mRubberBand->setGeometry(QRect(mOrigin, me->pos()).normalized()); } } break; case QEvent::MouseButtonRelease: { if( mRubberBand.isVisible() ) { mRubberBand->hide(); QList<QGraphicsItem*> items = this->items( mRubberBand->geometry() ); QRectF itemsRect; foreach( QGraphicsItem* item, items ) itemsRect |= item->screneRect(); if( itemsRect.isValid() ) this->fitInView( itemsRect ); } } break; } return QGraphicsView::viewportEvent(event); } };