跟着视频教程创建了翻金币的项目,花了好几个晚上才学习完。
视频地址:最新QT从入门到实战
感谢视频的教学,真是受益匪浅。
后面的代码参考了老师的模板以及文档的抒写格式。
发布到随笔中的目的一方面为了完成自己的学习目的,另一方面也便于在后续从事QT工作时遇到类似的问题能有处可查。
个人觉得QT中有两个地方是最有用的,也是最值得花时间去研究的。 一是信号与槽的处理,可以跨线程的触发信号来处理槽函数, 二是控件的封装,摆脱了原始控件的构造函数限制的功能。 其他的学习点有些印象即可,知识这玩意时间长了难免不会忘记,只要在用的时候去查就行了。 不过原理我们还是需要牢记在心的。
各个.cpp和.h文件在后面。
//mainscene.cpp #include "mainscene.h" #include "ui_mainscene.h" #include <QPixmap> #include <QPaintEvent> #include <QPainter> #include "mypushbutton.h" #include "chooselevelscene.h" #include <QTimer> #include <QSound> MainScene::MainScene(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainScene) { ui->setupUi(this); //设置窗口固定大小 this->setFixedSize(320,588); //设置图标 this->setWindowIcon(QPixmap(":/res/Coin0001.png")); //设置窗口标题 this->setWindowTitle("翻金币"); //退出操作 connect(ui->actionExit,&QAction::triggered,[=](){ this->close(); }); QSound *startSound = new QSound(":/res/TapButtonSound.wav",this); //创建开始按钮 MyPushButton *startBtn = new MyPushButton(":/res/MenuSceneStartButton.png"); startBtn->setParent(this); startBtn->move(this->width()*0.5-startBtn->width()*0.5,this->height()*0.7); //选择关卡场景 chooseScene = new ChooseLevelScene(); connect(chooseScene, &ChooseLevelScene::chooseSceneBack,[=](){ chooseScene->setGeometry(chooseScene->geometry()); chooseScene->hide(); this->show(); }); connect(startBtn,&MyPushButton::clicked,[=](){ startSound->play(); //开始音效 startBtn->zoom(); QTimer::singleShot(500, this,[=](){ //设置chooseScene场景的位置 chooseScene->setGeometry(this->geometry()); this->hide(); chooseScene->show(); }); }); } MainScene::~MainScene() { delete ui; } void MainScene::paintEvent(QPaintEvent*) { QPainter painter(this); //绘制主窗口背景 QPixmap pix; pix.load(":/res/PlayLevelSceneBg.png"); painter.drawPixmap(0,0,this->width(),this->height(),pix); //绘制主窗口背景标题 pix.load(":/res/Title.png"); pix = pix.scaled(pix.width()*0.5,pix.height()*0.5); painter.drawPixmap(10,30,pix.width(),pix.height(),pix); }
//mainscene.h #ifndef MAINSCENE_H #define MAINSCENE_H #include <QMainWindow> #include "chooselevelscene.h" QT_BEGIN_NAMESPACE namespace Ui { class MainScene; } QT_END_NAMESPACE class MainScene : public QMainWindow { Q_OBJECT public: MainScene(QWidget *parent = nullptr); ~MainScene(); void paintEvent(QPaintEvent*); ChooseLevelScene *chooseScene = NULL; private: Ui::MainScene *ui; }; #endif // MAINSCENE_H
//chooselevelscene.cpp #include "chooselevelscene.h" #include <QMenuBar> #include <QMenu> #include <QPainter> #include "mypushbutton.h" #include <QTimer> #include <QLabel> #include "playscene.h" #include <QSound> ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent) { //设置窗口固定大小 this->setFixedSize(320,588); //设置图标 this->setWindowIcon(QPixmap(":/res/Coin0001.png")); //设置标题 this->setWindowTitle("选择关卡"); //创建菜单栏 QMenuBar *bar = new QMenuBar(); this->setMenuBar(bar); //创建开始菜单 QMenu *startMenu = bar->addMenu("开始"); //创建按钮菜单项 QAction *quitAction = startMenu->addAction("退出"); //点击退出 connect(quitAction,&QAction::triggered,[=](){ this->close(); }); //选择关卡按钮音效 QSound *chooseSound = new QSound(":/res/TapButtonSound.wav",this); //返回按钮音效 QSound *backSound = new QSound(":/res/BackButtonSound.wav",this); //返回按钮 MyPushButton *backBtn = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png"); backBtn->setParent(this); backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height()); connect(backBtn,&MyPushButton::clicked,[=](){ backSound->play(); QTimer::singleShot(500, this,[=](){ this->hide(); //触发自定义信号,关闭自身,该信号写到 signals下做声明 emit this->chooseSceneBack(); } ); }); for(int i=0;i<20;i++) { MyPushButton *menuBtn = new MyPushButton(":/res/LevelIcon.png"); menuBtn->setParent(this); menuBtn->move(25 + (i%4)*70, 130+(i/4)*70); //按钮上显示的文字 QLabel *label = new QLabel; label->setParent(this); label->setFixedSize(menuBtn->width(),menuBtn->height()); label->setText(QString::number(i+1)); label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); label->move(25 + (i%4)*70, 130+(i/4)*70); label->setAttribute(Qt::WA_TransparentForMouseEvents,true); connect(menuBtn,&MyPushButton::clicked,[=](){ chooseSound->play(); // qDebug() << "select: " << i; if(pScene == NULL) //游戏场景最好不用复用,直接移除掉创建新的场景 { this->hide(); pScene = new PlayScene(i+1); //将选择的关卡号 传入给PlayerScene //设置游戏场景的初始位置 pScene->setGeometry(this->geometry()); pScene->show(); connect(pScene, &PlayScene::chooseSceneBack,[=](){ this->setGeometry(pScene->geometry()); this->show(); delete pScene; pScene = NULL; }); } }); } } void ChooseLevelScene::paintEvent(QPaintEvent*) { QPainter painter(this); QPixmap pix; pix.load(":/res/OtherSceneBg.png"); painter.drawPixmap(0,0,this->width(),this->height(),pix); //加载标题 pix.load(":/res/Title.png"); painter.drawPixmap( (this->width() - pix.width())*0.5,30,pix.width(),pix.height(),pix); }
//chooselevelscene.h #ifndef CHOOSELEVELSCENE_H #define CHOOSELEVELSCENE_H #include <QMainWindow> #include "playscene.h" class ChooseLevelScene : public QMainWindow { Q_OBJECT public: explicit ChooseLevelScene(QWidget *parent = nullptr); void paintEvent(QPaintEvent*); PlayScene *pScene = NULL; signals: //自定义信号 void chooseSceneBack(); }; #endif // CHOOSELEVELSCENE_H
//mypushbutton.cpp #include "mypushbutton.h" #include <QDebug> #include <QPropertyAnimation> #include <qpropertyanimation.h> //MyPushButton::MyPushButton(QWidget *parent) : QPushButton(parent) //{ //} MyPushButton::MyPushButton(QString normalImg,QString pressImg) { //成员变量normalImgPath保存正常显示图片路径 normalImgPath = normalImg; //成员变量pressedImgPath保存按下后显示的图片 pressedImgPath = pressImg; //创建QPixmap对象 QPixmap pixmap; //判断是否能够加载正常显示的图片,若不能提示加载失败 bool ret = pixmap.load(normalImgPath); if(!ret) { qDebug() << normalImg << "加载图片失败!"; } this->setFixedSize(pixmap.width(),pixmap.height()); //设置不规则图片的样式表 this->setStyleSheet("QPushButton{border:0px;}"); //设置图标 this->setIcon(pixmap); //设置图标大小 this->setIconSize(QSize(pixmap.width(),pixmap.height())); } void MyPushButton::zoom() { //创建动画对象 QPropertyAnimation * animation1 = new QPropertyAnimation(this,"geometry"); //设置时间间隔,单位毫秒 animation1->setDuration(200); //创建起始位置 animation1->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height())); //创建结束位置 animation1->setEndValue(QRect(this->x(),this->y(),this->width(),this->height())); //设置缓和曲线,QEasingCurve::OutBounce 为弹跳效果 animation1->setEasingCurve(QEasingCurve::OutBounce); //开始执行动画 animation1->start(); } void MyPushButton::mousePressEvent(QMouseEvent *e) { if(pressedImgPath != "") { QPixmap pixmap; bool ret = pixmap.load(pressedImgPath); if(!ret) { qDebug()<<pressedImgPath<<"加载图片失败!"; } this->setFixedSize( pixmap.width(), pixmap.height() ); this->setStyleSheet("QPushButton{border:0px;}"); this->setIcon(pixmap); this->setIconSize(QSize(pixmap.width(),pixmap.height())); } return QPushButton::mousePressEvent(e); } void MyPushButton::mouseReleaseEvent(QMouseEvent*e) { if(pressedImgPath != "") { QPixmap pixmap; bool ret = pixmap.load(normalImgPath); if(!ret) { qDebug()<<normalImgPath<<"加载图片失败!"; } this->setFixedSize( pixmap.width(), pixmap.height() ); this->setStyleSheet("QPushButton{border:0px;}"); this->setIcon(pixmap); this->setIconSize(QSize(pixmap.width(),pixmap.height())); } return QPushButton::mouseReleaseEvent(e); }
//mypushbutton.h #ifndef MYPUSHBUTTON_H #define MYPUSHBUTTON_H #include <QPushButton> class MyPushButton : public QPushButton { public: // explicit MyPushButton(QWidget *parent = nullptr); MyPushButton(QString normalImg,QString pressImg = ""); QString normalImgPath; //默认显示图片路径 QString pressedImgPath; //按下后显示图片路径 void zoom(); void mousePressEvent(QMouseEvent*); void mouseReleaseEvent(QMouseEvent*); signals: }; #endif // MYPUSHBUTTON_H
//playscene.cpp #include "playscene.h" #include <QDebug> #include <QMenu> #include <QMenuBar> #include <QPainter> #include "mypushbutton.h" #include <QTimer> #include <QLabel> #include "mycoin.h" #include "dataconfig.h" #include <QPropertyAnimation> #include <QSound> PlayScene::PlayScene(int levelnum) { QString str = QString("进入了第 %1 关卡 ").arg(levelnum); qDebug()<<str; this->levelIndex = levelnum; //设置窗口固定大小 this->setFixedSize(320,588); //设置图标 this->setWindowIcon(QPixmap(":/res/Coin0001.png")); //设置标题 this->setWindowTitle("翻金币"); //创建菜单栏 QMenuBar * bar = this->menuBar(); this->setMenuBar(bar); //创建开始菜单 QMenu * startMenu = bar->addMenu("开始"); //创建按钮菜单项 QAction * quitAction = startMenu->addAction("退出"); //点击退出 退出游戏 connect(quitAction,&QAction::triggered,[=](){this->close();}); //翻金币音效 QSound *flipSound = new QSound(":/res/ConFlipSound.wav",this); //胜利按钮音效 QSound *winSound = new QSound(":/res/LevelWinSound.wav",this); //返回按钮音效 QSound *backSound = new QSound(":/res/BackButtonSound.wav",this); //返回按钮 MyPushButton * backBtn = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png"); backBtn->setParent(this); backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height()); //返回按钮功能实现 connect(backBtn,&MyPushButton::clicked,[=](){ backSound->play(); QTimer::singleShot(500, this,[=](){ this->hide(); //触发自定义信号,关闭自身,该信号写到 signals下做声明 emit this->chooseSceneBack(); } ); }); //当前关卡标题 QLabel * label = new QLabel; label->setParent(this); QFont font; font.setFamily("华文新魏"); font.setPointSize(20); label->setFont(font); QString str1 = QString("Leavel: %1").arg(this->levelIndex); //QString拼凑代码 label->setText(str1); label->setGeometry(QRect(30, this->height() - 50,120, 50)); //设置大小和位置 //创建金币的背景图片 for(int i = 0 ; i < 4;i++) { for(int j = 0 ; j < 4; j++) { //绘制背景图片 QLabel* label = new QLabel; QPixmap *pix = new QPixmap(":/res/BoardNode(1).png"); label->setGeometry(0,0,pix->width(),pix->height()); label->setPixmap(*pix); label->setParent(this); label->move(57 + i*50,200+j*50); dataConfig config; gameArray[i][j] = config.mData[this->levelIndex][i][j]; QString img; if(gameArray[i][j] == 1) { img = ":/res/Coin0001.png"; } else { img = ":/res/Coin0008.png"; } MyCoin * coin = new MyCoin(img); coin->setParent(this); coin->move(59 + i*50,204+j*50); coin->posX = i; //记录x坐标 coin->posY = j; //记录y坐标 coin->flag =gameArray[i][j]; //记录正反标志 //提前加载胜利图片 QLabel* winLabel = new QLabel; QPixmap tmpPix; tmpPix.load(":/res/LevelCompletedDialogBg.png"); winLabel->setGeometry(0,0,tmpPix.width(),tmpPix.height()); winLabel->setPixmap(tmpPix); winLabel->setParent(this); winLabel->move( (this->width() - tmpPix.width())*0.5 , -tmpPix.height()); //将金币放入到金币的二维数组里,以便后期的维护 coinBtn[i][j] = coin; connect(coin,&MyCoin::clicked,[=](){ flipSound->play(); //防止在游戏胜利后二次点击金币 for(int i = 0 ; i < 4;i++) { for(int j = 0 ; j < 4; j++) { coinBtn[i][j]->isWin = true; } } coin->changeFlag(); gameArray[i][j] = gameArray[i][j] == 0 ? 1 : 0; //数组内部记录的标志同步修改 QTimer::singleShot(300, this,[=](){ //翻转周围硬币 if(coin->posX + 1 <=3) //右侧金币翻转条件判断 { coinBtn[coin->posX + 1][coin->posY]->changeFlag(); gameArray[coin->posX + 1][coin->posY] = gameArray[coin->posX + 1][coin->posY] == 0 ? 1 : 0; } if(coin->posX - 1 >= 0) //左侧金币翻转条件判断 { coinBtn[coin->posX - 1][coin->posY]->changeFlag(); gameArray[coin->posX - 1][coin->posY] = gameArray[coin->posX - 1][coin->posY] == 0 ? 1 : 0; } if(coin->posY + 1 <= 3) //下侧金币翻转条件判断 { coinBtn[coin->posX][coin->posY + 1]->changeFlag(); gameArray[coin->posX][coin->posY + 1] = gameArray[coin->posX][coin->posY + 1] == 0 ? 1 : 0; } if(coin->posY - 1 >= 0) //上侧金币翻转条件判断 { coinBtn[coin->posX][coin->posY - 1]->changeFlag(); gameArray[coin->posX][coin->posY - 1] = gameArray[coin->posX][coin->posY - 1] == 0 ? 1 : 0; } //还原 for(int i = 0 ; i < 4;i++) { for(int j = 0 ; j < 4; j++) { coinBtn[i][j]->isWin = false; } } //判断是否胜利 this->isWin = true; for(int i = 0 ; i < 4;i++) { for(int j = 0 ; j < 4; j++) { //qDebug() << coinBtn[i][j]->flag ; if( coinBtn[i][j]->flag == false) { this->isWin = false; break; } } } if(this->isWin == true) { winSound->play(); qDebug()<<"You win"; for(int i = 0 ; i < 4;i++) { for(int j = 0 ; j < 4; j++) { coinBtn[i][j]->isWin = true; } } QPropertyAnimation * animation1 = new QPropertyAnimation(winLabel,"geometry"); animation1->setDuration(1000); animation1->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height())); animation1->setEndValue(QRect(winLabel->x(),winLabel->y()+114,winLabel->width(),winLabel->height())); animation1->setEasingCurve(QEasingCurve::OutBounce); animation1->start(); } }); }); } } } void PlayScene::paintEvent(QPaintEvent *) { QPainter painter(this); QPixmap pix; pix.load(":/res/PlayLevelSceneBg.png"); painter.drawPixmap(0,0,this->width(),this->height(),pix); //加载标题 pix.load(":/res/Title.png"); pix = pix.scaled(pix.width()*0.5,pix.height()*0.5); painter.drawPixmap( 10,30,pix.width(),pix.height(),pix); }
//playscene.h #ifndef PLAYSCENE_H #define PLAYSCENE_H #include <QMainWindow> #include "mycoin.h" class PlayScene : public QMainWindow { Q_OBJECT public: // explicit PlayScene(QWidget *parent = nullptr); PlayScene(int levelnum); int levelIndex; int gameArray[4][4]; //二维数组数据 void paintEvent(QPaintEvent *); MyCoin * coinBtn[4][4]; bool isWin = true; //是否胜利 signals: void chooseSceneBack(); }; #endif // PLAYSCENE_H
//mycoin.cpp #include "mycoin.h" #include <QDebug> #include <QTimer> MyCoin::MyCoin(QString ImgPath) { QPixmap pix; bool ret = pix.load(ImgPath); if(!ret) { qDebug() << ImgPath << "加载图片失败!"; } this->setFixedSize( pix.width(), pix.height() ); this->setStyleSheet("QPushButton{border:0px;}"); this->setIcon(pix); this->setIconSize(QSize(pix.width(),pix.height())); timer1 = new QTimer(); timer2 = new QTimer(); connect(timer1,&QTimer::timeout,[=](){ QPixmap pixmap; QString str = QString(":/res/Coin000%1.png").arg(this->min++); pixmap.load(str); this->setFixedSize(pixmap.width(),pixmap.height() ); this->setStyleSheet("QPushButton{border:0px;}"); this->setIcon(pixmap); this->setIconSize(QSize(pixmap.width(),pixmap.height())); if(this->min > this->max) //如果大于最大值,重置最小值,并停止定时器 { this->min = 1; isAnimation = false; timer1->stop(); } }); connect(timer2,&QTimer::timeout,[=](){ QPixmap pixmap; QString str = QString(":/res/Coin000%1.png").arg(this->max--); pixmap.load(str); this->setFixedSize(pixmap.width(),pixmap.height() ); this->setStyleSheet("QPushButton{border:0px;}"); this->setIcon(pixmap); this->setIconSize(QSize(pixmap.width(),pixmap.height())); if(this->max < this->min) //如果小于最小值,重置最大值,并停止定时器 { this->max = 8; isAnimation = false; //翻转完之后才执行到这行 timer2->stop(); } }); } void MyCoin::changeFlag() { if(this->flag) { timer1->start(30); isAnimation = true; this->flag = false; } else { timer2->start(30); isAnimation = true; this->flag = true; } } void MyCoin::mousePressEvent(QMouseEvent*e) { if(this->isAnimation || isWin == true) { return; } else { QPushButton::mousePressEvent(e); } }
//mycoin.h #ifndef MYCOIN_H #define MYCOIN_H #include <QPushButton> class MyCoin : public QPushButton { Q_OBJECT public: // explicit MyCoin(QWidget *parent = nullptr); MyCoin(QString ImgPath); int posX; //x坐标 int posY; //y坐标 bool flag; //正反标志 QTimer *timer1; //正面翻反面 定时器 QTimer *timer2; //反面翻正面 定时器 int min = 1; //最小图片 int max = 8; //最大图片 bool isAnimation = false; void mousePressEvent(QMouseEvent*); bool isWin = false; //是否胜利 void changeFlag(); signals: }; #endif // MYCOIN_H
//dataconfig.cpp #include "dataconfig.h" #include <QDebug> dataConfig::dataConfig(QObject *parent) : QObject(parent) { int array1[4][4] = {{1, 1, 1, 1}, {1, 1, 0, 1}, {1, 0, 0, 0}, {1, 1, 0, 1} } ; QVector< QVector<int>> v; for(int i = 0 ; i < 4;i++) { QVector<int>v1; for(int j = 0 ; j < 4;j++) { v1.push_back(array1[i][j]); } v.push_back(v1); } mData.insert(1,v); int array2[4][4] = { {1, 0, 1, 1}, {0, 0, 1, 1}, {1, 1, 0, 0}, {1, 1, 0, 1}} ; v.clear(); for(int i = 0 ; i < 4;i++) { QVector<int>v1; for(int j = 0 ; j < 4;j++) { v1.push_back(array2[i][j]); } v.push_back(v1); } mData.insert(2,v); int array3[4][4] = { {0, 0, 0, 0}, {0, 1, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}} ; v.clear(); for(int i = 0 ; i < 4;i++) { QVector<int>v1; for(int j = 0 ; j < 4;j++) { v1.push_back(array3[i][j]); } v.push_back(v1); } mData.insert(3,v); int array4[4][4] = { {0, 1, 1, 1}, {1, 0, 0, 1}, {1, 0, 1, 1}, {1, 1, 1, 1}} ; v.clear(); for(int i = 0 ; i < 4;i++) { QVector<int>v1; for(int j = 0 ; j < 4;j++) { v1.push_back(array4[i][j]); } v.push_back(v1); } mData.insert(4,v); int array5[4][4] = { {1, 0, 0, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 0, 0, 1}} ; v.clear(); for(int i = 0 ; i < 4;i++) { QVector<int>v1; for(int j = 0 ; j < 4;j++) { v1.push_back(array5[i][j]); } v.push_back(v1); } mData.insert(5,v); int array6[4][4] = { {1, 0, 0, 1}, {0, 1, 1, 0}, {0, 1, 1, 0}, {1, 0, 0, 1}} ; v.clear(); for(int i = 0 ; i < 4;i++) { QVector<int>v1; for(int j = 0 ; j < 4;j++) { v1.push_back(array6[i][j]); } v.push_back(v1); } mData.insert(6,v); int array7[4][4] = { {0, 1, 1, 1}, {1, 0, 1, 1}, {1, 1, 0, 1}, {1, 1, 1, 0}} ; v.clear(); for(int i = 0 ; i < 4;i++) { QVector<int>v1; for(int j = 0 ; j < 4;j++) { v1.push_back(array7[i][j]); } v.push_back(v1); } mData.insert(7,v); int array8[4][4] = { {0, 1, 0, 1}, {1, 0, 0, 0}, {0, 0, 0, 1}, {1, 0, 1, 0}} ; v.clear(); for(int i = 0 ; i < 4;i++) { QVector<int>v1; for(int j = 0 ; j < 4;j++) { v1.push_back(array8[i][j]); } v.push_back(v1); } mData.insert(8,v); int array9[4][4] = { {1, 0, 1, 0}, {1, 0, 1, 0}, {0, 0, 1, 0}, {1, 0, 0, 1}} ; v.clear(); for(int i = 0 ; i < 4;i++) { QVector<int>v1; for(int j = 0 ; j < 4;j++) { v1.push_back(array9[i][j]); } v.push_back(v1); } mData.insert(9,v); int array10[4][4] = { {1, 0, 1, 1}, {1, 1, 0, 0}, {0, 0, 1, 1}, {1, 1, 0, 1}} ; v.clear(); for(int i = 0 ; i < 4;i++) { QVector<int>v1; for(int j = 0 ; j < 4;j++) { v1.push_back(array10[i][j]); } v.push_back(v1); } mData.insert(10,v); int array11[4][4] = { {0, 1, 1, 0}, {1, 0, 0, 1}, {1, 0, 0, 1}, {0, 1, 1, 0}} ; v.clear(); for(int i = 0 ; i < 4;i++) { QVector<int>v1; for(int j = 0 ; j < 4;j++) { v1.push_back(array11[i][j]); } v.push_back(v1); } mData.insert(11,v); int array12[4][4] = { {0, 1, 1, 0}, {0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}} ; v.clear(); for(int i = 0 ; i < 4;i++) { QVector<int>v1; for(int j = 0 ; j < 4;j++) { v1.push_back(array12[i][j]); } v.push_back(v1); } mData.insert(12,v); int array13[4][4] = { {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 1, 1, 0}} ; v.clear(); for(int i = 0 ; i < 4;i++) { QVector<int>v1; for(int j = 0 ; j < 4;j++) { v1.push_back(array13[i][j]); } v.push_back(v1); } mData.insert(13,v); int array14[4][4] = { {1, 0, 1, 1}, {0, 1, 0, 1}, {1, 0, 1, 0}, {1, 1, 0, 1}} ; v.clear(); for(int i = 0 ; i < 4;i++) { QVector<int>v1; for(int j = 0 ; j < 4;j++) { v1.push_back(array14[i][j]); } v.push_back(v1); } mData.insert(14,v); int array15[4][4] = { {0, 1, 0, 1}, {1, 0, 0, 0}, {1, 0, 0, 0}, {0, 1, 0, 1}} ; v.clear(); for(int i = 0 ; i < 4;i++) { QVector<int>v1; for(int j = 0 ; j < 4;j++) { v1.push_back(array15[i][j]); } v.push_back(v1); } mData.insert(15,v); int array16[4][4] = { {0, 1, 1, 0}, {1, 1, 1, 1}, {1, 1, 1, 1}, {0, 1, 1, 0}} ; v.clear(); for(int i = 0 ; i < 4;i++) { QVector<int>v1; for(int j = 0 ; j < 4;j++) { v1.push_back(array16[i][j]); } v.push_back(v1); } mData.insert(16,v); int array17[4][4] = { {0, 1, 1, 1}, {0, 1, 0, 0}, {0, 0, 1, 0}, {1, 1, 1, 0}} ; v.clear(); for(int i = 0 ; i < 4;i++) { QVector<int>v1; for(int j = 0 ; j < 4;j++) { v1.push_back(array17[i][j]); } v.push_back(v1); } mData.insert(17,v); int array18[4][4] = { {0, 0, 0, 1}, {0, 0, 1, 0}, {0, 1, 0, 0}, {1, 0, 0, 0}} ; v.clear(); for(int i = 0 ; i < 4;i++) { QVector<int>v1; for(int j = 0 ; j < 4;j++) { v1.push_back(array18[i][j]); } v.push_back(v1); } mData.insert(18,v); int array19[4][4] = { {0, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 1, 1}, {0, 0, 0, 0}} ; v.clear(); for(int i = 0 ; i < 4;i++) { QVector<int>v1; for(int j = 0 ; j < 4;j++) { v1.push_back(array19[i][j]); } v.push_back(v1); } mData.insert(19,v); int array20[4][4] = { {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}} ; v.clear(); for(int i = 0 ; i < 4;i++) { QVector<int>v1; for(int j = 0 ; j < 4;j++) { v1.push_back(array20[i][j]); } v.push_back(v1); } mData.insert(20,v); //测试数据 // for( QMap<int, QVector< QVector<int> > >::iterator it = mData.begin();it != mData.end();it++ ) // { // for(QVector< QVector<int> >::iterator it2 = (*it).begin(); it2!= (*it).end();it2++) // { // for(QVector<int>::iterator it3 = (*it2).begin(); it3 != (*it2).end(); it3++ ) // { // qDebug() << *it3 ; // } // } // qDebug() << endl; // } }
//dataconfig.h #ifndef DATACONFIG_H #define DATACONFIG_H #include <QObject> #include <QMap> #include <QVector> class dataConfig : public QObject { Q_OBJECT public: explicit dataConfig(QObject *parent = 0); public: QMap<int, QVector< QVector<int> > >mData; signals: public slots: }; #endif // DATACONFIG_H
//CoinFlip.pro QT += core gui multimedia greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++11 # The following define makes your compiler emit warnings if you use # any Qt feature that has been marked deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += chooselevelscene.cpp dataconfig.cpp main.cpp mainscene.cpp mycoin.cpp mypushbutton.cpp playscene.cpp HEADERS += chooselevelscene.h dataconfig.h mainscene.h mycoin.h mypushbutton.h playscene.h FORMS += mainscene.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target RESOURCES += res.qrc
//main.cpp #include "mainscene.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainScene w; w.show(); return a.exec(); }