上一节绘制了一个简单的棋盘,这一节将棋盘美化,以及绘制棋子。
效果如下:
#include "board.h"
Board::Board(QWidget *parent)
: QMainWindow(parent)
{
}
Board::~Board()
{
}
void Board::paintEvent(QPaintEvent *)
{
画棋盘 开始///
//设置背景图片
QPixmap bg(":/image/bg.jpg");
QPalette pal;
pal.setBrush(QPalette::Window,QBrush(bg));
this->setPalette(pal);
/
//设置背景颜色
//pal.setColor(QPalette::Background,QColor(244,244,181));
//this->setPalette(pal);
///
QPainter painter(this);
//设置线宽
QPen pen;
pen.setWidth(3);
painter.setPen(pen);
//init
int firstpoint=40; //设置第一点边距
int chesssize=120; //设置棋子大小
//固定窗口大小
this->setMaximumSize(2*firstpoint+8*chesssize,2*firstpoint+4*chesssize); //设置窗口大小
this->setMinimumSize(2*firstpoint+8*chesssize,2*firstpoint+4*chesssize); //设置窗口大小
//绘制棋盘
for(int i=0;i<5;i++) //画5条横线
{
painter.drawLine(QPoint(firstpoint,firstpoint+i*chesssize)
,QPoint(firstpoint+chesssize*8,firstpoint+chesssize*i));
}
for(int i=0;i<9;i++) //画9条竖线
{
painter.drawLine(QPoint(firstpoint+chesssize*i,firstpoint)
,QPoint(firstpoint+i*chesssize,firstpoint+chesssize*4));
}
画棋盘 结束///
画棋子 结束///
for(int i=0;i<8;i++){
for(int j=0;j<4;j++)
{
///设置棋子图片
QRectF rectangle(firstpoint+i*chesssize, firstpoint+j*chesssize, chesssize, chesssize);
QImage image(":/image/chessbg.png");
painter.drawImage(rectangle,image);
/*
//设置棋子颜色
QBrush brush;
brush.setColor(QColor(244,244,181));
brush.setStyle(Qt::SolidPattern);
painter.setBrush(brush);
painter.drawEllipse(rectangle);
*/
}
}
}