QT - Day 6
跟着视频教程创建了翻金币的项目,花了好几个晚上才学习完。
视频地址:最新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();
}
QT - Day 6的更多相关文章
- QT内省机制、自定义Model、数据库
本文将介绍自定义Model过程中数据库数据源的获取方法,我使用过以下三种方式获取数据库数据源: 创建 存储对应数据库所有字段的 结构体,将结构体置于容器中返回,然后根据索引值(QModelIndex) ...
- Ubuntu 下安装QT
Ubuntu 下安装QT 本文使用的环境 QT Library: qt-everywhere-opensource-src-4.7.4.tar.gz QT Creator: qt-creator-li ...
- Qt安装配置
Qt Creator: 下载: Qt 5.5.1 for Windows 32-bit(MinGW 4.9.2, 1.0 GB):http://download.qt.io/official_rele ...
- Qt信号与槽自动关联机制
参考链接1:http://blog.csdn.net/skyhawk452/article/details/6121407 参考链接2:http://blog.csdn.net/memory_exce ...
- 保持Qt GUI响应的几种方法
最开始使用Qt时就遇到过QT Gui失去响应的问题,我是用多线程的方式解决的,然而通常来说,多线程是会降低程序的运行速度. 之后,在使用QSqlQuery::execBatch()函数时,Qt Gui ...
- Qt 中使用Singleton模式需小心
在qt中,使用Singleton模式时一定要小心.因为Singleton模式中使用的是静态对象,静态对象是直到程序结束才被释放的,然而,一旦把该静态对象纳入了Qt的父子对象体系,就会导致不明确的行为. ...
- Qt——组件位置随窗口变化
当我们用Qt Designer设计界面时,有时会面临这样一个问题:需要在窗口指定位置放置组件,并且当窗口位置大小改变时,该组件相对其父对象的位置是不变的,如下面两幅图所示 ,首先看上面这幅图,注意bu ...
- (转) Qt 出现“undefined reference to `vtable for”原因总结
由于Qt本身实现的机制所限,我们在使用Qt制作某些软件程序的时候,会遇到各种各样这样那样的问题,而且很多是很难,或者根本找不到原因的,即使解决了问题,如果有人问你为什么,你只能回答--不知道. 今天我 ...
- qt中ui的 使用介绍
1.什么是ui?ui通常是用Qt 设计师设计出来的界面文件的后缀.通常情况下ui是一个指向这个界面类的指针.ui-> 一般就是用来访问这个界面类里面的控件.例如你的ui文件里有一个叫okButt ...
- Qt 开启鼠标跟踪,自动激活mouseMoveEvent的问题
最近在Qt上实现一个功能,鼠标在图片上移动,触发mouseMoveEvent事件,进而生成一个小的半透明窗口,放大显示以鼠标为中心的一个区域的图像并随鼠标移动.但是,必须鼠标摁下,才触发mouseMo ...
随机推荐
- [转帖]Jmeter之JDBC Request使用方法(oracle)
https://zhuanlan.zhihu.com/p/121747788 JDBC Request: 这个sampler可以向数据库发送一个jdbc请求(sql语句),它经常需要和JDBC Con ...
- [转帖]通过配置优化KingbaseES服务器性能
目录 1. 概述 2. 数据库应用类型 3. 服务器参数 3.1. max_connections 3.2. shared_buffers 3.3. effective_cache_size 3.4. ...
- [转帖]Linux性能分析:理解系统平均负载
Linux系统中,平均负载是指单位时间内,处于可运行状态和不可中断状态的进程数.它不仅包括了正在使用CPU的进程,也包括处于不可打断的睡眠状态的进程-它们是在等待其它系统资源如磁盘 I/O 等的进程. ...
- [LeetCode刷题记录]39 组合总和
题目描述 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的数字可以无限制 ...
- pycharm的docstring多了一行type
注释中多了一行:type 设置为Epytext PyCharm 2020.3.5 (Community Edition) def test(param1,param2,param3): "& ...
- 设计模式学习-使用go实现备忘录模式
备忘录模式 定义 优点 缺点 适用范围 代码实现 参考 备忘录模式 定义 备忘录( Memento ):在不破坏封装性的前提下,获取一个对象的内部状态,并在该对象之处保存该状态.这样以后就可将该对象恢 ...
- 【三】AI Studio 项目详解——单机多机训练分布式训练--PARL
相关文章 [一]-环境配置+python入门教学 [二]-Parl基础命令 [三]-Notebook.&pdb.ipdb 调试 [四]-强化学习入门简介 [五]-Sarsa&Qlear ...
- .net ELk 成功使用
原文地址: http://t.zoukankan.com/shousiji-p-15222276.html
- Gin 响应方式
响应 1. 字符串方式 r.GET("/user/save", func(ctx *gin.Context) { ctx.String(http.StatusOK, "t ...
- IDEA破解(无限重启激活时间版)
下载地址[将下载的目录打成zip压缩包后使用]:「ide-eval-resetter」https://www.aliyundrive.com/s/UFHpDX5d6Xv 点击链接保存,或者复制本段内容 ...