QT:棋盘类设计(七)

逻辑思路

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、界面设计时,布局完单行单列需要进行整体布局才可以随窗体变动
在这里插入图片描述

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值