QPainter在窗口绘图区中绘制图形的默认坐标系,原点位于绘图区的左上角,X轴向右为正,Y轴向下为正,1代表1像素。这样的坐标系也叫设备坐标系。此外,QPainter还提供了一些坐标变换的功能,将设备坐标系平移、缩放、旋转、扭曲为逻辑坐标系。很多时候,在逻辑坐标系中绘制图形,比使用设备坐标更加方便。
QPainter类提供了一套与坐标变换有关的方法:
xxxxxxxxxx
71void QPainter::translate(qreal dx, qreal dy); // 坐标系平移
2void QPainter::rotate(qreal angle); // 坐标系旋转
3void QPainter::scale(qreal sx, qreal sy); // 坐标系缩放
4void QPainter::shear(qreal sh, qreal sv); // 坐标系扭曲
5void QPainter::save(); // 将当前坐标系压入到堆栈中
6void QPainter::restore(); // 从堆栈中弹出并恢复坐标系
7void QPainter::resetTransform(); // 恢复到默认坐标系
此外,QPainter类也支持通过坐标变换矩阵(CTM)变换坐标系。
通过QtCreator,在C:\Users\Minwei\Projects\Qt路径下,创建名为Transform的项目。
C:\Users\Minwei\Projects\Qt\Transform\transformdialog.ui:
xxxxxxxxxx
831
2<ui version="4.0">
3 <class>TransformDialog</class>
4 <widget class="QDialog" name="TransformDialog">
5 <property name="geometry">
6 <rect>
7 <x>0</x>
8 <y>0</y>
9 <width>800</width>
10 <height>600</height>
11 </rect>
12 </property>
13 <property name="palette">
14 <palette>
15 <active>
16 <colorrole role="Base">
17 <brush brushstyle="SolidPattern">
18 <color alpha="255">
19 <red>255</red>
20 <green>255</green>
21 <blue>255</blue>
22 </color>
23 </brush>
24 </colorrole>
25 <colorrole role="Window">
26 <brush brushstyle="SolidPattern">
27 <color alpha="255">
28 <red>255</red>
29 <green>255</green>
30 <blue>255</blue>
31 </color>
32 </brush>
33 </colorrole>
34 </active>
35 <inactive>
36 <colorrole role="Base">
37 <brush brushstyle="SolidPattern">
38 <color alpha="255">
39 <red>255</red>
40 <green>255</green>
41 <blue>255</blue>
42 </color>
43 </brush>
44 </colorrole>
45 <colorrole role="Window">
46 <brush brushstyle="SolidPattern">
47 <color alpha="255">
48 <red>255</red>
49 <green>255</green>
50 <blue>255</blue>
51 </color>
52 </brush>
53 </colorrole>
54 </inactive>
55 <disabled>
56 <colorrole role="Base">
57 <brush brushstyle="SolidPattern">
58 <color alpha="255">
59 <red>255</red>
60 <green>255</green>
61 <blue>255</blue>
62 </color>
63 </brush>
64 </colorrole>
65 <colorrole role="Window">
66 <brush brushstyle="SolidPattern">
67 <color alpha="255">
68 <red>255</red>
69 <green>255</green>
70 <blue>255</blue>
71 </color>
72 </brush>
73 </colorrole>
74 </disabled>
75 </palette>
76 </property>
77 <property name="windowTitle">
78 <string>坐标变换</string>
79 </property>
80 </widget>
81 <resources/>
82 <connections/>
83</ui>
C:\Users\Minwei\Projects\Qt\Transform\transformdialog.h:
xxxxxxxxxx
251
2
3
4
5
6QT_BEGIN_NAMESPACE
7namespace Ui { class TransformDialog; }
8QT_END_NAMESPACE
9
10class TransformDialog : public QDialog
11{
12 Q_OBJECT
13
14public:
15 TransformDialog(QWidget *parent = nullptr);
16 ~TransformDialog();
17
18protected:
19 void paintEvent(QPaintEvent*);
20
21private:
22 Ui::TransformDialog *ui;
23};
24
25// TRANSFORMDIALOG_H
C:\Users\Minwei\Projects\Qt\Transform\transformdialog.cpp:
xxxxxxxxxx
471
2
3
4
5
6TransformDialog::TransformDialog(QWidget *parent)
7 : QDialog(parent)
8 , ui(new Ui::TransformDialog)
9{
10 ui->setupUi(this);
11}
12
13TransformDialog::~TransformDialog()
14{
15 delete ui;
16}
17
18void TransformDialog::paintEvent(QPaintEvent*)
19{
20 QPainter painter(this);
21 painter.setRenderHint(QPainter::Antialiasing);
22
23 int w = width(), h = height();
24
25 painter.translate(w/2, h/2);
26 painter.rotate(180);
27 painter.scale(-1.5, 1.5);
28 painter.shear(1, 0);
29 //painter.resetTransform();
30
31 QPen pen;
32 pen.setWidth(8);
33 pen.setStyle(Qt::SolidLine);
34 pen.setJoinStyle(Qt::MiterJoin);
35
36 pen.setColor(Qt::red);
37 painter.setPen(pen);
38 painter.drawLine(QPoint(-w/4, 0), QPoint(w/4, 0));
39 painter.drawPolygon(QPolygon()
40 << QPoint(w/4, -5) << QPoint(w/4+10, 0) << QPoint(w/4, 5));
41
42 pen.setColor(Qt::blue);
43 painter.setPen(pen);
44 painter.drawLine(QPoint(0, -h/4), QPoint(0, h/4));
45 painter.drawPolygon(QPolygon()
46 << QPoint(-5, h/4) << QPoint(0, h/4+10) << QPoint(5, h/4));
47}
运行效果如图所示: