Qt Qpainter Example

mywidget.h

#ifndef MYWIDGET_H
#define MYWIDGET_H
 
#include <QtGui>
#include <QWidget>
 
class MyWidget : public QWidget
{
    Q_OBJECT
public:
 //  MyWidget();
 
protected:
    void paintEvent(QPaintEvent *event);
signals:
 
public slots:
 
};
 
#endif // MYWIDGET_H

mywidget.cpp

#include "mywidget.h"
 
void MyWidget::paintEvent(QPaintEvent *event)
{
    //create a QPainter and pass a pointer to the device.
    //A paint device can be a QWidget, a QPixmap or a QImage
    QPainter painter(this);
 
    //a simple line
    painter.drawLine(1,1,100,100);
 
    //create a black pen that has solid line
    //and the width is 2.
    QPen myPen(Qt::black, 2, Qt::SolidLine);
    painter.setPen(myPen);
    painter.drawLine(100,100,100,1);
 
    //draw a point
    myPen.setColor(Qt::red);
    painter.drawPoint(110,110);
 
    //draw a polygon
    QPolygon polygon;
    polygon << QPoint(130, 140) << QPoint(180, 170)
             << QPoint(180, 140) << QPoint(220, 110)
             << QPoint(140, 100);
     painter.drawPolygon(polygon);
 
     //draw an ellipse
     //The setRenderHint() call enables antialiasing, telling QPainter to use different
     //color intensities on the edges to reduce the visual distortion that normally
     //occurs when the edges of a shape are converted into pixels
     painter.setRenderHint(QPainter::Antialiasing, true);
     painter.setPen(QPen(Qt::black, 3, Qt::DashDotLine, Qt::RoundCap));
     painter.setBrush(QBrush(Qt::green, Qt::SolidPattern));
     painter.drawEllipse(200, 80, 400, 240);
 
}

main.cpp

#include <QtGui>
#include "mywidget.h"
 
int main( int argc, char **argv )
{
  QApplication app( argc, argv );
 
  MyWidget myWidget;
  myWidget.show();
 
  return app.exec();
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.