Use of Qpainter in QT4 and drawing of common shapes such as rectangles and circles

Source: Internet
Author: User

use of Qpainter in QT4 and drawing of common shapes such as rectangles and circles

Routines: Drawing of rectangles in//qt4

#include <QApplication>
#include <QWidget>
#include <QPainter>
Class Mymainwindow:public qwidget
{
Public:
 mymainwindow (Qwidget *parent = 0);
 private:
 void paintevent (qpaintevent *);
 qpainter *paint;
};


void Mymainwindow::p aintevent (qpaintevent *)

The//paintevent function is automatically called by the system, without the need for us to call it artificially.
{
 paint=new qpainter;
 paint->begin (this);
 paint->setpen (Qpen (qt::blue,4,qt::D ashline));//Set brush form  
 paint->setbrush (QBrush (Qt :: Red,qt::solidpattern)); Set the brush form  
 paint->drawrect (20,20,160,160);
 paint->end ();
}


Mymainwindow::mymainwindow (qwidget *parent): Qwidget (parent)
{
 setgeometry ( 100,100,200,200);
}


int main (int argc,char **argv)
{
 qapplication a (ARGC,ARGV);
 mymainwindow W;
 w.show ();
 return a.exec ();
}

 


Output: Draw circle and Oval core code: Paint->setpen (Qpen (qt::blue,4,qt::solidline)); Paint->drawellipse (20,20,210,160); 1th, the 2 parameters represent the number of pixels in the upper-left corner of the circle/ellipse distance, respectively. 3rd, 4 parameters represent the width and height of a circle/ellipse. More precisely, the circle or ellipse is in the rectangle, and the vertex of the upper-left corner of the rectangle is in the axis position (20,20), the center of the circle or ellipse is the center of the rectangle, the following are similar ...
Draw Rounded Rectangle Core code: Paint->setpen (Qpen (qt::blue,4,qt::solidline)); Paint->drawroundrect (20,20,210,160,50,50); The last two parameters determine the roundness of the corners. It can be any value between 0 and 99 (99 represents the most rounded).
Draw the pie chart core code: Paint->setpen (Qpen (qt::green,4,qt::solidline)); Paint->drawpie (20,20,210,160,0,500); The first four parameters define a circle (same as the DrawEllipse () function). The latter two parameters define the style of the circle. 0 is the starting angle (the actual unit is 1/16 degrees), and 500 is the angle (in units of 1/16 degrees) that the sector expands.
Draw chord Core code: Paint->setpen (Qpen (qt::green,4,qt::solidline)); Paint->drawchord (20,20,210,160,500,1000); The Drawchord () function is exactly the same as the parameter of the Drawpie () function.
Draw Arc Core code: Paint->setpen (Qpen (qt::green,4,qt::solidline)); Paint->drawarc (20,20,210,160,500,1000); The DrawArc () function is exactly the same as the parameter of the Drawpie () function.
Draw the Bézier curve Core code: Paint->setpen (Qpen (qt::green,4,qt::solidline)); Paint->drawquadbezier (Qpointarray (Qrect (20,20,210,160))); The only parameter passed to the function represents a rectangle in which the Bezier curve is created (the other parameter is the default parameter, which can be omitted).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.