C++ (Qt)QPixmap map; //так же делаю view->setDragMode(QGraphicsView::ScrollHandDrag); view->centerOn(0,0);// карта рисутется от левого верхнего угла сцены - все ок.//но если подвигать скроллы карта рисуется все время угла видимой области, а не от угла 0,0 сцены.//(делал и Scene::drawBackground) NetView::NetView(QGraphicsScene *scene, QWidget *parent) : QGraphicsView(scene, parent){ QString name = "C:/MAP1.bmp"; map.load(name);} void NetView::drawBackground(QPainter *painter, const QRectF &rect){ painter->drawPixmap(rect.left(),rect.top(),map);}
C++ (Qt)void cene::drawBackground(QPainter *painter, const QRectF &rect){ int w=img.width(); int h=img.height(); QRectF r; r.setRect(0,0,w,h); painter->drawImage(r,img); }
C++ (Qt)void MyScene::drawBackground( QPainter* p, const QRectF& clip ){ if( !bg_image_ ) { QGraphicsScene::drawBackground( p, clip ); return; } p->eraseRect( clip ); QMatrix wm( p->worldMatrix() ); if( wm.m11() != 1 && clip.intersects( bg_image_->rect() ) ) { QImage img; QRectF c( clip ); if( wm.m11() > 1 ) c.adjust( -1, -1, 1, 1 ); qreal cx = c.x(); qreal cy = c.y(); qreal cw = qMax( 16., c.width() ); qreal ch = qMax( 16., c.height() ); if( cx < 0 ) { cw += cx; cx = 0; } if( cy < 0 ) { ch += cy; cy = 0; } cw = qMin( cw, bg_image_->width() - cx ); ch = qMin( ch, bg_image_->height() - cy ); img = bg_image_->copy( QRectF( cx, cy, cw, ch ).toRect() ); cw = qRound( cw * wm.m11() ); ch = qRound( ch * wm.m11() ); img = img.scaled( (int) cw, (int) ch, Qt::IgnoreAspectRatio, /*wm.m11() > .8 ? Qt::SmoothTransformation :*/ Qt::FastTransformation ); p->setWorldMatrix( QMatrix( 1, 0, 0, 1, wm.dx(), wm.dy() ) ); p->drawImage( QPointF( cx * wm.m11(), cy * wm.m11() ), img ); p->setWorldMatrix( wm ); } else { p->drawImage( QPointF( clip.x(), clip.y() ), *bg_image_, QRectF( clip.x(), clip.y(), qMin( clip.width(), qreal( bg_image_->width() ) ), qMin( clip.height(), qreal( bg_image_->height() ) ) ) ); }}
double newScale = scale.left(scale.indexOf(tr("%"))).toDouble() / 100.0; QMatrix oldMatrix = view->matrix(); view->resetMatrix(); view->translate(oldMatrix.dx(), oldMatrix.dy()); view->scale(newScale, newScale);
qreal scale;int angle;...slot_scale(){ scale = ...setupMatrix();}slot_rotate(){angle = ...setupMatrix();}setupMatrix(){ QMatrix matrix; matrix.rotate(angle); matrix.scale(scale, scale); view->setMatrix(matrix);}