功能实现:1、包含两个ui界面,登陆界面包含人机对战和人人对战;2、实现棋盘的编辑,都是使用QT自带的类画线,实现象棋棋盘线路。
1、新建工程的框架如下图所示
2、login.h
#ifndef LOGIN_H
#define LOGIN_H
#include<QDialog>
#include <QMainWindow>
#include <QPushButton>
namespace Ui {
class Login;
}
class Login : public QDialog
{
Q_OBJECT
public:
explicit Login(QWidget *parent = 0);
public slots:
void selecthuman();
void selectcomputer();
private:
QPushButton *human;
QPushButton *computer;
private:
Ui::Login *ui;
public:
void paintEvent(QPaintEvent *);
private:
int mode;
};
#endif // LOGIN_H
3、login.cpp
#include "login.h"
#include "ui_login.h"
#include <QPainter>
Login::Login(QWidget *parent) :
QDialog(parent),
ui(new Ui::Login)
{
//ui->setupUi(this);
this->resize(600,600);
this->move(400,50);
QPixmap pixmap("./pic/4.jpg");
pixmap = pixmap.scaled(this->width(), this->height(), Qt::IgnoreAspectRatio);
QPalette palette;
palette.setBrush(QPalette::Background,QBrush(pixmap));
this->setPalette(palette);
human=new QPushButton(tr("VS Human"),this);
computer=new QPushButton(tr("VS Computer"),this);
human->setGeometry(20,500,90,50);
computer->setGeometry(20,420,90,50);
//具体来说:如果一个事件处理函数调用了一个事件对象的accept()函数,这个事件就不会被继续传播给其父组件
connect(human,SIGNAL(clicked()),this,SLOT(accept()));
connect(human,SIGNAL(clicked()),this,SLOT(selectcomputer()));
connect(computer,SIGNAL(clicked()),this,SLOT(accept()));
connect(computer,SIGNAL(clicked()),this,SLOT(selecthuman()));
}
void Login::selecthuman()
{
mode = 0;
}
void Login::selectcomputer()
{
mode = 1;
}
void Login::paintEvent(QPaintEvent *)
{
//1、画字
QPainter painter(this);
//painter.setPen(Qt::blue);
painter.setPen(Qt::blue);
painter.setFont(QFont("Arial", 50));
painter.drawText(rect(), Qt::AlignCenter, "中国象棋");
}
4、mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
private:
short size;
QPixmap pic[32];
protected:
void paintEvent(QPaintEvent *);
};
#endif // MAINWINDOW_H
5、mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QPen>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
size = 60;
this->setWindowTitle(tr("楚河汉界"));
this->setFixedSize(size*10+100,size*10+100);
this->move(400,0);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::paintEvent(QPaintEvent *) //绘制棋盘格线
{
QPainter p;
p.begin(this);
QPen pen;
pen.setWidth(10);
pen.setColor(Qt::green); // 设置画笔为黄色
p.setPen(pen); // 设置画笔
p.drawPoint(size-5,size-5);
p.drawPoint(size*9+5,size-5);
p.drawPoint(size-5,size*10+5);
p.drawPoint(size*9+5,size*10+5);
//设置画笔颜色和宽度
QPen background=p.pen();
background.setColor(Qt::black);
background.setWidth(2);
p.setPen(background);
qDebug() << size << endl;
//画棋盘的轮廓线
p.drawLine(size-5,size-5,size*9+5,size-5);
p.drawLine(size-5,size-5,size-5,size*10+5);
p.drawLine(size-5,size*10+5,size*9+5,size*10+5);
p.drawLine(size*9+5,size-5,size*9+5,size*10+5);
background.setWidth(1);
p.setPen(background);
short n=1;
//画棋盘的18条垂直线
for(;n<10;n++)
{
p.drawLine(size*n,size,size*n,size*5);
p.drawLine(size*n,size*6,size*n,size*10);//Vertical
}
//画楚河汉界的两条短垂直线
p.drawLine(size,size*5,size,size*6);
p.drawLine(size*9,size*5,size*9,size*6);
//画棋盘的10条水平线
for(n=1;n<=10;n++)
p.drawLine(size,size*n,size*9,size*n);//Horizon
//画棋盘“士”行走的斜线
p.drawLine(size*4,size,size*6,size*3); //斜线轨迹
p.drawLine(size*6,size,size*4,size*3);
p.drawLine(size*4,size*8,size*6,size*10);
p.drawLine(size*6,size*8,size*4,size*10);
//画楚河汉界
QPointF point(size*2.6, size*5.7);
p.setFont(QFont("Arial", 30));
p.drawText(point, "楚河 汉界");
p.end();
}
6、main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <login.h>
#include <QTextCodec>
int main(int argc, char *argv[])
{
QTextCodec *codec = QTextCodec::codecForName("UTF-8");//情况2
QTextCodec::setCodecForTr(codec);
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QApplication a(argc, argv);
MainWindow w;
//w.show();
Login c;
//c.show();
if(c.exec()==QDialog::Accepted) //当选择对话框上的button被单击调用了accept槽时,显示主窗口
{
w.show();
return a.exec();
}
return a.exec();
}
7、运行结果