实现功能:棋盘绘制
核心函数:
void paintEvent(QPaintEvent *); //QWidget自带的虚函数,重写后使用 QPainter 类来绘制图形
QPainter painter(this); //QPainter 相当于画笔
painter.drawLine(QPoint(x, y), QPoint(x, y)); //drawLine(QPoint(x, y), QPoint(x, y))画直线
核心代码:
1 void Board::paintEvent(QPaintEvent *) 2 { 3 QPainter painter(this); 4 int d = 40; 5 // 画10横线 6 for(int i=1; i<=10; ++i) 7 { 8 painter.drawLine(QPoint(d, i*d), QPoint(9*d, i*d)); 9 } 10 // 画9竖线 11 for(int i=1; i<=9; ++i) 12 { 13 if(i==1 || i==9) 14 painter.drawLine(QPoint(i*d, d), QPoint(i*d, 10*d)); 15 else 16 { 17 painter.drawLine(QPoint(i*d, d), QPoint(i*d, 5*d)); 18 painter.drawLine(QPoint(i*d, 6*d), QPoint(i*d, 10*d)); 19 } 20 } 21 22 // 九宫格 23 painter.drawLine(QPoint(4*d, 1*d), QPoint(6*d, 3*d)); 24 painter.drawLine(QPoint(6*d, 1*d), QPoint(4*d, 3*d)); 25 26 painter.drawLine(QPoint(4*d, 8*d), QPoint(6*d, 10*d)); 27 painter.drawLine(QPoint(6*d, 8*d), QPoint(4*d, 10*d)); 28 29 30 }
效果图: