[Qt-interest] QGraphicsView Scroll problem.

Roland Labrecque roland at ics.com
Tue Feb 17 20:41:55 CET 2009


Sorry. Here is the example code.

#include <QtGui>

class MyView : public QGraphicsView
{
public:
    MyView() : QGraphicsView() {}
    ~MyView() {};

protected:
    void resizeEvent ( QResizeEvent * event );
    void fitVerticalInView();
};

void MyView::resizeEvent ( QResizeEvent * event )
{   
  fitVerticalInView();
  QGraphicsView::resizeEvent(event);
}

// Vertically transform the scene as the view is resized (Do not 
transform horizontally).
void MyView::fitVerticalInView()
{
    if (!scene())
        return;

    // Reset the view scale to 1:1.
    QRectF unity = matrix().mapRect(QRectF(0, 0, 1, 1));
    if (unity.isEmpty())
        return;
    scale(1 / unity.width(), 1 / unity.height());

    // Find the ideal x / y scaling ratio to fit \a rect in the view.
    int margin = 2;
    QRectF viewRect = viewport()->rect().adjusted(margin, margin, 
-margin, -margin);
    if (viewRect.isEmpty())
        return;
    QRectF sceneRect(sceneRect());
    if (sceneRect.isEmpty())
        return;
    qreal xratio = 1;
        //qreal xratio = viewRect.width() / sceneRect.width();
    qreal yratio = viewRect.height() / sceneRect.height();

    // Scale and center on the center of \a rect.
    scale(xratio, yratio);

    horizontalScrollBar()->setValue(horizontalScrollBar()->maximum());

}

int main (int argc, char **argv)
{
   QApplication app(argc,argv);

   QGraphicsScene* s= new QGraphicsScene();
     s->setBackgroundBrush(Qt::lightGray);
   s->setSceneRect(QRectF(0,0,200,100));
   s->addLine(200-0.5,0,200-0.5,100,QPen(Qt::red));

   MyView* v = new MyView();
    v->setAlignment(Qt::AlignRight);

     // NOTE: Things behave as expected if we set the policy to 
Qt::ScrollBarAsNeeded     
     //v->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
     v->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

    v->setScene(s);

   v->show();
   app.exec();

}


Roland Labrecque wrote:
> I am implementing a view that scales the scene vertically (but not 
> horizontally) when the view resizes and has its horizontal scroll bar 
> policy set to always on.
>
> The problem I'm seeing is that when the view is resized to the point 
> where the horizontal scroll bar is needed the view won't use the space 
> alloted for the vertical scroll bar which is hidden.
> I've included a small example showing the problem. In the example the 
> red line at the right of the scene should always be at the right side 
> of the viewport but when the horizontal scroll bar kicks in the 
> line(and scene) step in the width of a a vertical scroll bar.
>
> If you set  v->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded) it 
> works as expected but I need the scroll bar always on.
>
> Any ideas or workarounds?
>
> FYI. I have tried 4.4.3 and 4.5.0-rc1 on XP with the same results.
>
> Roland
>
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest




More information about the Qt-interest-old mailing list