{
Q_OBJECT
public:
private:
...
public slots:
void zoomIn ();
void zoomOut();
private:
GridScene* m_scene;
int m_minScaleFactor;
int m_maxScaleFactor;
int m_curScaleFactor;
};
class GridView : public QGraphicsView
{
Q_OBJECT
public:
GridView(QWidget* parent = 0);
private:
...
public slots:
void zoomIn ();
void zoomOut();
private:
GridScene* m_scene;
int m_minScaleFactor;
int m_maxScaleFactor;
int m_curScaleFactor;
};
To copy to clipboard, switch view to plain text mode
GridView
::GridView(QWidget* parent
){
setGeometry
(QRect(0,
0,
474,
621));
setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOff);
m_minScaleFactor = 0;
m_maxScaleFactor = 500;
m_curScaleFactor = 250;
m_curPanX = 0;
m_curPanY = 0;
m_scene = new GridScene;
this->setScene (m_scene);
m_scene
->setSceneRect
(QRectF(0,
0,
1500,
1500));
setAutoFillBackground(true);
// setViewportUpdateMode(QGraphicsView::FullViewPortUpdate);
}
void GridView::zoomIn ()
{
qreal scale = qPow(qreal(2), (m_curScaleFactor - 250) / qreal(50));
QTransform transform;
transform.scale(scale, scale);
m_curScaleFactor = transform.m11();
}
{
if (event->button() == Qt::LeftButton)
{
m_pan = true;
m_panStartX = event->x();
m_panStartY = event->y();
setCursor(Qt::ClosedHandCursor);
event->accept();
return;
}
event->ignore();
}
{
if (m_pan)
{
m_curPanX = (event->x() - m_panStartX);
m_curPanY = (event->y() - m_panStartY);
m_panStartX = event->x();
m_panStartY = event->y();
event->accept();
return;
}
event->ignore();
}
{
if (event->button() == Qt::LeftButton)
{
m_pan = false;
setCursor(Qt::ArrowCursor);
m_curPanX = event->x();
m_curPanY = event->y();
setCursor(Qt::OpenHandCursor);
event->accept();
return;
}
event->ignore();
}
GridView::GridView(QWidget* parent)
: QGraphicsView(parent)
{
setGeometry(QRect(0, 0, 474, 621));
setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOff);
setDragMode (QGraphicsView::ScrollHandDrag);
m_minScaleFactor = 0;
m_maxScaleFactor = 500;
m_curScaleFactor = 250;
m_curPanX = 0;
m_curPanY = 0;
m_scene = new GridScene;
this->setScene (m_scene);
m_scene->setSceneRect(QRectF(0, 0, 1500, 1500));
setBackgroundRole(QPalette::Base);
setAutoFillBackground(true);
// setViewportUpdateMode(QGraphicsView::FullViewPortUpdate);
}
void GridView::zoomIn ()
{
qreal scale = qPow(qreal(2), (m_curScaleFactor - 250) / qreal(50));
QTransform transform;
transform.scale(scale, scale);
m_curScaleFactor = transform.m11();
}
void GridView::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
m_pan = true;
m_panStartX = event->x();
m_panStartY = event->y();
setCursor(Qt::ClosedHandCursor);
event->accept();
return;
}
event->ignore();
}
void GridView::mouseMoveEvent(QMouseEvent *event)
{
if (m_pan)
{
m_curPanX = (event->x() - m_panStartX);
m_curPanY = (event->y() - m_panStartY);
m_panStartX = event->x();
m_panStartY = event->y();
event->accept();
return;
}
event->ignore();
}
void GridView::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
m_pan = false;
setCursor(Qt::ArrowCursor);
m_curPanX = event->x();
m_curPanY = event->y();
setCursor(Qt::OpenHandCursor);
event->accept();
return;
}
event->ignore();
}
To copy to clipboard, switch view to plain text mode
#include <QGraphicsScene>
{
public:
GridScene();
protected:
private:
...
};
#include <QGraphicsScene>
class GridScene : public QGraphicsScene
{
public:
GridScene();
protected:
void drawBackground(QPainter *painter, const QRectF &rect);
private:
...
};
To copy to clipboard, switch view to plain text mode
GridScene::GridScene()
{
minX = 0.0;
maxX = 10.0;
numXTicks = 12;
minY = 0.0;
maxY = 10.0;
numYTicks = 12;
}
{
QRect rect1
(Margin, Margin,
width() - 2 * Margin, height() - 2 * Margin);
if (!rect1.isValid())
return;
for (int i = 0; i <= numXTicks; ++i) {
int x = rect1.left() + (i * (rect1.width() - 1) / numXTicks);
double label = minX + (i * spanX() / numXTicks);
painter->drawLine(x, rect1.top(), x, rect1.bottom());
painter->drawLine(x, rect1.bottom(), x, rect1.bottom() + 5);
painter->drawText(x - 50, rect1.bottom() + 5, 100, 20,
Qt::AlignHCenter | Qt::AlignTop,
}
for (int j = 0; j <= numYTicks; ++j) {
int y = rect1.bottom() - (j * (rect1.height() - 1)/ numYTicks);
double label = minY + (j * spanY()
/ numYTicks);
painter->drawLine(rect1.left(), y, rect1.right(), y);
painter->drawLine(rect1.left() - 5, y, rect1.left(), y);
painter->drawText(rect1.left() - Margin, y - 10, Margin - 5, 20,
Qt::AlignRight | Qt::AlignVCenter,
}
painter->drawRect(rect1.adjusted(0, 0, -1, -1));
}
GridScene::GridScene()
{
minX = 0.0;
maxX = 10.0;
numXTicks = 12;
minY = 0.0;
maxY = 10.0;
numYTicks = 12;
}
void GridScene::drawBackground(QPainter *painter, const QRectF &rect)
{
QRect rect1(Margin, Margin,
width() - 2 * Margin, height() - 2 * Margin);
if (!rect1.isValid())
return;
for (int i = 0; i <= numXTicks; ++i) {
int x = rect1.left() + (i * (rect1.width() - 1) / numXTicks);
double label = minX + (i * spanX() / numXTicks);
painter->setPen(QPen(QColor(Qt::darkBlue)));
painter->drawLine(x, rect1.top(), x, rect1.bottom());
painter->setPen(QPen(QColor(Qt::darkBlue)));
painter->drawLine(x, rect1.bottom(), x, rect1.bottom() + 5);
painter->drawText(x - 50, rect1.bottom() + 5, 100, 20,
Qt::AlignHCenter | Qt::AlignTop,
QString::number(label));
}
for (int j = 0; j <= numYTicks; ++j) {
int y = rect1.bottom() - (j * (rect1.height() - 1)/ numYTicks);
double label = minY + (j * spanY()
/ numYTicks);
painter->setPen(QPen(QColor(Qt::darkBlue)));
painter->drawLine(rect1.left(), y, rect1.right(), y);
painter->setPen(QPen(QColor(Qt::darkBlue)));
painter->drawLine(rect1.left() - 5, y, rect1.left(), y);
painter->drawText(rect1.left() - Margin, y - 10, Margin - 5, 20,
Qt::AlignRight | Qt::AlignVCenter,
QString::number(label));
}
painter->drawRect(rect1.adjusted(0, 0, -1, -1));
}
To copy to clipboard, switch view to plain text mode
Bookmarks