2009年7月10日 星期五

Qt讓畫圖達到旋轉


這是在QMatrix的說明中的一段,我們拿來套用,QMatrix可讓QPainter,上下左右或者旋轉

double pi = 3.14;

// double a = pi/180 * 185.0; //改變角度
double a = pi/180 * shapex;
double sina = sin(a);
double cosa = cos(a);

QMatrix translationMatrix(1, 0, 0, 1, 50.0, 50.0);
QMatrix rotationMatrix(cosa, sina, -sina, cosa, 0, 0);
QMatrix scalingMatrix(0.5, 0, 0, 1.0, 0, 0);

QMatrix matrix;
matrix = scalingMatrix * rotationMatrix * translationMatrix;


painter.setPen(QPen(Qt::blue, 1, Qt::DashLine));
painter.drawRect(0, 0, 100, 100);

painter.setMatrix(matrix); //這會改變painter的matrix

painter.setFont(QFont("Helvetica", 24));
painter.setPen(QPen(Qt::black, 1));
painter.drawText(20, 10, "QMatrix");

來看結果:

video

沒有留言: