QGraphicsViewでパン/スケールを行う予定です。
だから私はQGraphicsViewのドキュメントを読んで、 ensureVisible()やcenterOn( )のようないくつかのユーティリティ関数を見ました。
ドキュメントの内容は理解できたと思いますが、実際の例を書くことができません。
問題を理解するためのサンプルコードを書いてください/提案してください.
3836 次
1 に答える
3
ビューを特定の量 (たとえば、ビューの ) でパンします(次のコードはすべて Python から移植されたものであり、テストしていません)のサブクラスである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
フラグが設定された) 変形不可能なアイテムが含まれている場合は、正しいバウンディング ボックスを計算するために追加の手順を実行する必要があることに注意してください。
/**
* 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()
オブジェクトが正確に収まらない場合があります (たとえば、大きくズームアウトされたビューから選択したオブジェクトを合わせる場合)。手っ取り早い解決策はMyView::fit()
、バウンディング rect が自然に「安定」するまで繰り返し呼び出すことです。
于 2010-05-25T23:17:24.900 に答える