PDA

View Full Version : I can't ZOOM THE ICONS can any body help me..!



jthacker
1st June 2010, 09:33
hi guys,
i want to zoom particular application icon but can't do can you help me in this..!
i can see the the icon but cant zoom it.
in the zoomTheItem() function i'm getting the x=0 and y=0.
can u help me..!

in the ui file i have put the graphicsView




/*graphicstest2.h*/

#ifndef GRAPHICSTEST2_H
#define GRAPHICSTEST2_H

#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsItem>

namespace Ui {
class GraphicsTest2;
}

class MyQGraphicsScene;

class GraphicsTest2 : public QMainWindow {
Q_OBJECT
public:
GraphicsTest2(QWidget *parent = 0);
~GraphicsTest2();

void zoomTheItem(QGraphicsSceneMouseEvent *event);
void makeItemNormal(QGraphicsSceneMouseEvent *event);


protected:
void changeEvent(QEvent *e);


private:
Ui::GraphicsTest2 *ui;
MyQGraphicsScene *myScene;
QGraphicsPixmapItem *item1;

QGraphicsItem *currentItem;
bool isItemNotScaled;



};

class MyQGraphicsScene :public QGraphicsScene {
Q_OBJECT
public:
MyQGraphicsScene(GraphicsTest2 *parent);
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

private:
GraphicsTest2 *myParent;


};

#endif // GRAPHICSTEST2_H

/*graphicstest2.cpp*/

#include "graphicstest2.h"
#include "ui_graphicstest2.h"

GraphicsTest2::GraphicsTest2(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::GraphicsTest2)
{
isItemNotScaled = true;

myScene = new MyQGraphicsScene(this);
myScene->setSceneRect(0,80,800,460);
item1 = new QGraphicsPixmapItem;
item1->setPixmap(QPixmap("./myimages/addressbook.png"));


myScene->addItem(item1);
item1->setPos(50,130);

ui->setupUi(this);
ui->graphicsView->setScene(myScene);
}

GraphicsTest2::~GraphicsTest2()
{
delete ui;
}

void GraphicsTest2::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}

void GraphicsTest2::zoomTheItem(QGraphicsSceneMouseEven t *event){
printf("\n zoomIn x:%d,y=%d",event->pos().x(),event->pos().y());fflush(stdout);
currentItem = ui->graphicsView->itemAt(event->pos().x(),event->pos().y());

if(!currentItem){
return;
}

if(isItemNotScaled){
currentItem->setScale(1.25);
currentItem->moveBy(-10,-10);
}
isItemNotScaled = false;



}

void GraphicsTest2::makeItemNormal(QGraphicsSceneMouseE vent *event){
if(!isItemNotScaled){
currentItem->setScale(1);
currentItem->moveBy(10,10);
}
isItemNotScaled = true;
printf("\n zoomOut");fflush(stdout);
}

void MyQGraphicsScene::mousePressEvent(QGraphicsSceneMo useEvent *event){
myParent->zoomTheItem(event);
}

void MyQGraphicsScene::mouseReleaseEvent(QGraphicsScene MouseEvent *event){

myParent->makeItemNormal(event);

}


MyQGraphicsScene::MyQGraphicsScene(GraphicsTest2 *parent){
myParent = parent;

}


/*main.cpp*/

#include <QtGui/QApplication>
#include "graphicstest2.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
GraphicsTest2 w;
w.show();
return a.exec();
}




note: Qt 4.6.2
Qt Creator 1.3.1



please help

jthacker
1st June 2010, 10:34
help please...!
thankx

aamer4yu
1st June 2010, 11:02
First please re-consider you design. It seems quite complex for a simple task.
You need not pass the event to myParent.. you can handle items from within the scene. So use of GraphicsTest2 *myParent; in ur scene class seems irrelevant.

Then coming how to zoom in / out . You just need to use QGraphicsView::fitInView. For zoom in, you can pass the item itself...for zoom out, manipulate the scene rect you want to show and call fitInview.

jthacker
1st June 2010, 11:53
hey,
i have modified the this in the simpler one...!
and one thing to say that i have 6 to 9 app icons on the scene...!
and i want to zoom one app icon at a time to the factor 1.25 ....!
here is the code:
i'm still getting the x=0,y=0;
i have tried to give manual value in
in mousePressEvent()
currentItem=itemAt(55,135);

i'm not getting the item at given coordinate.


/*graphicstest2.h*/

#ifndef GRAPHICSTEST2_H
#define GRAPHICSTEST2_H

#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsItem>

namespace Ui {
class GraphicsTest2;
}

class MyQGraphicsScene;

class GraphicsTest2 : public QMainWindow {
Q_OBJECT
public:
GraphicsTest2(QWidget *parent = 0);
~GraphicsTest2();

//void zoomTheItem(QGraphicsSceneMouseEvent *event);
//void makeItemNormal(QGraphicsSceneMouseEvent *event);


protected:
void changeEvent(QEvent *e);


private:
Ui::GraphicsTest2 *ui;
MyQGraphicsScene *myScene;
QGraphicsPixmapItem *item1;
QGraphicsPixmapItem *item2;
QGraphicsPixmapItem *item3;

};

class MyQGraphicsScene :public QGraphicsScene {
Q_OBJECT
public:
MyQGraphicsScene();
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

private:
//GraphicsTest2 *myParent;
QGraphicsItem *currentItem;
bool isItemNotScaled;


};

#endif // GRAPHICSTEST2_H


/* graphicstest2.cpp*/
#include "graphicstest2.h"
#include "ui_graphicstest2.h"

