widget.h
class MyLabel : public QLabel
{
Q_OBJECT
public:
MyLabel(QWidget *parent = 0);
~MyLabel();
void mousePressEvent(QMouseEvent *ev);
};
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
};
widget.cpp
QGraphicsProxyWidget* proxyWidget = NULL;
QGraphicsView *view = NULL;
MyLabel::MyLabel(QWidget *parent) : QLabel(parent)
{
}
void MyLabel::mousePressEvent(QMouseEvent *ev)
{
qreal x_new = 0;
qreal y_new = 0;
qreal x_real = ev->pos().x();;
qreal y_real = ev->pos().y();
QTransform transform = proxyWidget->transform();
transform.scale(2,2);
transform.map(x_real,y_real,&x_new,&y_new);
proxyWidget->setTransform(transform);
view->setSceneRect(x_new-view->width()/2,y_new-view->height()/2,view->width(),view->height());
}
MyLabel::~MyLabel()
{
}
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
MyLabel *label = new MyLabel();
label->setPixmap(QPixmap("..."));
proxyWidget = new QGraphicsProxyWidget();
proxyWidget->setWidget(label);
QGraphicsScene *scene = new QGraphicsScene();
view = new QGraphicsView(this);
scene->addItem(proxyWidget);
view->setScene(scene);
view->resize(width(),height());
}
Widget::~Widget()
{
}