2009-07-23 6 views
3

ミッション:1つのグラフに2つの線を描画し、1つのグラフに自動クリップを付け、ビットを1つずつ追加します。C++とQt - 2Dグラフィックスの問題

私は何をしていますか? QGraphicsViewから継承したGraphWidgetクラスを作成します。 QGraphicsSceneのメンバーを作成します。 2つのQPainterPathインスタンスを作成し、graphicsSceneに追加します。

次に、graphWidget.Redraw()を呼び出して、両方のインスタンスのQPainterPath.lineTo()を呼び出します。そして私は、グラフィックス・ビューのそのラインの登場を期待していますが、そうはしません。

Qtのドキュメントとフォーラムを読んで疲れました。私は間違って何をしていますか?

+2

これまでに行ったことを理解するのに役立ちます。 –

答えて

4

何が起こっていないのですか?ウィンドウはまったく表示されますか?線は描かれていませんか?それまでの間、このサンプルコードを試してみてください:)編集:更新を表示するように更新されました。

#include ... 

class QUpdatingPathItem : public QGraphicsPathItem { 
    void advance(int phase) { 
     if (phase == 0) 
      return; 
     int x = abs(rand()) % 100; 
     int y = abs(rand()) % 100; 
     QPainterPath p = path(); 
     p.lineTo(x, y); 
     setPath(p); 
    } 
}; 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    QGraphicsScene s; 
    QGraphicsView v(&s); 

    QUpdatingPathItem item; 
    item.setPen(QPen(QColor("red"))); 
    s.addItem(&item); 
    v.show(); 

    QTimer *timer = new QTimer(&s); 
    timer->connect(timer, SIGNAL(timeout()), &s, SLOT(advance())); 
    timer->start(1000); 

    return a.exec(); 
} 

あなたはこのような何かを取得する必要があります:

meh...

パスを後に更新されるコースのいずれかのQGraphicsPathItem缶に。あなたはそれがいるようだアニメーション

QPainterPath p = gPath.path(); 
p.lineTo(0, 42); 
gPath.setPath(p); 

(私はQPainterPathが暗黙のうちに共有されているかどうかわからないんだけど...)、すべてのパスをコピーによるパフォーマンスヒットを避けるために、どこかの元画家の道を残しておきたいかもしれません何らかのアニメーション/オンザフライ更新をしようとしています。 Qtにはこのためのフレームワークが全部あります。最も単純なフォームでは、QGraphicsPathItemをサブクラス化し、advance()スロットを再実装して、モーションから次のポイントを自動的に取得します。そうするために残された唯一のことは、必要な頻度でs.advance()を呼び出すことです。

http://doc.trolltech.com/4.5/qgraphicsscene.html#advance

+0

ウィンドウが表示されます。 線は描画されません。 scene.addPath()を呼び出す前にそれらを追加すると、それらが表示されますが、(あなたの例のように)GraphicsViewは更新を行わないので、パスを変更すると新しい行は表示されません。 – peterdemin

+0

ああ、私はバグを見つけたと思う...コンボ board_path_item = new QGraphicsPathItem(); scene.addPath(nature_path_item-> path()、nature_pen); Doen'tは変更を追跡します。あなたのサンプルのおかげで – peterdemin

+0

しかし、クリッピングに関するタスクはまだ開いています... – peterdemin

1

エバン・テラン、そのコメントを申し訳ありません。

// Constructor: 
GraphWidget::GraphWidget(QWidget *parent) : 
     QGraphicsView(parent), 
     bounds(0, 0, 0, 0) 
{ 
    setScene(&scene); 
    QPen board_pen(QColor(255, 0, 0)); 
    QPen nature_pen(QColor(0, 0, 255)); 
    nature_path_item = scene.addPath(board_path, board_pen); 
    board_path_item = scene.addPath(nature_path, nature_pen); 
} 

// Eventually called func: 
void GraphWidget::Redraw() { 
    if(motion) { 
     double nature[6]; 
     double board[6]; 
     // Get coords: 
     motion->getNature(nature); 
     motion->getBoard(board); 
     if(nature_path.elementCount() == 0) { 
      nature_path.moveTo(nature[0], nature[1]); 
     } else { 
      nature_path.lineTo(nature[0], nature[1]); 
     } 
     if(board_path.elementCount() == 0) { 
      board_path.moveTo(board[0], board[1]); 
     } else { 
      board_path.lineTo(board[0], board[1]); 
     } 
    } 
}