GraphicsTest2::GraphicsTest2(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::GraphicsTest2)
{


myScene = new MyQGraphicsScene;
myScene->setSceneRect(0,80,800,460);
item1 = new QGraphicsPixmapItem;
item1->setPixmap(QPixmap("./myimages/addressbook.png"));
item1->setPos(50,130);

item2 = new QGraphicsPixmapItem;
item2->setPixmap(QPixmap("./myimages/flash.png"));
item2->setPos(250,130);

item3 = new QGraphicsPixmapItem;
item3->setPixmap(QPixmap("./myimages/games.png"));
item3->setPos(400,130);

myScene->addItem(item1);
myScene->addItem(item2);
myScene->addItem(item3);


ui->setupUi(this);
ui->graphicsView->setScene(myScene);
}

GraphicsTest2::~GraphicsTest2()
{
delete ui;
}

void GraphicsTest2::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}

//void GraphicsTest2::zoomTheItem(QGraphicsSceneMouseEven t *event){}
//void GraphicsTest2::makeItemNormal(QGraphicsSceneMouseE vent *event){}

void MyQGraphicsScene::mousePressEvent(QGraphicsSceneMo useEvent *event){
//yParent->zoomTheItem(event);

printf("\n zoomIn x:%f,y=%f",event->pos().x(),event->pos().y());fflush(stdout);

currentItem = itemAt(event->pos().x(),event->pos().y());

if(!currentItem){
return;
}

if(isItemNotScaled){
currentItem->setScale(1.25);
currentItem->moveBy(-10,-10);
}
isItemNotScaled = false;

}

void MyQGraphicsScene::mouseReleaseEvent(QGraphicsScene MouseEvent *event){
printf("\n zoomIn x:%f,y=%f",event->pos().x(),event->pos().y());fflush(stdout);
//myParent->makeItemNormal(event);

if(!isItemNotScaled){
currentItem->setScale(1);
currentItem->moveBy(10,10);
}
isItemNotScaled = true;
printf("\n zoomOut");fflush(stdout);

}


MyQGraphicsScene::MyQGraphicsScene(){
// myParent = parent;
isItemNotScaled = true;

}






plz help..!

jthacker
1st June 2010, 12:17
hey aamer4yu where are you..!
can anybody help in this...!

thanx

Lykurg
1st June 2010, 13:14
hey aamer4yu where are you..!
Hey! Why you can't wait more then 25 minutes? There is no need to beg for answers every hour. Better use the time while you are waiting to read the documentation about QGraphicsSceneMouseEvent. You might want have a closer look at QGraphicsSceneMouseEvent::scenePos().

jthacker
1st June 2010, 13:50
hey ..!
thanx for your nice feedback..! i got the solution for the zooming effect...!
But i in this i'm facing another problem when i run the following modified code..!

when i press mouse: mousePressEvent() is called and icon is getting zoomed but
when i release the mouse : mouseReleaseEvent() is not getting called...! and item remains in zoomed position

but when i press & release mouse out side the GraphicsView area at that time the both function are called...!


/*graphicstest2.cpp*/

#ifndef GRAPHICSTEST2_H
#define GRAPHICSTEST2_H

#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsItem>

namespace Ui {
class GraphicsTest2;
}

class GraphicsTest2 : public QMainWindow {
Q_OBJECT
public:
GraphicsTest2(QWidget *parent = 0);
~GraphicsTest2();


protected:
void changeEvent(QEvent *e);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);

private:
Ui::GraphicsTest2 *ui;
QGraphicsScene *myScene;
QGraphicsPixmapItem *item1;
QGraphicsPixmapItem *item2;
QGraphicsPixmapItem *item3;

QGraphicsItem *currentItem;

};

#endif // GRAPHICSTEST2_H

/*graphicstest2.cpp*/

#include "graphicstest2.h"
#include "ui_graphicstest2.h"
#include <iostream>
#include <QMouseEvent>
GraphicsTest2::GraphicsTest2(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::GraphicsTest2)
{


myScene = new QGraphicsScene;
myScene->setSceneRect(0,80,800,460);
item1 = new QGraphicsPixmapItem;
item1->setPixmap(QPixmap("./myimages/addressbook.png"));
item1->setPos(50,130);

item2 = new QGraphicsPixmapItem;
item2->setPixmap(QPixmap("./myimages/flash.png"));
item2->setPos(250,130);

item3 = new QGraphicsPixmapItem;
item3->setPixmap(QPixmap("./myimages/games.png"));
item3->setPos(400,130);

myScene->addItem(item1);
myScene->addItem(item2);
myScene->addItem(item3);



ui->setupUi(this);
ui->graphicsView->setScene(myScene);

}

GraphicsTest2::~GraphicsTest2()
{
delete ui;
}

void GraphicsTest2::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}


void GraphicsTest2::mousePressEvent(QMouseEvent *event){

std::cout<<qPrintable(QString("\n main press x=%1,y=%2").arg(event->pos().x()).arg(event->pos().y()));fflush(stdout);


currentItem = ui->graphicsView->itemAt(event->pos().x(),event->pos().y()-80);
if(currentItem)
currentItem->setScale(1.25);



}

void GraphicsTest2::mouseReleaseEvent(QMouseEvent *event){

std::cout<<qPrintable(QString("\n main release x=%1,y=%2").arg(event->pos().x()).arg(event->pos().y()));fflush(stdout);

if(currentItem){
currentItem->setScale(1);
currentItem=0;
}
}




thank you...!

jthacker
2nd June 2010, 07:13
hey can anybody help in this ...!
why the mouseReleaseEvent() is not getting called...!


thank you.
any comments...?