qt的坐標(biāo)變換函數(shù):
為了繪圖的方便刊殉,QPainter提功了一些坐標(biāo)變換的功能闺阱,通過(guò)平移炮车,旋轉(zhuǎn)等坐標(biāo)變換,得到一個(gè)邏輯坐標(biāo)系統(tǒng),使用邏輯坐標(biāo)系統(tǒng)在某些時(shí)候繪圖更方便示血。
QPainter有關(guān)坐標(biāo)變換操作的函數(shù):
分組? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 函數(shù)原型? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 功能
坐標(biāo)變換? ? ? ? ? ? ? ? ?void translate(qreal dx,qreal dy)? ? ? ? ? ? 坐標(biāo)系統(tǒng)一定的偏移量棋傍,坐標(biāo)原點(diǎn)平移到新的點(diǎn)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? void rotate(qreal angle)? ? ? ? ? ? ? ? ? ? ? ? 坐標(biāo)系統(tǒng)順時(shí)針旋轉(zhuǎn)一個(gè)角度
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? void scale(qreal sx,qreal sy)? ? ? ? ? ? ? ? 坐標(biāo)系統(tǒng)縮放
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? void shear(qrael sh,qreal sy)? ? ? ? ? ? ? ? 坐標(biāo)系統(tǒng)做扭轉(zhuǎn)變換
狀態(tài)保存與恢復(fù)? ? ? ?void save()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 保存painter當(dāng)前的狀態(tài),就是將當(dāng)前狀態(tài)壓入棧
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? void restore()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 恢復(fù)上一次狀態(tài)难审,就是從堆棧中彈出上次的狀態(tài)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? void resetTransform()? ? ? ? ? ? ? ? ? ? ? ? ? ? 復(fù)位所有的坐標(biāo)變換
實(shí)例:
在widget的頭文件中聲明:protected:? ? void paintEvent(QPaintEvent *event);
繪圖時(shí)件中的代碼:
????QPainter painter(this);
? ? //指定渲染的模式
? ? //QPainter::Antialiasing=抗鋸齒
? ? //QPainter::TextAntialiasing=繪制的字體抗鋸齒
? ? painter.setRenderHint(QPainter::Antialiasing);
? ? painter.setRenderHint(QPainter::TextAntialiasing);
? ? //生成五角星的五個(gè)頂點(diǎn)的坐標(biāo)瘫拣,假設(shè)原點(diǎn)在五角星
? ? qreal R=100;//半徑
? ? const qreal Pi=3.1415926;
? ? qreal deg=Pi*72/180;
? ? QPoint points[5]={
? ? ? QPoint(R,0),
? ? ? QPoint(R*std::cos(deg),-R*std::sin(deg)),
? ? ? QPoint(R*std::cos(2*deg),-R*std::sin(2*deg)),
? ? ? QPoint(R*std::cos(3*deg),-R*std::sin(3*deg)),
? ? ? QPoint(R*std::cos(4*deg),-R*std::sin(4*deg)),
? ? };
? ? //設(shè)置字體
? ? QFont? font;
? ? //設(shè)置字體的大小
? ? font.setPointSize(12);
? ? //設(shè)置字體是否加粗
? ? font.setBold(true);
? ? //畫家使用該字體
? ? painter.setFont(font);
? ? //設(shè)置畫筆
? ? QPen penLine;
? ? //設(shè)置畫筆線寬
? ? penLine.setWidth(2);
? ? //設(shè)置畫線顏色
? ? penLine.setColor(Qt::blue);
? ? //設(shè)置線的類型
? ? penLine.setStyle(Qt::SolidLine);
? ? //設(shè)置線的端點(diǎn)樣式
? ? penLine.setJoinStyle(Qt::BevelJoin);
? ? //畫家使用畫筆
? ? painter.setPen(penLine);
? ? //設(shè)置畫刷
? ? QBrush brush;
? ? //設(shè)置畫刷顏色
? ? brush.setColor(Qt::yellow);
? ? //畫刷填充樣式
? ? brush.setStyle(Qt::SolidPattern);
? ? //設(shè)計(jì)繪制五角星的painterPath,已重復(fù)使用
? ? QPainterPath starPath;
? ? starPath.moveTo(points[0]);
? ? starPath.lineTo(points[2]);
? ? starPath.lineTo(points[4]);
? ? starPath.lineTo(points[1]);
? ? starPath.lineTo(points[3]);
? ? //閉合路徑,最后一個(gè)點(diǎn)與第一個(gè)點(diǎn)相連
? ? starPath.closeSubpath();
? ? //顯示端點(diǎn)編號(hào)
? ? starPath.addText(points[0],font,"0");
? ? starPath.addText(points[0],font,"1");
? ? starPath.addText(points[0],font,"2");
? ? starPath.addText(points[0],font,"3");
? ? starPath.addText(points[0],font,"4");
? ? //繪圖
? ? //保存坐標(biāo)狀態(tài)
? ? painter.save();
? ? //平移
? ? painter.translate(100,120);
? ? //畫星星
? ? painter.drawPath(starPath);
? ? //設(shè)置文本
? ? painter.drawText(0,0,"S1");
? ? //恢復(fù)坐標(biāo)狀態(tài)
? ? painter.restore();
? ? //平移
? ? painter.translate(300,120);
? ? //縮放
? ? painter.scale(0.8,0.8);
? ? //順時(shí)針旋轉(zhuǎn)90度
? ? painter.rotate(90);
? ? //畫第二個(gè)星星
? ? painter.drawPath(starPath);
? ? //設(shè)置文本
? ? painter.drawText(0,0,"S2");
? ? //復(fù)位所有坐標(biāo)變換
? ? painter.resetTransform();
? ? //平移
? ? painter.translate(500,120);
? ? //逆時(shí)針旋轉(zhuǎn)-145度
? ? painter.rotate(-145);
? ? //畫星星
? ? painter.drawPath(starPath);
? ? //設(shè)置文本
? ? painter.drawText(0,0,"S3");
? ??????