PDA

View Full Version : Graphics View Panning ,zooming



linuxdev
26th December 2008, 15:34
Hi,

I have written a small application using Graphics View.
I have drawn a circle at the (0,0) in the scene.

Now i want to do the following:
a. ZoomIn/ZoomOut, the circle should always be the center.
b. Pan around using mouse.
c. Remove all the panning/zooming related activities (if present) and restore to the default state (without any Panning/Zoom) on a click of button .

I have done points a) and b) , but once i have panned around the view , i dont know how to get back to the default view, i.e. the view should be centered at scene's (0,0) without any scaling

This is my code for view and scene class




main.cpp:
int main(int argc, char *argv[])
{

QApplication a(argc, argv);
GvHmiMainWindow Gvhmi;
/*~ or this way-nithya~*/
/* by with out using pointers~*/


Gvhmi.show();
return a.exec();

}

mainwindow.cpp
GvHmiMainWindow::GvHmiMainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::GvHmiMainWindowClass)
{
ui->setupUi(this);
/*~either this way-nithya~*/
/* by using pointers~*/
viewGv = new GvHmiView();
setCentralWidget(viewGv);
QObject::connect(ui->actionPan,SIGNAL(triggered ( bool ) ),this,SLOT(slotactionPan(bool)));
setWindowTitle(tr("Graphics View"));
}
void GvHmiMainWindow::slotactionPan(bool )
{
QMessageBox::warning(0,"Graphics View","Pan action disabled");
viewGv->enablePan(false);

}

Scene.cpp:
GvHmiScene::GvHmiScene(QObject *parent):QGraphicsScene(parent)
{
setBackgroundBrush(Qt::blue);
setItemIndexMethod(QGraphicsScene::NoIndex);
setSceneRect(-30000, -30000, 60000, 60000);

}

GvHmiScene::~GvHmiScene()
{



}


void GvHmiScene::mouseMoveEvent(QGraphicsSceneMouseEven t *event)
{

QPointF p = event->scenePos();
qDebug("The cursor is at position %f and %f in the Scene",p.rx(),p.ry());
QGraphicsScene::mouseMoveEvent(event);
}

View.cpp:

GvHmiView::GvHmiView()
{
/*~ code added create scene inside view nithya */
GvHmiScene *scene = new GvHmiScene(this);

setCacheMode(CacheBackground);
setRenderHint(QPainter::Antialiasing);

// QGraphicsTextItem * item1 = scene->addText("Hello World");
// item1->setPos(0,0);

QPen pen1;
pen1.setColor(Qt::red);

MyItem *item3 = new MyItem((QGraphicsItem *)0, scene, 0);
item3->setPos(0,0);
// scene->addLine(0,0, 10,10, pen1);



// setAttribute (Qt::WA_SetCursor);
// setCursor(Qt::CrossCursor);


setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;

setTransformationAnchor(QGraphicsView::AnchorViewC enter);
setAlignment(Qt::AlignCenter);
setResizeAnchor(QGraphicsView::AnchorViewCenter);
// setTransformationAnchor(QGraphicsView::AnchorUnder Mouse);

setScene(scene);
enablePan(true);

}


GvHmiView :: ~GvHmiView()
{


}


void GvHmiView::wheelEvent(QWheelEvent *event)

{

scaleView(pow((double)2, event->delta() / 240.0));

}



void GvHmiView::scaleView(qreal scaleFactor)

{

qreal factor = matrix().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
if (factor < 0.07 || factor > 100)
return;
scale(scaleFactor, scaleFactor);

}


void GvHmiView::enableDisablePan(bool enable)
{
if(true == enable)
setDragMode(ScrollHandDrag);
else
{
setTransformationAnchor(QGraphicsView::AnchorViewC enter);
setAlignment(Qt::AlignCenter);
setResizeAnchor(QGraphicsView::AnchorViewCenter);

setTransformationAnchor(QGraphicsView::AnchorViewC enter);
setDragMode(NoDrag);
scale(1,1);
update();

}

}


GvHmiView::enableDisablePan(bool enable) is the function where i am trying to get the view to show default screen , without any zoom/pan.
Can anyone suggest what going wrong here ?

robertson1
27th December 2008, 15:19
For scaling you need to scale with values which will scale from your views current scale to a your desired scale. (scaling by 1,1) wont scale anything.

For panning QGraphicsView::ScrollHandDrag is just setting the views horizontal and vertical scrollbars. You can manually set their values or alternatively centerOn(0,0) will center your view on 0,0.

for your example If all you want to do is put the view back to the way it was originally was:


centerOn(0,0)
resetMatrix ()

should do it.

linuxdev
29th December 2008, 05:17
Hey thank you thats works fine :)

aamer4yu
29th December 2008, 07:17
Or you can have a look at QGraphicsView::fitInView