How to set the pen color and fill when draw a line in QT

Source: Internet
Author: User

The custom control has been implemented in the previous introduction, and the widget is placed in the main interface to draw a circle. For details, refer to "QT custom window"

The following describes how to set the paint brush color and the fill color of the image.

Paint color:

Void circlewidget: paintevent (qpaintevent * event) {qpainter painter (this); // painter. setbrush (qbrush (qcolor (0x00, 0xff, 0x00); // painter. drawellipse (qpoint (100,100), 100,100); qcolor Green (0, 0xff, 0); // set the color qpen pen (green); // define the paint brush painter. setpen (PEN); painter. drawrect (100,100, 50, 30 );}

You can also set the line width and style.

Void circlewidget: paintevent (qpaintevent * event) {qpainter painter (this); // painter. setbrush (qbrush (qcolor (0x00, 0xff, 0x00); // painter. drawellipse (qpoint (100,100), 100,100); qcolor Green (0, 0xff, 0); // set the color qpen pen (green); // define the pen. setwidth (5); // pen. setstyle (QT: dashdotdotline); painter. setpen (PEN); painter. drawrect (100,100, 50, 30 );}

Fill color:

void CircleWidget::paintEvent(QPaintEvent *event){      QPainter painter(this);      QBrush brush(QColor(0x00,0xFF,0x00));      painter.setBrush(brush);      //painter.drawEllipse(QPoint(100,100),100,100);      painter.drawRect(100,100,50,30);}

To change the canvas color, you can first obtain the length and width of the canvas, draw a rectangle, and then add the rectangle color as the desired color.

void CircleWidget::paintEvent(QPaintEvent *event){        QPainter painter(this);        int width=this->width();        int height=this->height();         QBrush brush(QColor(0xFF,0xFF,0xFF));         painter.setBrush(brush);         painter.drawRect(0,0,width,height);}

How to set the pen color and fill when draw a line in QT

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.