/*graphicstest2.h*/
#ifndef GRAPHICSTEST2_H
#define GRAPHICSTEST2_H
#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsItem>
namespace Ui {
class GraphicsTest2;
}
class MyQGraphicsScene;
Q_OBJECT
public:
GraphicsTest2
(QWidget *parent
= 0);
~GraphicsTest2();
//void zoomTheItem(QGraphicsSceneMouseEvent *event);
//void makeItemNormal(QGraphicsSceneMouseEvent *event);
protected:
private:
Ui::GraphicsTest2 *ui;
MyQGraphicsScene *myScene;
};
Q_OBJECT
public:
MyQGraphicsScene();
protected:
private:
//GraphicsTest2 *myParent;
bool isItemNotScaled;
};
#endif // GRAPHICSTEST2_H
/* graphicstest2.cpp*/
#include "graphicstest2.h"
#include "ui_graphicstest2.h"
GraphicsTest2
::GraphicsTest2(QWidget *parent
) : ui(new Ui::GraphicsTest2)
{
myScene = new MyQGraphicsScene;
myScene->setSceneRect(0,80,800,460);
item1
->setPixmap
(QPixmap("./myimages/addressbook.png"));
item1->setPos(50,130);
item2
->setPixmap
(QPixmap("./myimages/flash.png"));
item2->setPos(250,130);
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
) {
switch (e->type()) {
ui->retranslateUi(this);
break;
default:
break;
}
}
//void GraphicsTest2::zoomTheItem(QGraphicsSceneMouseEvent *event){}
//void GraphicsTest2::makeItemNormal(QGraphicsSceneMouseEvent *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;
}
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;
}
/*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(QGraphicsSceneMouseEvent *event){}
//void GraphicsTest2::makeItemNormal(QGraphicsSceneMouseEvent *event){}
void MyQGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *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(QGraphicsSceneMouseEvent *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;
}
To copy to clipboard, switch view to plain text mode
Bookmarks