How to create of Viewport size on Rectangle selection



  • How to change the viewport size in the view on Rubber band selection of the items.
    only view should be zoomed with out change in any items size or positions.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What view are you talking about ?



  • here i am going to use ViewPort in QGraphicsView.

    all items which intersects the rubberband drag should be drawn inside the viewport of the viewscreen.


  • Moderators

    @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);
        }
    };
    
    


  • thanks its works fine


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.