1. 程式人生 > 其它 >QPainter繪製基本圖形元件的函式及示例

QPainter繪製基本圖形元件的函式及示例


/***********************/
//畫弧線
#if 0
QPainter painter(this);//建立物件
int w = this->width();
int h = this->height();
QRect rect(w/4,h/4,w/2,h/2);
int startAngle = 90*16; //起始90°
int spanAngle = 90*16;//旋轉90°
painter.drawArc(rect,startAngle,spanAngle);
#endif
/***********************/
//畫一段弦
#if 0
QPainter painter(this);//建立物件
int w = this->width();
int h = this->height();
QRect rect(w/4,h/4,w/2,h/2);
int startAngle = 90*16; //起始90°
int spanAngle = 90*16;//旋轉90°
painter.drawChord(rect,startAngle,spanAngle);
#endif
/***********************/
//根據給定的點話凸多邊行
#if 0
QPainter painter(this);//建立物件
int w = this->width();
int h = this->height();
QPoint point[4]={
QPoint(5*w/12,h/4),
QPoint(3*w/4,5*h/12),
QPoint(5*w/12,h/4),
QPoint(2/4,h/12),
};
painter.drawConvexPolygon(point,4);
#endif
/***********************/
//畫橢圓
#if 0
QPainter painter(this);//建立物件
int w = this->width();
int h = this->height();
QRect rect(w/4,h/4,w/2,h/2);
painter.drawEllipse(rect);
#endif
/***********************/
//在指定矩形區域類繪製圖片
#if 0
QPainter painter(this);//建立物件
int w = this->width();
int h = this->height();
QRect rect(w/4,h/4,w/2,h/2);
QImage image(":/xxx.jpg");
painter.drawImage(image);
#endif
/***********************/
//畫直線
#if 0
QPainter painter(this);//建立物件
int w = this->width();
int h = this->height();
QLine line(w/4,h/4,w/2,h/2);
painter.drawLine(line);
#endif
/***********************/
//畫一批直線
#if 0
QPainter painter(this);//建立物件
int w = this->width();
int h = this->height();
QRect rect(w/4,h/4,w/2,h/2);
QVector<QLine>lines;
lines.append(QLine(rect.topLeft(),rect.bottomRight()));
lines.append(QLine(rect.topRight(),rect.bottomLeft()));
lines.append(QLine(rect.topLeft(),rect.bottomLeft()));
lines.append(QLine(rect.topRight(),rect.bottomRight()));
painter.drawLines(lines);

#endif
/***********************/
//繪製由QPainterPath物件定義的路線
#if 0
QPainter painter(this);//建立物件
int w = this->width();
int h = this->height();
QLine line(w/4,h/4,w/2,h/2);
painter.drawLine(line);
QPainterPath path;
path.addEllipse(rect);
path.addRect(rect);
painter.drawPath(path);
#endif

/***********************/
//繪製扇形
#if 0
QPainter painter(this);//建立物件
int w = this->width();
int h = this->height();
QRect rect(w/4,h/4,w/2,h/2);
int startAngle = 40*16; //起始40°
int spanAngle = 120*16; //旋轉120°
painter.drawPie(rect,startAngle,spanAngle);
#endif
/***********************/
//繪製PixMAP圖片
#if 0
QPainter painter(this);//建立物件
int w = this->width();
int h = this->height();
QRect rect(w/4,h/4,w/2,h/2);
QPixmap pixmap(":/xxx.jpg");
painter.drawPixmap(rect,pixmap);
#endif
/***********************/
//畫一個點
#if 0
QPainter painter(this);//建立物件
int w = this->width();
int h = this->height();
painter.drawPoint(QPoint(w/2,h/2));
#endif
/***********************/
//畫一批點
#if 0
QPainter painter(this);//建立物件
int w = this->width();
int h = this->height();
QPoint point[]{
QPoint(5*w/12,h/4),
QPoint(3*w/4,5*h/12),
QPoint(2*w/4,5*h/12)
};
painter.drawPoints(point,3);
#endif
/***********************/
//畫多邊形,最後一個點會和第一個點閉合
#if 0
QPainter painter(this);//建立物件
int w = this->width();
int h = this->height();
QPoint point[]{
QPoint(5*w/12,h/4),
QPoint(3*w/4,5*h/12),
Qpoint(5*w/12,3*h/4),
QPoint(2*w/4,5*h/12)
};
painter.drawPolygon(point,4);
#endif
/***********************/
//畫多多點連線的線,最後一個點不會和第一個點連線
#if 0
QPainter painter(this);//建立物件
int w = this->width();
int h = this->height();
QPoint point[]{
QPoint(5*w/12,h/4),
QPoint(3*w/4,5*h/12),
Qpoint(5*w/12,3*h/4),
QPoint(2*w/4,5*h/12)
};
painter.drawPolyline(point,4);
#endif
/***********************/
//畫矩形
#if 0
QPainter painter(this);//建立物件
int w = this->width();
int h = this->height();
QRect rect(w/4,h/4,w/2,h/2);
painter.drawRect(rect);
#endif
/***********************/
//畫圓角矩形
#if 0
QPainter painter(this);//建立物件
int w = this->width();
int h = this->height();
QRect rect(w/4,h/4,w/2,h/2);
painter.drawRoundedRect(rect,20,20);
#endif
/***********************/
//繪製文字,只能繪製單行文字字型的大小等屬性由QPainter::font()決定
#if 0
QPainter painter(this);//建立物件
int w = this->width();
int h = this->height();
QRect rect(w/4,h/4,w/2,h/2);
QFont font;
font.setPointSize(30);
font.setBold(true);
painter.setFont(font);
painter.drawText(rect,"hello");

#endif
/***********************/
//擦除某個矩形區域,等效於用背景色填充該區域
#if 0
QPainter painter(this);//建立物件
int w = this->width();
int h = this->height();
QRect rect(w/4,h/4,w/2,h/2);
painter.eraseRect(rect);
#endif
/***********************/
//填充一個矩形,無邊框線
#if 0
QPainter painter(this);//建立物件
int w = this->width();
int h = this->height();
QRect rect(w/4,h/4,w/2,h/2);
painter.fillRect(rect,Qt::green);
#endif
/***********************/
//填充某個QPainterPath定義的繪圖路徑,但是輪廓線不顯示
#if 0
QPainter painter(this);//建立物件
int w = this->width();
int h = this->height();
QRect rect(w/4,h/4,w/2,h/2);
QPainterPath path;
path.addEllipse(rect);
path.addRect(rect);
painter.fillPath(path,Qt::green);
#endif