[Development] scaling the background of QGraphicsView or QGraphicsScene

心翔 assangema at gmail.com
Sun Oct 2 03:18:56 CEST 2016


Hi,
I subclass the QGraphicsView to reimplement drawBackground and wheelEvent  and
want to get  this effect:


but when I wheel the mouse, the background point will scale also。

​
how can implement that it only scale t*he spacing between points but not
the point itself,like this:*



*​*

*the code:*

> void SchView::wheelEvent(QWheelEvent *event)
>
> {
>
>     qreal scaleFactor = (pow((double)2, event->delta() / 240.0));
>
>     qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
>
>
>     if (factor < 0.07 || factor > 100)
>
>         return;
>
>
>     scale(scaleFactor, scaleFactor);
>
> }
>
>
void SchView::drawBackground(QPainter *painter, const QRectF &rect)
>
> {
>
>     int gridSize = 20;
>
>     QPen pen(QColor(Qt::white));
>
>     painter->setPen(pen);
>
>
>     qreal left = int(rect.left()) - (int(rect.left()) % gridSize);
>
>     qreal top = int(rect.top()) - (int(rect.top()) % gridSize);
>
>
>     QVarLengthArray<QPointF, 800> points;
>
>     for (qreal x = left; x < rect.right(); x += gridSize){
>
>         for (qreal y = top; y < rect.bottom(); y += gridSize){
>
>             points.append(QPointF(x,y));
>
>         }
>
>     }
>
>     painter->drawPoints(points.data(), points.size());
>
> }
>
>
>


*Any suggestions are greatly appreciated.*
*thanks*



------------------------------
itviewer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20161002/2bc5fcd4/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 1.png
Type: image/png
Size: 356 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/development/attachments/20161002/2bc5fcd4/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 1.png
Type: image/png
Size: 470 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/development/attachments/20161002/2bc5fcd4/attachment-0001.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 1.png
Type: image/png
Size: 318 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/development/attachments/20161002/2bc5fcd4/attachment-0002.png>


More information about the Development mailing list