2017-09-12 5 views
0

Qtで図形や回転を実験していますが、どのように動作するのかは本当に迷っています。現在、私はそれの上に小さな三角形で矩形を描画するコードを持っています。私は35度で図形を回転したいので、私はこれを試してみてください。Qt回転シェイプ

void Window::paintEvent(QPaintEvent *event) 
{ 
    QPainter painter(this); 
    QBrush brush; 
    brush.setStyle(Qt::SolidPattern); 
    brush.setColor(Qt::white); 
    painter.setBrush(brush); 
    painter.setPen(Qt::NoPen); 
    painter.fillRect(0,0,800,800,brush); 
    brush.setColor(Qt::red); 
    painter.translate(s.getX()-5,s.getY()-8); 
    painter.rotate(35); 
    painter.fillRect(0,0,10,16,brush); 
    QPolygon pol; 
    pol.setPoints(3,0,0,10,0,5,10); 
    QPainterPath p; 
    p.addPolygon(pol); 
    painter.fillPath(p,brush); 
} 

(今x 150で、yは750で、s.getX/Y()の呼び出しを無視します)

コードを回転し、変換せず細かく動作し、形状を描画します。現在のコードでは、ポリゴンではなく長方形だけが表示されます。これらの図形をどのように回転させるのですか?

+0

どのような結果が期待されますか?両方の図形を回転させるという目標はありますか?どの地点を回りたいですか? – AMA

+0

@AMAしたがって、0度の回転では、その形状はその上に三角形がある四角形です。私はこの形を取って全体を35度回転させたい。 – imulsion

+0

@imulsionウィジェットのサイズは? – eyllanesc

答えて

1

affine transformationsの仕組みを正しく理解する必要があります。適切な理解がなければ、必要なものを達成するのに苦労するでしょう。

コードがポイント(s.getX() - 5, s.getY() - 8)周りのすべてを回転させる新しい位置に座標(0,0)

  • translate移動センター:

    • rotate座標の中心の周りのすべてを回転させます。変換なしで

      QPainter painter(this); 
      QBrush brush; 
      brush.setStyle(Qt::SolidPattern); 
      brush.setColor(Qt::white); 
      painter.setBrush(brush); 
      painter.setPen(Qt::NoPen); 
      painter.fillRect(0, 0, 800, 800, brush); 
      brush.setColor(Qt::red); 
      painter.translate(150, 750); 
      
      painter.translate(5, 8); // move center of coordinates to the center of red rectangle 
      painter.rotate(35); // rotate around the center of red rectangle 
      painter.translate(-5, -8); // move center of coordinates back to where it was 
      
      painter.fillRect(0, 0, 10, 16, brush); 
      QPolygon pol; 
      pol.setPoints(3, 0, 0, 10, 0, 5, 10); 
      QPainterPath p; 
      p.addPolygon(pol); 
      brush.setColor(Qt::blue); 
      painter.fillPath(p, brush); 
      

      :変換と

      enter image description here

      は、だからここに赤い長方形の中心の周りに両方の形状に35度回転するコードです

      enter image description here

  • +0

    完全に働いてくれてありがとう! – imulsion

    +0

    @imulsionあなたは大歓迎です:) – AMA