Qt之QPainter 画一个4*4棋盘

1.widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();
private slots:

private:
    void paintEvent(QPaintEvent *);//绘图函数声明
    QPainter *paint;
};

#endif // WIDGET_H

2.widget.cpp

#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
//       setGeometry(100,100,200,200);
}

Widget::~Widget()
{

}
void Widget::paintEvent(QPaintEvent *)
{
    int x=20,y=20;

    paint=new QPainter;
    paint->begin(this);

    paint->setPen(QPen(Qt::blue,4,Qt::SolidLine));
    paint->setBrush(QBrush(Qt::white,Qt::SolidPattern));
//画5条横线
    for(int i=0;i<5;i++)
    {
        paint->drawLine(20,y+80*i,340,y+80*i);
    }
//画5条竖线
    for(int i=0;i<5;i++)
    {
        paint->drawLine(x+80*i,20,x+80*i,340);
    }
//在棋盘正中做两个红棋子
    paint->setBrush(QBrush(Qt::red,Qt::SolidPattern));
    paint->drawEllipse(100,100,80,80);
    paint->drawEllipse(180,180,80,80);
//
    paint->setBrush(QBrush(Qt::green,Qt::SolidPattern));
    paint->drawEllipse(180,100,80,80);
    paint->drawEllipse(100,180,80,80);

//    paint->drawRect(20,20,160,160);

    paint->end();

}

3.main.cpp

#include "widget.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}


你可能感兴趣的:(QT)