Graphics View Panning ,zooming
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
Code:
main.cpp:
int main(int argc, char *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:
{
setBackgroundBrush(Qt::blue);
setSceneRect(-30000, -30000, 60000, 60000);
}
GvHmiScene::~GvHmiScene()
{
}
{
qDebug("The cursor is at position %f and %f in the Scene",p.rx(),p.ry());
}
View.cpp:
GvHmiView::GvHmiView()
{
/*~ code added create scene inside view nithya */
GvHmiScene *scene = new GvHmiScene(this);
setCacheMode(CacheBackground);
// QGraphicsTextItem * item1 = scene->addText("Hello World");
// item1->setPos(0,0);
pen1.setColor(Qt::red);
item3->setPos(0,0);
// scene->addLine(0,0, 10,10, pen1);
// setAttribute (Qt::WA_SetCursor);
// setCursor(Qt::CrossCursor);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setAlignment(Qt::AlignCenter);
// setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
setScene(scene);
enablePan(true);
}
GvHmiView :: ~GvHmiView()
{
}
{
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);
}
[B]void GvHmiView::enableDisablePan(bool enable)
{
if(true == enable)
setDragMode(ScrollHandDrag);
else
{
setAlignment(Qt::AlignCenter);
setDragMode(NoDrag);
scale(1,1);
update();
}
}[/B]
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 ?
Re: Graphics View Panning ,zooming
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:
Code:
centerOn(0,0)
resetMatrix ()
should do it.
Re: Graphics View Panning ,zooming
Hey thank you thats works fine :)
Re: Graphics View Panning ,zooming
Or you can have a look at QGraphicsView::fitInView