11

QGraphicsViewQtのサブクラスとサブクラスを使用していQGraphicsItemます。ビューの長方形が変更されたとき、たとえばズームインしたときに、ビュー内のアイテムのグラフィック表現を拡大縮小しない方法はありますか。デフォルトの動作では、アイテムはビューの長方形に対して拡大縮小されます。

ビューを拡大するときに拡大縮小されない細い長方形で表される2Dポイントを視覚化したいと思います。頂点が常に同じサイズで表示される、一般的な3Dモデリングソフトウェアを参照してください。

ありがとう!

4

5 に答える 5

12

QGraphicItem'sフラグをtrueに設定QGraphicsItem::ItemIgnoresTransformationsしても機能しませんか?

于 2009-08-19T18:44:03.513 に答える
10

私も同じ問題に直面し、それを理解するのに少し時間がかかりました。これが私がそれを解決した方法です。

QGraphicsItemクラスを拡張し、paint()をオーバーライドします。paint()内で、変換のスケーリング係数を1(m11とm22)にリセットし、リセットする前にm11(xスケーリング係数)とm22(yスケーリング係数)を保存します。次に、通常どおりに描画しますが、xにm11を掛け、yにm22を掛けます。これにより、デフォルトの変換での描画が回避されますが、シーンの変換に従って位置が明示的に計算されます。

void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget)
{
    QTransform t = painter->transform();
    qreal m11 = t.m11(), m22 = t.m22();
    painter->save(); // save painter state
    painter->setTransform(QTransform(1, t.m12(), t.m13(),
                                     t.m21(), 1, t.m23(), t.m31(),
                                     t.m32(), t.m33()));
    int x = 0, y = 0; // item's coordinates
    painter->drawText(x*m11, y*m22, "Text"); // the text itself will not be scaled, but when the scene is transformed, this text will still anchor correctly
    painter->restore(); // restore painter state
}

次のコードブロックは、デフォルトの変換で描画しています

void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget)
{
    int x = 0, y = 0;
    painter->drawText(x, y, "Text"); 
}

両方を試して違いを確認できます。お役に立てれば。

于 2014-07-21T20:22:44.350 に答える
2

これはどう:

#include <QtGui/QApplication>
#include <QtGui/QGraphicsScene>
#include <QtGui/QGraphicsView>
#include <QtGui/QGraphicsRectItem>

int main(int argc, char* argv[]) {
    QApplication app(argc, argv);
    QGraphicsScene scene;
    scene.addText("Hello, world!");
    QRect rect(50, 50, 100, 100);
    QGraphicsRectItem* recti = scene.addRect(rect);
    QGraphicsView view(&scene);

    // Set scale for the view
    view.scale(10.0, 5.0);

    // Set the inverse transformation for the item
    recti->setTransform(view.transform().inverted());

    view.show();
    return app.exec();
}

ご覧のとおり、テキストは拡大されていますが、長方形は拡大されていません。これにより、長方形のスケーリングだけでなく、その他の変換も妨げられることに注意してください。

于 2009-08-03T21:50:07.860 に答える
2

次の解決策は私にとって完璧に機能しました:

void MyDerivedQGraphicsItem::paint(QPainter *painter, const StyleOptionGraphicsItem *option, QWidget *widget)
{
    double scaleValue = scale()/painter->transform().m11();
    painter->save();
    painter->scale(scaleValue, scaleValue);
    painter->drawText(...);
    painter->restore();
    ...
}

また、scaleValueに、保存/復元環境の外部でサイズを一定に保ちたい他の測定値を掛けることもできます。

    QPointF ref(500, 500);
    QPointF vector = scaleValue * QPointF(100, 100);
    painter->drawLine(ref+vector, ref-vector);
于 2016-11-14T01:04:24.117 に答える
1

新しいクラスを派生させてペイント関数を再実装すると、できることがわかりました。

void MyDerivedQGraphicsItem::paint(QPainter *painter, 
                                   const QStyleOptionGraphicsItem *option, 
                                   QWidget *widget)
{
  double scaleValue = scale();
  double scaleX = painter->transform().m11();
  setScale(scaleValue / scaleX);
  QGraphicsSvgItem::paint(painter,option,widget);
}

これは私がこれまでに見つけたそれを行うための最良の方法ですが、私はまだいじくり回しています。

于 2013-03-28T19:54:18.787 に答える