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 ?
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 ?