逻辑思路
1、设置棋盘背景,定义一个QPainter,设置QPixmap属性,调用QPainter类的drawPixmap()函数
2、设置棋盘线条,设置格子宽高,起始X,Y,此处可以用qDebug()查看起始位置是否根据窗体大小变化
3、初始化棋盘数据,设置二维数组,在初始化函数中判断数据名是black还是white,根据逻辑判断设置QPixmap界面背景属性,调用QPainter类的drawPixmap()函数,将棋子画在相应格子中。(此处用二维数组对棋盘中的每个格子进行对应)根据二维数组数据赋值(white or black)进行相应位置棋子设置。
检验:直接对二维数组最后一个格子赋值black,即chessDate[7] [7] =black,查看是否能对应上。
4、点击棋盘,棋子落下显示
void Widget::mousePressEvent(QMouseEvent *event)
{
int x = event->x();
int y = event->y();
if(x>startX&&x<startX+8*gridWidth){
if(y>startX&&y<startY+8*gridHeight){
int i,j;
i = (x-startX)/gridWidth;
j = (y-startY)/gridHeight;
chessDate[i][j] = Black;
this->update();
}
}
}
5、创建带ui的主界面文件,将棋盘类引入主界面文件,并用gridlayout调用棋盘类显示,此处需要对main函数棋盘类的显示替换成主界面的显示。
注:函数可分为function、event、slot,其中每项可分为private、public
代码结构
此代码没有ui文件,只需创建文件时取消ui文件勾选
cpp文件
#include "widget.h"
#include <QRect>
#include <QPixmap>
#include <QDebug>
#include <QPen>
#define GRIDCOUNT 10
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
Init();
}
Widget::~Widget()
{
}
//---------function------------//
//(private)
void Widget::Init()
{
bgFilename.clear();
bgFilename = "../mychess_5/chessBack.jpg";
lineColor = Qt::red;
lineStyle = Qt::SolidLine;
lineWidth = 3;
//初始化棋盘数据
//memset(chessDate,0,sizeof(int)*64);
}
//(public)
void Widget::changeBgFilename(const QString filename)
{
this->bgFilename = filename;
}
void Widget::changeLine(const QColor color, const Qt::PenStyle style, const int width)
{
this->lineColor = color;
this->lineStyle = style;
this->lineWidth = width;
}
//----------------slots------------------//
//----------------event------------------//
void Widget::paintEvent(QPaintEvent *)
{
QPainter mypainter(this);
//用图画
QRect myrect;
myrect.setTopLeft(QPoint(0,0));
myrect.setBottomRight(QPoint(this->width(),this->height()));
mypainter.drawPixmap(myrect,QPixmap("../mychess_5/image/widget_background.jpg"));
QPen pen1;
pen1.setColor(lineColor);
pen1.setStyle(lineStyle);
pen1.setWidth(lineWidth);
mypainter.setPen(pen1);
for(int i = 0;i<9;i++){
mypainter.drawLine(startX,startY+i*gridHeight,9*gridWidth,startY+i*gridHeight);
mypainter.drawLine(startX+i*gridWidth,startY,startX+i*gridWidth,9*gridHeight);
}
for(int i = 0;i<8;i++){
for(int j=0;j<8;j++){
if(chessDate[i][j] == White)
chessFilename = "../mychess_5/image/white.png";
else if(chessDate[i][j] == Black){
chessFilename = "../mychess_5/image/black.png";
}
else{
chessFilename.clear();
continue;
}
mypainter.drawPixmap(startX+i*gridWidth,startY+j*gridHeight,gridWidth,gridHeight,QPixmap(chessFilename));
}
}
}
void Widget::resizeEvent(QResizeEvent *event)
{
gridWidth = event->size().width()/GRIDCOUNT;
gridHeight = event->size().height()/GRIDCOUNT;
startX = gridWidth;
startY = gridHeight;
}
void Widget::mousePressEvent(QMouseEvent *event)
{
int x = event->x();
int y = event->y();
if(x>startX&&x<startX+8*gridWidth){
if(y>startX&&y<startY+8*gridHeight){
int i,j;
i = (x-startX)/gridWidth;
j = (y-startY)/gridHeight;
chessDate[i][j] = Black;
this->update();
}
}
}
h文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
void changeBgFilename(const QString filename);
void changeLine(const QColor color,const Qt::PenStyle style,const int width);
enum chessType{Empty=0,White,Black};
protected:
void paintEvent(QPaintEvent *);
void resizeEvent(QResizeEvent *event);
private:
//-----------函数------------//
void Init();
//-----------变量------------//
QString bgFilename;
QColor lineColor;
Qt::PenStyle lineStyle;
int lineWidth;
int gridWidth;
int gridHeight;
int startX;
int startY;
int chessDate[8][8];
QString chessFilename;
};
#endif // WIDGET_H
main文件
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
出现问题:
1、代码调试时出现背景界面不能设置,代码继续往后写就可以了
2、界面设计时,布局完单行单列需要进行整体布局才可以随窗体变动