QPainter的用法
Yanboo 发表于 2005-03-10 19:14:57
QPainter能在wdiget或pixmap等"paint device"上绘图. Qpainter在写自定义widget或有独特风格的自定义item时很有用.
QPainter能绘制几何图形: 点, 线, 矩形, 椭圆, 弧, 弦, 饼图, 多边形 及 贝塞尔曲线. 它也能绘制pixmap, images, and text.
当把一个paint device传递给QPainter的构造器时, QPainter采用这个device的一些设置并把其他设置初始化为缺省值. 这些设置影响执行绘制操作的方法. 三个最重要的是painter的pen, brush, font:
pen: 颜色, 粗细, 线型
brush: 颜色, 风格
font:
例如:
QPainter painter ( this);
painter.setPen( QPen( black, 3, DashDotLine ) );
painter.setBrush( QBrush( red, SolidPattern) );
painter.drawEllipse( 20, 20, 100, 60 );
其中,pen styles: SolidLine(实心线), DashLine(长划线), DotLine(点线), DashDotLine(点划线), DashDotDotLine(双点划线).
brush styles: SolidPattern, Dense1Pattern, Dense2Pattern, Dense3Pattern, Dense4Pattern, Dense5Pattern, Dense6Pattern, Dense7Pattern, HorPattern, VerPattern, CrossPattern, BDiagPattern, FDiagPattern, DiagCrossPattern, NoBrush.
**
painter的当前状态能通过调用save()和稍后调用restore()存储在stack中.当我们需要临时改变一些painter settings并要恢复到以前的状态时,这个功能是很有用的.
pen, brush, font之外的其他painter控制设置有:
background color
raster poeration
brush origin
clip region
viewport, window, world matrix
Let’s take a closer look at the coordinate system defined by the viewport,
window, and world matrix. (In this context, the term “window” does not refer
to a window in the sense of a top-level widget, and the “viewport” has nothing
to do with QScrollView’s viewport.)
viewport和window关系紧密。viewport是在物理坐标系中指定的任意矩形。 window在逻辑坐标系中指定同样的矩形。 当绘制时,在逻辑坐标系中指定了点, 这些坐标点被按照线性代数关系转换成物理坐标, 这种转换基于当前window-viewport设置。
缺省的,viewport和window都被设置成device的矩形。例如, 如果device是320*200部件, viewport 和 window 都是同样的320*200矩形,它的左上角是(0, 0)。 这时,逻辑和物理是统一的。
window-viewport机制对绘制那些独立于尺寸编码的图形或device的分辨率。
我们常自己运算转换逻辑坐标到物理坐标。但他的简单性让QPainter起作用。
例如,如果我们想让逻辑坐标从(-50, -50)扩展到(+50, +50), (0, 0)为中点,
可以如下设置:
painter.setWindow( QRect(-50, -50, 100, 100) );
(-50,-50)指定原点,(100, 100)指定宽和高。 这意味着逻辑坐标(-50,-50)与物理坐标
(0,0)重合, 逻辑坐标(+50,+50)与物理坐标(320,200)吻合。
例子:
main.cpp
//-------------------------------------------------------------------------
#include <qapplication.h>
#include "testpainter.h"
int main( int argc, char **argv )
{
QApplication app ( argc, argv );
TestPainter testPainter ;
app.setMainWidget( &testPainter );
testPainter.show();
testPainter.draw();
testPainter.drawPie();
testPainter.drawCubicBezier();
testPainter.show();
return app.exec();
}
testpainter.h:
//-------------------------------------------------------------------------
#ifndef TESTPAINTER_H
#define TESTPAINTER_H
#include <qwidget.h>
class TestPainter : public QWidget
{
public:
TestPainter( QWidget* parent=0, const char* name=0 );
~TestPainter();
void draw();
void drawPie();
void drawCubicBezier();
};
#endif
testpainter.cpp:
//-------------------------------------------------------------------------
#include <qpainter.h>
#include "testpainter.h"
TestPainter::TestPainter( QWidget* parent, const char* name)
:QWidget(parent, name )
{
}
TestPainter::~TestPainter()
{
}
void TestPainter::draw()
{
QPainter painter (this );
painter.setPen( QPen( black, 8, DashDotLine) );
painter.setBrush( QBrush( red, Dense6Pattern) );
painter.drawEllipse( 20, 20, 100, 60 );
}
void TestPainter::drawPie( )
{
QPainter painter ( this );
painter.setPen( QPen( black, 5, SolidLine) );
painter.setBrush( QBrush( red, Dense1Pattern) );
painter.drawPie( 160, 20, 100, 60, 60 * 16, 270 * 16 );
}
void TestPainter::drawCubicBezier()
{
QPainter painter( this );
QPointArray points( 4 );
points[0] = QPoint( 280, 80 );
points[1] = QPoint( 320, 10 );
points[2] = QPoint( 360, 10 );
points[3] = QPoint( 400, 80 );
painter.setPen( QPen(blue, 2, SolidLine) );
painter.drawCubicBezier( points );
}
QPainter能绘制几何图形: 点, 线, 矩形, 椭圆, 弧, 弦, 饼图, 多边形 及 贝塞尔曲线. 它也能绘制pixmap, images, and text.
当把一个paint device传递给QPainter的构造器时, QPainter采用这个device的一些设置并把其他设置初始化为缺省值. 这些设置影响执行绘制操作的方法. 三个最重要的是painter的pen, brush, font:
pen: 颜色, 粗细, 线型
brush: 颜色, 风格
font:
例如:
QPainter painter ( this);
painter.setPen( QPen( black, 3, DashDotLine ) );
painter.setBrush( QBrush( red, SolidPattern) );
painter.drawEllipse( 20, 20, 100, 60 );
其中,pen styles: SolidLine(实心线), DashLine(长划线), DotLine(点线), DashDotLine(点划线), DashDotDotLine(双点划线).
brush styles: SolidPattern, Dense1Pattern, Dense2Pattern, Dense3Pattern, Dense4Pattern, Dense5Pattern, Dense6Pattern, Dense7Pattern, HorPattern, VerPattern, CrossPattern, BDiagPattern, FDiagPattern, DiagCrossPattern, NoBrush.
**
painter的当前状态能通过调用save()和稍后调用restore()存储在stack中.当我们需要临时改变一些painter settings并要恢复到以前的状态时,这个功能是很有用的.
pen, brush, font之外的其他painter控制设置有:
background color
raster poeration
brush origin
clip region
viewport, window, world matrix
Let’s take a closer look at the coordinate system defined by the viewport,
window, and world matrix. (In this context, the term “window” does not refer
to a window in the sense of a top-level widget, and the “viewport” has nothing
to do with QScrollView’s viewport.)
viewport和window关系紧密。viewport是在物理坐标系中指定的任意矩形。 window在逻辑坐标系中指定同样的矩形。 当绘制时,在逻辑坐标系中指定了点, 这些坐标点被按照线性代数关系转换成物理坐标, 这种转换基于当前window-viewport设置。
缺省的,viewport和window都被设置成device的矩形。例如, 如果device是320*200部件, viewport 和 window 都是同样的320*200矩形,它的左上角是(0, 0)。 这时,逻辑和物理是统一的。
window-viewport机制对绘制那些独立于尺寸编码的图形或device的分辨率。
我们常自己运算转换逻辑坐标到物理坐标。但他的简单性让QPainter起作用。
例如,如果我们想让逻辑坐标从(-50, -50)扩展到(+50, +50), (0, 0)为中点,
可以如下设置:
painter.setWindow( QRect(-50, -50, 100, 100) );
(-50,-50)指定原点,(100, 100)指定宽和高。 这意味着逻辑坐标(-50,-50)与物理坐标
(0,0)重合, 逻辑坐标(+50,+50)与物理坐标(320,200)吻合。
例子:
main.cpp
//-------------------------------------------------------------------------
#include <qapplication.h>
#include "testpainter.h"
int main( int argc, char **argv )
{
QApplication app ( argc, argv );
TestPainter testPainter ;
app.setMainWidget( &testPainter );
testPainter.show();
testPainter.draw();
testPainter.drawPie();
testPainter.drawCubicBezier();
testPainter.show();
return app.exec();
}
testpainter.h:
//-------------------------------------------------------------------------
#ifndef TESTPAINTER_H
#define TESTPAINTER_H
#include <qwidget.h>
class TestPainter : public QWidget
{
public:
TestPainter( QWidget* parent=0, const char* name=0 );
~TestPainter();
void draw();
void drawPie();
void drawCubicBezier();
};
#endif
testpainter.cpp:
//-------------------------------------------------------------------------
#include <qpainter.h>
#include "testpainter.h"
TestPainter::TestPainter( QWidget* parent, const char* name)
:QWidget(parent, name )
{
}
TestPainter::~TestPainter()
{
}
void TestPainter::draw()
{
QPainter painter (this );
painter.setPen( QPen( black, 8, DashDotLine) );
painter.setBrush( QBrush( red, Dense6Pattern) );
painter.drawEllipse( 20, 20, 100, 60 );
}
void TestPainter::drawPie( )
{
QPainter painter ( this );
painter.setPen( QPen( black, 5, SolidLine) );
painter.setBrush( QBrush( red, Dense1Pattern) );
painter.drawPie( 160, 20, 100, 60, 60 * 16, 270 * 16 );
}
void TestPainter::drawCubicBezier()
{
QPainter painter( this );
QPointArray points( 4 );
points[0] = QPoint( 280, 80 );
points[1] = QPoint( 320, 10 );
points[2] = QPoint( 360, 10 );
points[3] = QPoint( 400, 80 );
painter.setPen( QPen(blue, 2, SolidLine) );
painter.drawCubicBezier( points );
}
//-------------------------------------------------------------------------
#ifndef TESTPAINTER_H
#define TESTPAINTER_H
#include <qwidget.h>
class TestPainter : public QWidget
{
public:
TestPainter( QWidget* parent=0, const char* name=0 );
~TestPainter();
void draw();
void drawPie();
void drawCubicBezier();
};
#endif
//-------------------------------------------------------------------------
#include <qpainter.h>
#include <Q3PointArray>
#include "testpainter.h"
TestPainter::TestPainter( QWidget* parent, const char* name)
:QWidget(parent, name )
{
}
TestPainter::~TestPainter()
{
}
void TestPainter::draw()
{
QPainter painter (this );
painter.setPen( QPen( Qt::black, 8, Qt::DashDotLine) );
painter.setBrush( QBrush( Qt::red, Qt::Dense6Pattern) );
painter.drawEllipse( 20, 20, 100, 60 );
}
void TestPainter::drawPie( )
{
QPainter painter ( this );
painter.setPen( QPen( Qt::black, 5, Qt::SolidLine) );
painter.setBrush( QBrush( Qt::red, Qt::Dense1Pattern) );
painter.drawPie( 160, 20, 100, 60, 60 * 16, 270 * 16 );
}
void TestPainter::drawCubicBezier()
{
QPainter painter( this );
Q3PointArray points( 4 );
points[0] = QPoint( 280, 80 );
points[1] = QPoint( 320, 10 );
points[2] = QPoint( 360, 10 );
points[3] = QPoint( 400, 80 );
painter.setPen( QPen(Qt::blue, 2, Qt::SolidLine) );
painter.drawCubicBezier( points );
}
//-------------------------------------------------------------------------
#include <qapplication.h>
#include "testpainter.h"
int main( int argc, char **argv )
{
QApplication app ( argc, argv );
TestPainter testPainter ;
app.setMainWidget( &testPainter );
testPainter.show();
testPainter.draw();
testPainter.drawPie();
testPainter.drawCubicBezier();
testPainter.show();
return app.exec();
}
/*
QPainter::begin: Paint device returned engine == 0, type: 1
QPainter::setPen: Painter not active
QPainter::setBrush: Painter not active
QPainter::begin: Paint device returned engine == 0, type: 1
QPainter::setPen: Painter not active
QPainter::setBrush: Painter not active
QPainter::begin: Paint device returned engine == 0, type: 1
QPainter::setPen: Painter not active
请按任意键继续. . .
*/