利用QPainter繪制各種圖形
Qt的二維圖形引擎是基於QPainter類的。QPainter既可以繪制幾何形狀(點、線、矩形、橢圓、弧形、弦形、餅狀圖、多邊形和貝塞爾曲線),也可以繪制像素映射、圖像和文字。此外,QPainter還支持一些高級特性,例如反走樣(針對文字和圖形邊緣)、像素混合、漸變填充和矢量路徑等。QPainter也支持線性變換,例如平移、旋轉、錯切和縮放。
本例子中利用QPainter類提供的各種draw函數,繪制各種類型的圖形,包括對圖形的形狀、顏色、填充風格等的選擇。
1、創建paintarea.h
#ifndef PAINTAREA_H #define PAINTAREA_H #include <QtGui> class PaintArea : public QWidget { Q_OBJECT public: enum Shape {Line,Rectangle,RoundRect,Ellipse,Polygon,Polyline,Points,Arc,Path,Text,Pixmap}; PaintArea(QWidget *parent = 0); void setShape(Shape); void setPen(QPen); void setBrush(QBrush); void paintEvent(QPaintEvent *); private: Shape shape; QBrush brush; QPen pen; }; #endif // PAINTAREA_H
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
2、創建paintarea.cpp文件
#include "paintarea.h" PaintArea::PaintArea(QWidget *parent) : QWidget(parent) { shape = Line; QPalette p = palette(); p.setColor(QPalette::Window,Qt::white); setPalette(p); setAutoFillBackground(true); setMinimumSize(400,400); } void PaintArea::setShape(Shape s) { shape = s; update(); } void PaintArea::setPen(QPen p) { pen = p; update(); } void PaintArea::setBrush(QBrush b) { brush = b; update(); } void PaintArea::paintEvent(QPaintEvent *) { QPainter p(this); p.setPen(pen); p.setBrush(brush); QRect rect(50,100,300,200); static const QPoint points[4] = { QPoint(150,100), QPoint(300,150), QPoint(350,250), QPoint(100,300) }; int startAngle = 30 * 16; int spanAngle = 120 * 16; QPainterPath path; path.addRect(150,150,100,100); path.moveTo(100,100); path.cubicTo(300,100,200,200,300,300); path.cubicTo(100,300,200,200,100,100); switch(shape) { case Line: p.drawLine(rect.topLeft(),rect.bottomRight()); break; case Rectangle: p.drawRect(rect); break; case RoundRect: p.drawRoundRect(rect); break; case Ellipse: p.drawEllipse(rect); break; case Polygon: p.drawPolygon(points,4); break; case Polyline: p.drawPolyline(points,4); break; case Points: p.drawPoints(points,4); break; case Arc: p.drawArc(rect,startAngle,spanAngle); break; case Path: p.drawPath(path); break; case Text: p.drawText(rect,Qt::AlignCenter,tr("Hello Qt")); break; case Pixmap: p.drawPixmap(150,150,QPixmap(":/images/butterfly.png")); break; default: break; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
3、創建mainwidget.h
#ifndef MAINWIDGET_H #define MAINWIDGET_H #include <QtGui> #include "paintarea.h" class MainWidget : public QWidget { Q_OBJECT public: MainWidget(QWidget *parent = 0); public slots: void slotShape(int); void slotPenWidth(int); void slotPenColor(); void slotPenStyle(int); void slotPenCap(int); void slotPenJoin(int); void slotBrush(int); private: PaintArea *area; QComboBox *shapeComboBox; QSpinBox *widthSpinBox; QComboBox *penStyleComboBox; QComboBox *penCapComboBox; QComboBox *penJoinComboBox; QComboBox *brushStyleComboBox; QFrame *colorFrame; }; #endif // MAINWIDGET_H
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
4、創建mainwidget.cpp
#include "mainwidget.h" MainWidget::MainWidget(QWidget *parent) : QWidget(parent) { area = new PaintArea; QLabel *label1 = new QLabel(tr("Shape:")); QLabel *label2 = new QLabel(tr("Pen Width:")); QLabel *label3 = new QLabel(tr("Pen Color:")); QLabel *label4 = new QLabel(tr("Pen Style:")); QLabel *label5 = new QLabel(tr("Pen Cap:")); QLabel *label6 = new QLabel(tr("Pen Join:")); QLabel *label7 = new QLabel(tr("Brush:")); shapeComboBox = new QComboBox; shapeComboBox->addItem(tr("Line"), PaintArea::Line); shapeComboBox->addItem(tr("Polygon"), PaintArea::Polygon); shapeComboBox->addItem(tr("Rectangle"), PaintArea::Rectangle); shapeComboBox->addItem(tr("Round Rectangle"), PaintArea::RoundRect); shapeComboBox->addItem(tr("Ellipse"), PaintArea::Ellipse); shapeComboBox->addItem(tr("Path"), PaintArea::Path); shapeComboBox->addItem(tr("Polyline"), PaintArea::Polyline); shapeComboBox->addItem(tr("Arc"), PaintArea::Arc); shapeComboBox->addItem(tr("Points"), PaintArea::Points); shapeComboBox->addItem(tr("Text"), PaintArea::Text); shapeComboBox->addItem(tr("Pixmap"), PaintArea::Pixmap); connect(shapeComboBox,SIGNAL(activated(int)),this,SLOT(slotShape(int))); widthSpinBox = new QSpinBox; widthSpinBox->setRange(0,20); connect(widthSpinBox,SIGNAL(valueChanged(int)),this,SLOT(slotPenWidth(int))); colorFrame = new QFrame; colorFrame->setAutoFillBackground(true); colorFrame->setPalette(QPalette(Qt::blue)); QPushButton *colorPushButton = new QPushButton(tr("change")); connect(colorPushButton,SIGNAL(clicked()),this,SLOT(slotPenColor())); penStyleComboBox = new QComboBox; penStyleComboBox->addItem(tr("Solid"), Qt::SolidLine); penStyleComboBox->addItem(tr("Dash"), Qt::DashLine); penStyleComboBox->addItem(tr("Dot"), Qt::DotLine); penStyleComboBox->addItem(tr("Dash Dot"), Qt::DashDotLine); penStyleComboBox->addItem(tr("Dash Dot Dot"), Qt::DashDotDotLine); penStyleComboBox->addItem(tr("None"), Qt::NoPen); connect(penStyleComboBox,SIGNAL(activated(int)),this,SLOT(slotPenStyle(int))); penCapComboBox = new QComboBox; penCapComboBox->addItem(tr("Flat"), Qt::FlatCap); penCapComboBox->addItem(tr("Square"), Qt::SquareCap); penCapComboBox->addItem(tr("Round"), Qt::RoundCap); connect(penCapComboBox,SIGNAL(activated(int)),this,SLOT(slotPenCap(int))); penJoinComboBox = new QComboBox; penJoinComboBox->addItem(tr("Miter"), Qt::MiterJoin); penJoinComboBox->addItem(tr("Bevel"), Qt::BevelJoin); penJoinComboBox->addItem(tr("Round"), Qt::RoundJoin); connect(penJoinComboBox,SIGNAL(activated(int)),this,SLOT(slotPenJoin(int))); brushStyleComboBox = new QComboBox; brushStyleComboBox->addItem(tr("Linear Gradient"), Qt::LinearGradientPattern); brushStyleComboBox->addItem(tr("Radial Gradient"), Qt::RadialGradientPattern); brushStyleComboBox->addItem(tr("Conical Gradient"), Qt::ConicalGradientPattern); brushStyleComboBox->addItem(tr("Texture"), Qt::TexturePattern); brushStyleComboBox->addItem(tr("Solid"), Qt::SolidPattern); brushStyleComboBox->addItem(tr("Horizontal"), Qt::HorPattern); brushStyleComboBox->addItem(tr("Vertical"), Qt::VerPattern); brushStyleComboBox->addItem(tr("Cross"), Qt::CrossPattern); brushStyleComboBox->addItem(tr("Backward Diagonal"), Qt::BDiagPattern); brushStyleComboBox->addItem(tr("Forward Diagonal"), Qt::FDiagPattern); brushStyleComboBox->addItem(tr("Diagonal Cross"), Qt::DiagCrossPattern); brushStyleComboBox->addItem(tr("Dense 1"), Qt::Dense1Pattern); brushStyleComboBox->addItem(tr("Dense 2"), Qt::Dense2Pattern); brushStyleComboBox->addItem(tr("Dense 3"), Qt::Dense3Pattern); brushStyleComboBox->addItem(tr("Dense 4"), Qt::Dense4Pattern); brushStyleComboBox->addItem(tr("Dense 5"), Qt::Dense5Pattern); brushStyleComboBox->addItem(tr("Dense 6"), Qt::Dense6Pattern); brushStyleComboBox->addItem(tr("Dense 7"), Qt::Dense7Pattern); brushStyleComboBox->addItem(tr("None"), Qt::NoBrush); connect(brushStyleComboBox,SIGNAL(activated(int)),this,SLOT(slotBrush(int))); QGridLayout *ctrlLayout = new QGridLayout; int labelCol = 0; int contentCol = 1; ctrlLayout->addWidget(label1,0,labelCol); ctrlLayout->addWidget(shapeComboBox,0,contentCol); ctrlLayout->addWidget(label2,1,labelCol); ctrlLayout->addWidget(widthSpinBox,1,contentCol); ctrlLayout->addWidget(label3,2,labelCol); ctrlLayout->addWidget(colorFrame,2,contentCol); ctrlLayout->addWidget(colorPushButton,2,2); ctrlLayout->addWidget(label4,3,labelCol); ctrlLayout->addWidget(penStyleComboBox,3,contentCol); ctrlLayout->addWidget(label5,4,labelCol); ctrlLayout->addWidget(penCapComboBox,4,contentCol); ctrlLayout->addWidget(label6,5,labelCol); ctrlLayout->addWidget(penJoinComboBox,5,contentCol); ctrlLayout->addWidget(label7,6,labelCol); ctrlLayout->addWidget(brushStyleComboBox,6,contentCol); QHBoxLayout *mainLayout = new QHBoxLayout; mainLayout->addWidget(area); mainLayout->addLayout(ctrlLayout); mainLayout->setMargin(10); mainLayout->setSpacing(10); setLayout(mainLayout); slotShape(0); slotPenWidth(0); slotPenStyle(0); slotPenCap(0); slotPenJoin(0); slotBrush(0); } void MainWidget::slotShape(int value) { PaintArea::Shape shape = PaintArea::Shape(shapeComboBox->itemData(value,Qt::UserRole)