2010-05-25 3 views
2

QGraphicsView에 팬/스케일 작업을 할 것입니다.
QGraphicsView의 설명서를 읽고 ensureVisible()centerOn()과 같은 유틸리티 기능을 참조하십시오.
설명서가 말하는 내용을 이해하지만 실제로 작동하는 예제를 작성할 수는 없습니다.
문제를 이해하기 위해 예제 코드를 작성/제안 해주십시오.QGraphicsView ensureVisible() 및 centerOn()

+0

마우스로 팬 및 크기 조절을 하시겠습니까? 또는보기에 개체 집합을 맞 춥니 다? –

+0

나는 거기에 맞는 작업이있을 것 같아요 또한 마우스로 팬/스케일 작업을 어쩌면. – onurozcelik

답변

3

톤 일정량 뷰를 이동 (의 예를 들어보기의 mouseMoveEvent()), MyView 가정 할 QGraphicsView의 서브 클래스 (나는 그것을 테스트하지 않았고, 다음의 모든 코드를 파이썬에서 이식 된)입니다 :

void MyView::moveBy(QPoint &delta) 
{ 
    QScrollBar *horiz_scroll = horizontalScrollBar(); 
    QScrollBar *vert_scroll = verticalScrollBar(); 
    horiz_scroll->setValue(horiz_scroll.value() - delta.x()); 
    vert_scroll->setValue(vert_scroll.value() - delta.y()); 
} 

좌표 줌과 패닝에 의해 현장에서 지정된 사각형에 맞게 :

void MyView::fit(QRectF &rect) 
{ 
    setSceneRect(rect); 
    fitInView(rect, Qt::KeepAspectRatio); 
} 

장면합니다 (QGraphicsItem::ItemIgnoresTransformations 플래그가 설정)이 아닌 변형 가능한 항목이 포함되어있는 경우, 당신은 이메일을 우선 처리하십시오 이 경우 귀하의 객체의 경계 RECT는 피팅 할 때 가끔 MyView::fit() (예를 들어 정확하게 물체를 맞지 않는 것을 의미 뷰의 줌 레벨에 의존하게에서

/** 
* Compute the bounding box of an item in scene space, handling non 
* transformable items. 
*/ 
QRectF sceneBbox(QGraphicsItem *item, QGraphicsItemView *view=NULL) 
{ 
    QRectF bbox = item->boundingRect(); 
    QTransform vp_trans, item_to_vp_trans; 

    if (!(item->flags() & QGraphicsItem::ItemIgnoresTransformations)) { 
     // Normal item, simply map its bounding box to scene space 
     bbox = item->mapRectToScene(bbox); 
    } else { 
     // Item with the ItemIgnoresTransformations flag, need to compute its 
     // bounding box with deviceTransform() 
     if (view) { 
      vp_trans = view->viewportTransform(); 
     } else { 
      vp_trans = QTransform(); 
     } 
     item_to_vp_trans = item->deviceTransform(vp_trans); 
     // Map bbox to viewport space 
     bbox = item_to_vp_trans.mapRect(bbox); 
     // Map bbox back to scene space 
     bbox = vp_trans.inverted().mapRect(bbox); 
    } 

    return bbox; 
} 

: 엑스트라 단계는 올바른 경계 상자를 계산하기 크게 축소 된보기에서 개체 선택). 더 빠르고 더러운 해결책은 경계 사각형이 자연적으로 "안정화"될 때까지 반복적으로 MyView::fit()으로 전화하는 것입니다.

관련 문제