<b>Qtѧϰ֮·(27):Í»±äÌî³ä</b>[VC/C++±à³Ì]
¡¡¡¡±¾ÎÄ¡°<b>Qtѧϰ֮·(27):Í»±äÌî³ä</b>[VC/C++±à³Ì]¡±ÊÇÓÉÆßµÀÆæΪÄú¾«ÐÄÊÕ¼¯£¬À´Ô´ÓÚÍøÂçתÔØ£¬ÎÄÕ°æȨ¹éÎÄÕÂ×÷ÕßËùÓУ¬±¾Õ¾²»¶ÔÆä¹ÛµãÒÔ¼°ÄÚÈÝ×öÈκÎÆÀ¼Û£¬Çë¶ÁÕß×ÔÐÐÅжϣ¬ÒÔÏÂÊÇÆä¾ßÌåÄÚÈÝ£º
Ç°Ãæ˵ÁËÓйط´×ßÑùµÄÏà¹Ø֪ʶ,ÏÂÃæÀ´ËµÒ»ÏÂÍ»±ä.Í»±äÊǻͼÖкܳ£¼ûµÄÒ»ÖÖ¹¦Ð§,¼òµ¥À´Ëµ¾ÍÊÇ¿ÉÒ԰Ѽ¸ÖÖÑÕÉ«»ìºÏÔÚһͬ,ÈÃËüÃÇ¿ÉÒÔ×ÔÈ»µØ¹ý¶É,¶ø²»ÊÇÒ»ÏÂ×Ó±ä³ÉÁíÒ»ÖÖÑÕÉ«.Í»±äµÄËã·¨¶Ô±È¸´ÔÓ,дµÃ²»ºÃµÄ»°Ð§ÂÊ»áºÜµÍ,ºÃÔںܶà»Í¼ÏµÍ³¶¼ÄÚÖÃÁËÍ»±äµÄ¹¦Ð§,QtÒ²²»ÀýÍâ.Í»±äÆÕͨÊÇÓÃÔÚÌî³äÀïÃæµÄ,ËùÒÔ,Í»±äµÄÉèÖþÍÊÇÔÚQBrushÀïÃæ.
Qt¹©Ó¦ÁËÈýÖÖÍ»±ä»Ë¢,±æ±ðÊÇÏßÐÔÍ»±ä(QLinearGradient)¡¢·øÉäÍ»±ä(QRadialGradient)¡¢½Ç¶ÈÍ»±ä(QConicalGradient).ÒÔÏÂͼËùʾ(ͼƬ³ö×ÔC++ GUI Programming with Qt4, 2nd Edition):
ÏÂÃæÎÒÃÇÀ´¿´Ò»ÏÂÏßÐÔÍ»±äQLinearGradientµÄÓ÷¨.
void¡¡PaintedWidget::paintEvent(QPaintEvent¡¡*event)
{
QPainter¡¡painter(this);
painter.setRenderHint(QPainter::Antialiasing,¡¡true);
QLinearGradient¡¡linearGradient(60,¡¡50,¡¡200,¡¡200);
linearGradient.setColorAt(0.2,¡¡Qt::white);
linearGradient.setColorAt(0.6,¡¡Qt::green);
linearGradient.setColorAt(1.0,¡¡Qt::black);
painter.setBrush(QBrush(linearGradient));
painter.drawEllipse(50,¡¡50,¡¡200,¡¡150);
}
ͬǰÃæÒ»Ñù,ÕâÀïÒ²½ö½ö¸ø³öÁËpaintEvent()º¯ÊýÀïÃæµÄ´úÂë.
Ê×ÏÈÎÒÃÇ·¿ªÁË·´×ßÑù,È»ºó³ÉÁ¢Ò»¸öQLinearGradient¶ÔÏóʵÀý.QLinearGradient¹¹Ô캯ÊýÓÐËĸö²ÎÊý,±æ±ðÊÇx1, y1, x2, y2,¼´Í»±äµÄÆðʼµãºÍÖÕÖ¹µã.ÔÚÕâÀï,ÎÒÃÇ´Ó(60, 50)¿ªÊ¼Í»±ä,µ½(200, 200)Ö¹.
Í»±äµÄÑÕÉ«ÊÇÔÚsetColorAt()º¯ÊýÖÐÖ¸¶¨µÄ.ÏÂÃæÊÇÕâ¸öº¯ÊýµÄÇ©Ãû:
void¡¡QGradient::setColorAt¡¡(¡¡qreal¡¡position,¡¡const¡¡QColor¡¡&¡¡color¡¡)
ËüµÄÒâ˼ÊÇ°ÑpositionλÖõÄÑÕÉ«ÉèÖóÉcolor.´ËÖÐ,positionÊÇÒ»¸ö0 - 1Çø¼äµÄÊý×Ö.Ò²¾ÍÊÇ˵,positionÊÇÏà¹ØÓÚÎÒÃdzÉÁ¢Í»±ä¶ÔÏóʱ×öµÄÄǸöÆðʼµãºÍÖÕÖ¹µãÇø¼äµÄ.±È·½Õâ¸öÏßÐÔÍ»±ä,¾ÍÊÇ˵,ÔÚ´Ó(60, 50)µ½(200, 200)µÄÏ߶ÎÉÏ,ÔÚ0.2,Ò²¾ÍÎå·ÖÖ®Ò»´¦ÉèÖóɰ×É«,ÔÚ0.6Ò²¾ÍÊÇÎå·ÖÖ®Èý´¦ÉèÖóÉÂÌÉ«,ÔÚ1.0Ò²¾ÍÊÇÖյ㴦ÉèÖóɺÚÉ«.
ÔÚ³ÉÁ¢QBrushʱ,°ÑÕâ¸öÍ»±ä¶ÔÏó´«ËͽøÈ¥,¾ÍÊÇÎÒÃǵĺó¹ûÀ²:
ÄÇô,ÎÒÃÇÔõôÈÃÏ߶ÎÒ²ÊÇÍ»±äµÄÄØ?ÒªÖªµÀ,Ö±ÏßÊÇÓû±Ê»æÖƵİ¡!ÕâÀï,¼ÙÈçÄãϸÐIJéÔÄÁËAPIÎĵµ¾Í»á·¢ÏÖ,QPenÊdzÐÊÜQBrush×÷Ϊ²ÎÊýµÄ.Ò²¾ÍÊÇ˵,Äã¿ÉÒÔ²Ù×ÝÒ»¸öQBrush³ÉÁ¢Ò»¸öQPen,ÕâÑù,QBrushÈ«²¿µÄÌî³äЧ¹û¶¼¿ÉÒÔÓÃÔÚ»±ÊÉÏÁË!
void¡¡PaintedWidget::paintEvent(QPaintEvent¡¡*event)
{
QPainter¡¡painter(this);
painter.setRenderHint(QPainter::Antialiasing,¡¡true);
QLinearGradient¡¡linearGradient(60,¡¡50,¡¡200,¡¡200);
linearGradient.setColorAt(0.2,¡¡Qt::white);
linearGradient.setColorAt(0.6,¡¡Qt::green);
linearGradient.setColorAt(1.0,¡¡Qt::black);
painter.setPen(QPen(QBrush(linearGradient),¡¡5));
painter.drawLine(50,¡¡50,¡¡200,¡¡200);
}
¿´¿´ÎÒÃǵÄÍ»±äÏß°É!
³ö´¦:http://devbean.blog.51cto.com/448512/238168
¡¡¡¡ÒÔÉÏÊÇ¡°<b>Qtѧϰ֮·(27):Í»±äÌî³ä</b>[VC/C++±à³Ì]¡±µÄÄÚÈÝ£¬Èç¹ûÄã¶ÔÒÔÉϸÃÎÄÕÂÄÚÈݸÐÐËȤ£¬Äã¿ÉÒÔ¿´¿´ÆßµÀÆæΪÄúÍƼöÒÔÏÂÎÄÕ£º
±¾ÎĵØÖ·£º | ÓëÄúµÄQQ/BBSºÃÓÑ·ÖÏí! |