PDA

View Full Version : Zoom functionality on pushbutton



chirudi
7th January 2013, 08:09
Dear All,
I have to do zoom functionality in my project using two pushbutton zoomin and zoomout . in my graphicsview .......

can anybody help me ........

thanking you .......

Santosh Reddy
7th January 2013, 08:30
It is not much different, you need to apply transformations as required. Only difference is instead of wheel event as discussed in your earlier thread (http://www.qtcentre.org/threads/52603-Zoom-effect-by-mouse-Wheel-in-QGraphicsview), you need to trigger based of push button clicked signals

chirudi
7th January 2013, 08:36
thank you boss
why i am not able to do zoom from my mouse pointer i have to do zoom to particular object ............ and i am new to it so please help me ......

Santosh Reddy
7th January 2013, 09:45
Run this example, and try to point the mouse cursor at the centre of any colored circle/rectangle and sroll, then see if it zooms to the centre of same colored circle/rectange?


//main.cpp
#include <QWheelEvent>
#include <QMainWindow>
#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>

class View : public QGraphicsView
{
Q_OBJECT

public:
explicit View(QWidget *parent) : QGraphicsView(parent),
h11(1.0), h12(0), h21(0), h22(1.0) {}

protected:
void wheelEvent(QWheelEvent *event);

private:
qreal h11;
qreal h12;
qreal h21;
qreal h22;
};

void View::wheelEvent(QWheelEvent *event)
{
if(qApp->keyboardModifiers() & Qt::ControlModifier)
{
const int degrees = event->delta() / 8;
int steps = degrees / 15;

double scaleFactor = 1.0;
const qreal minFactor = 1.0;
const qreal maxFactor = 10.0;
if(steps > 0)
{
h11 = (h11 >= maxFactor) ? h11 : (h11 + scaleFactor);
h22 = (h22 >= maxFactor) ? h22 : (h22 + scaleFactor);
}
else
{
h11 = (h11 <= minFactor) ? minFactor : (h11 - scaleFactor);
h22 = (h22 <= minFactor) ? minFactor : (h22 - scaleFactor);
}

setTransformationAnchor(AnchorUnderMouse);
setTransform(QTransform(h11, h12, h21, h22, 0, 0));
}
else
{
QGraphicsView::wheelEvent(event);
}
}

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow mainWindow;
QColor colors[] = {Qt::red, Qt::blue, Qt::black, Qt::green, Qt::yellow };
int color = 0;

QGraphicsView *view = new View(&mainWindow);
QGraphicsScene *scene = new QGraphicsScene(&mainWindow);
QPen pen;
pen.setWidth(4);

for(int i = 0; i < 2000; i += 150)
{
for(int j = 0; j < 2000; j += 150)
{
pen.setColor(colors[color++ % 5]);
scene->addRect(i, j, 100, 100, pen);
scene->addEllipse(i, j, 100, 100, pen);
}
}

view->setScene(scene);
mainWindow.setCentralWidget(view);

mainWindow.showMaximized();

return a.exec();
}

#include "main.moc"

chirudi
7th January 2013, 10:23
Dear ,
All work well but i am not able to call Qgraphicsview Wheel event which is in else function............. i am getting
error: ‘virtual void QGraphicsView::wheelEvent(QWheelEvent*)’ is protected

Santosh Reddy
7th January 2013, 10:43
What version of Qt?

I example I posted compiles and run with Qt 4.8.1 and MinGw 4.6.2.

Show us what you did?

chirudi
8th January 2013, 04:23
i have QT 4.7.3(i have develop application in Linux Platform)
In my project i have taken QWidget application and on that i have added Qgraphicsview and Qgraphicsscene is added to graphicsview. and i am drawing the object in graphicsview using painterpath i have widget of size 1280x1024 . and graphicsview of 1141x991

Santosh Reddy
8th January 2013, 07:02
and what is graphicsscene rect size?