#include<QtGui>
class MyPaint:public QWidget
{
Q_OBJECT
public:
MyPaint(QWidget *parent=0,int i=1);
QSize sizeHint()const;
#if 1
void drawAnEllipse(QPainter *p);
void drawAPie(QPainter *p);
void drawACure(QPainter *p);
#endif
protected:
void paintEvent(QPaintEvent * );
private:
int num;
};
MyPaint::MyPaint(QWidget *parent,int i)
:QWidget(parent),num(i)
{
}
void MyPaint::drawAPie(QPainter *pa1)
{
pa1->setPen(QPen(Qt::black,15,Qt::SolidLine,Qt::RoundCap,Qt::MiterJoin));
pa1->setBrush(QBrush(Qt::blue,Qt::DiagCrossPattern));
pa1->drawPie(0,0,200,140,60*16,270*16);
}
void MyPaint::drawACure(QPainter *pa2)
{
pa2->setRenderHint(QPainter::Antialiasing,true);
QPainterPath path;
path.moveTo(0,220);
path.cubicTo(100,0,220,0,380,220);
pa2->setPen(QPen(Qt::black,8));
pa2->drawPath(path);
}
void MyPaint::drawAnEllipse(QPainter *pa3)
{
pa3->setRenderHint(QPainter::Antialiasing,true);
pa3->setPen(QPen(Qt::black,12,Qt::DashDotLine,Qt::RoundCap));
//#define GRADIENTPATTERN
#ifndef GRADIENTPATTERN
pa3->setBrush(QBrush(Qt::green));
#else
// 该句也有问题,目前不知道如何解决,不过 Qt::LinearGradientPattern换成
// Qt::SolidePattern的话,程序正常。
pa3->setBrush(QBrush(Qt::green,Qt::LinearGradientPattern));
#endif
// 还有另外两种渐变模式,分别为:QRadialGradient,QConicalGradient
#ifndef GRADIENTPATTERN
QLinearGradient gradient(0,0,200,140);
gradient.setColorAt(0.0,Qt::red);
gradient.setColorAt(0.2,Qt::green);
gradient.setColorAt(1.0,Qt::black);
pa3->setBrush(gradient);
#endif
pa3->drawEllipse(0,0,200,140);
}
QSize MyPaint::sizeHint()const
{
return QSize(400,400);
}
void MyPaint::paintEvent(QPaintEvent *)
{
QPainter pa1(this);
switch (num)
{
case 1:
drawAnEllipse(&pa1);
break;
case 2:
drawAPie(&pa1);
break;
case 3:
drawACure(&pa1);
break;
default:
break;
}
}
#include"main.moc" //?目前还不清楚这里面是什么,但必须包含进去,否则链接时候会出错。
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
QMainWindow *win = new QMainWindow;
MyPaint *mp1 = new MyPaint(win,1);
MyPaint *mp2 = new MyPaint(win,2);
MyPaint *mp3 = new MyPaint(win,3);
QSplitter sp(win);
sp.addWidget(mp1);
sp.addWidget(mp2);
sp.addWidget(mp3);
win->setCentralWidget(&sp);
#if 0
mp1->show();
mp2->show();
mp3->show();
#endif
win->show();
return app.exec();
}
|