Qt中QPainter與坐標(biāo)的使用
一、坐標(biāo)系統(tǒng)與坐標(biāo)變換
坐標(biāo)系統(tǒng)
QPainter坐標(biāo)變換相關(guān)函數(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)變換 |
示例
繪制五角星
船艦widget窗口,只重載paintEvent
函數(shù)
#include "widget.h" #include "ui_widget.h" #include <QPalette> #include <QPainter> #include <cmath> Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); setPalette(QPalette(Qt::white)); setAutoFillBackground(true); resize(600,300); } Widget::~Widget() { delete ui; } void Widget::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter(this); QPen pen; //筆 pen.setStyle(Qt::SolidLine); painter.setPen(pen); painter.setRenderHint(QPainter::Antialiasing); //抗鋸齒 QBrush brush; //刷子 brush.setColor(Qt::yellow); brush.setStyle(Qt::SolidPattern); painter.setBrush(brush); //qreal deg = 3.141592*2/5; qreal deg = (360/5)*3.141592/180; qreal R=100; 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)), }; QPainterPath starPath; starPath.moveTo(points[3]); starPath.lineTo(points[1]); starPath.lineTo(points[4]); starPath.lineTo(points[2]); starPath.lineTo(points[0]); starPath.closeSubpath(); QFont font; font.setPointSize(12); starPath.addText(points[0], font, "0"); starPath.addText(points[1], font, "1"); starPath.addText(points[2], font, "2"); starPath.addText(points[3], font, "3"); starPath.addText(points[4], font, "4"); painter.setFont(font); painter.save(); //保存 painter.translate(100,120); painter.drawPath(starPath); painter.drawText(0,0,"S1"); painter.restore(); //恢復(fù) painter.translate(300,120); painter.rotate(90); painter.scale(0.7,0.7); painter.drawPath(starPath); painter.drawText(0,0,"S2"); painter.resetTransform(); painter.translate(500,120); painter.rotate(-90); painter.scale(1.05,1.05); painter.drawPath(starPath); painter.drawText(0,0,"S3"); }
二、可縮放圖形
視口: 繪圖設(shè)備的任意一個(gè)矩形區(qū)域的物理坐標(biāo),可以只選取物理坐標(biāo)的一個(gè)矩形區(qū)域用
于繪圖。視口默認(rèn)情況下等于繪圖設(shè)備的整個(gè)矩形區(qū)。
窗口: 對(duì)應(yīng)于視口的矩形區(qū)域,只不過(guò)是用邏輯坐標(biāo)定義的坐標(biāo)系,窗口坐標(biāo)的中心在矩
形中心。
使用窗口坐標(biāo)繪制,不用管實(shí)際的物理大小
#include "widget.h" #include "ui_widget.h" #include <QPalette> #include <QPainter> #include <QLinearGradient> Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); setPalette(QPalette(Qt::white)); setAutoFillBackground(true); resize(300,300); } Widget::~Widget() { delete ui; } void Widget::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); int W = QWidget::width(); int H = QWidget::height(); int side = qMin(W,H); QRect rect((W-side)/2,(H-side)/2, side,side); //視口大小 painter.drawRect(rect); painter.setViewport(rect); painter.setWindow(-100,-100,200,200); QLinearGradient linerGradient(0,0,100,0); // 漸變 linerGradient.setColorAt(0,Qt::yellow); linerGradient.setColorAt(1,Qt::green); linerGradient.setSpread(QGradient::PadSpread); //發(fā)散 painter.setCompositionMode(QPainter::RasterOp_NotSourceXorDestination); painter.setBrush(linerGradient); for (size_t i=0; i<36; i++) { painter.drawEllipse(QPoint(50,0),50,50); painter.rotate(10); } }
到此這篇關(guān)于Qt中QPainter與坐標(biāo)的使用的文章就介紹到這了,更多相關(guān)Qt中QPainter與坐標(biāo)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
緩存處理函數(shù)storageKeySuffix操作示例解析
這篇文章主要介紹了淺析緩存處理函數(shù)storageKeySuffix操作示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08C++基礎(chǔ) class、struct、union詳細(xì)
這篇文章主要 給大家介紹的是C++基礎(chǔ) class、struct、union,主要由三部分組成,分別是、類class、結(jié)構(gòu)體struct、共用體union,需要的朋友可以參考一下2021-09-09基于Sizeof與Strlen的區(qū)別以及聯(lián)系的使用詳解
本篇文章是對(duì)Sizeof與Strlen的區(qū)別以及聯(lián)系的使用進(jìn)行了詳細(xì)的介紹。需要的朋友參考下2013-05-05關(guān)于C++靜態(tài)數(shù)據(jù)成員的實(shí)現(xiàn)講解
今天小編就為大家分享一篇關(guān)于關(guān)于C++靜態(tài)數(shù)據(jù)成員的實(shí)現(xiàn)講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12C++實(shí)現(xiàn)Date類各種運(yùn)算符重載的示例代碼
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)Date類各種運(yùn)算符重載的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單航班管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單航班管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12詳解C++中new運(yùn)算符和delete運(yùn)算符的使用
這篇文章主要介紹了C++中new運(yùn)算符和delete運(yùn)算符的使用,文章來(lái)自于微軟開(kāi)發(fā)者文檔,因而根據(jù)Visual C++的一些特性來(lái)進(jìn)行講解,需要的朋友可以參考下2016-01-01