4.QPixmap,QTransform,绘图函数的使用
新建一個(gè)項(xiàng)目Painter
| MyWidget.h |
| #ifndef MYWIDGET_H #define MYWIDGET_H ? #include <QWidget> ? class MyWidget : public QWidget { ??? Q_OBJECT public: ??? explicit MyWidget(QWidget *parent = 0); ??? void paintEvent(QPaintEvent *); ? signals: ? public slots: ? }; ? #endif // MYWIDGET_H |
| MyWidget.cpp |
| #include "MyWidget.h" #include <QPainter> #include <QPixmap> #include <QApplication> ? MyWidget::MyWidget(QWidget *parent) : ??? QWidget(parent) { } ? void MyWidget::paintEvent(QPaintEvent *) { ??? QPixmap pixmap(size()); ? ??? QPainter p(&pixmap); ? ??? //p.translate(100, 100); ??? //p.scale(); ??? //消除鋸齒 ??? p.setRenderHint(QPainter::Antialiasing); ??? //轉(zhuǎn)換 ??? QTransform transform; ??? transform.translate(50,50); ??? //旋轉(zhuǎn)30度 ??? transform.rotate(30); ?? // transform.scale(.5, .5); ??? p.setTransform(transform); #if 1 ??? //下面的transform可以覆蓋上面的一個(gè)transform的效果 ??? QTransform transform2; ??? //對(duì)整個(gè)效果進(jìn)行縮放 ??? transform2.scale(.5, .5); ??? //最后一個(gè)參數(shù)實(shí)現(xiàn)和上面一個(gè)transform實(shí)現(xiàn)組合 ??? p.setTransform(transform2, true); #endif ??? //通過(guò)兩個(gè)點(diǎn)實(shí)現(xiàn)畫線 ??? p.drawLine(QPoint(0, 0), QPoint(100, 100)); ? ??? //p.translate(-100, -100); ??? //鋼筆 ??? p.setPen(QPen(Qt::red, 2, Qt::DashLine)); ??? //使用刷子 ??? p.setBrush(Qt::yellow); ??? //設(shè)置刷子 ??? p.setFont(QFont("aaa", 40, 700, true)); ? ??? p.drawEllipse(QPoint(95, 333), 50, 50); ??? //里面寫上文字 ??? p.drawText(QPoint(300, 50), "Hello world"); ??? //p.drawPixmap(QPoint(40, 40), QPixmap("../aaa.png")); ??? //p.drawRect(QRect(40, 60, 100, 50)); ??? //下面的方式實(shí)現(xiàn)畫一個(gè)圓角矩形 ??? p.drawRoundRect(QRect(40, 60, 100, 50)); ? ??? p.end(); ? ??? p.begin(this); ??? //通過(guò)下面的方式實(shí)現(xiàn)畫圖,之所以運(yùn)行的結(jié)果是黑絲的圖,是因?yàn)榧拥氖?span lang="EN-US">pixmap ??? p.drawPixmap(0, 0, pixmap); } ? int main(int argc, char** argv) { ??? QApplication app(argc, argv); ? ??? MyWidget w; ??? w.show(); ? ??? return app.exec(); } |
| 運(yùn)行結(jié)果:
|
| ? |
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的4.QPixmap,QTransform,绘图函数的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 工行world奋斗信用卡好过吗
- 下一篇: 6.QT信号和槽