在visual studio2019中编写Qt5.14版本的程序

b站一个老师的课程,跟着做的BV1g4411H78N


环境是visual studio 2019    qt 5.14.2

5.14版本是最后一个安装不强制要求注册的版本,安装时记得断网(要不要注册)

ui是用.的,与教程不一样

(我创建的项目最后的显示效果有点卡,特别是点金币多了之后)

在visual studio中安装了Qt的插件!!!这个很重要

扩展-管理扩展-搜索-qt-选择第一个就好

等待安装完成(这是龟速,请耐心等待)

在ui文件右键 界面中选择点击属性

qtuser interface 的 uic 中的 outputfile Name前面加上"./"

!!!这个也很重要

扩展qt vs tool ---- qt project setting ---- Qt project settings ---- Qt modules中点击select modules 勾选添加要添加的模块

qt user interface compiler -- uic -- Output File Name ----改为------ ./ui_%(Filename).h

.qrc文件双击就可以添加文件

如果ui或者qrc文件无法打开的,右键点击打开方式,添加一个新的方式,选择qt---版本号---bin 安装目录中能打开对应文件的程序(上网能搜到更详细的方式)

有时候visual studio界面的东西明明在ui文件中已经创建,但是就是报错(先保存看一下有没有出现),关闭项目再打开就行(这能解决90%以上的问题),如果重新打开还是没有智能提示,看一下./ui_%(Filename).h这步做了没有

扩展里面 qt -- qt version中选择qmake文件,qt安装路径 -- 版本号--运行环境(32位、64位、arm?/x86、windows/linux) -  bin ,然后给它一个名字

写好了类头文件中的函数声明后,将鼠标光标点击到函数中间,alt+enter添加定义\声明,就能直接添加函数体,(不用一个字一个字地敲,还要写上作用域)

有一些奇奇怪怪的报错很有可能是没有引入头文件造成的,请检查头文件

再写上一些visual studio快捷键,这几个经常用

shift+del 快速删除一行

ctrl+j 智能提示

ctrl+d 复制一行


// ChooseLevelScene.h
#pragma once#include <QObject>
#include <qmainwindow.h>
#include <qdebug.h>
#include <qstring.h>
#include <qmenubar.h>
#include <qaction.h>
#include <qmenu.h>
#include <qmenubar.h>
#include <qpainter.h>
#include <qtimer.h>
#include <qlabel.h>
#include <qsound.h>
using namespace std;#include "MyPushButton.h"
#include "PlayScene.h"class ChooseLevelScene  : public QMainWindow
{Q_OBJECTpublic:ChooseLevelScene(QMainWindow* parent);~ChooseLevelScene();void paintEvent(QPaintEvent* event);PlayScene* play = NULL;
signals:void chooseSceneBack();
public slots:};// ChooseLevelScene.cpp
#include "ChooseLevelScene.h"ChooseLevelScene::ChooseLevelScene(QMainWindow* parent): QMainWindow(parent)
{this->setFixedSize(1080, 1920);this->setWindowIcon(QPixmap(":/icon/Coin0001.png"));this->setWindowTitle(QStringLiteral("选择关卡的场景"));// 选择关卡音效QSound* chooseSound = new QSound(":/icon/TapButtonSound.wav", this);// 返回按钮音效QSound* backSound = new QSound(":/icon/BackButtonSound.wav", this);QMenuBar* bar = menuBar();setMenuBar(bar);QMenu * startMenu = bar->addMenu(QStringLiteral("开始"));QAction* quitAction = startMenu->addAction(QStringLiteral("退出"));connect(quitAction, &QAction::triggered, [=]() {this->close();});/*connect(chooseScene, &ChooseLevelScene::chooseSceneBack, this, [=]() {chooseScene->hide();this->show();});*/MyPushButton* backBtn = new MyPushButton(":/icon/BackButton.png", ":/icon/BackButtonSelected.png");backBtn->setParent(this);backBtn->move(this->width() - backBtn->width(), this->height() - backBtn->height());backBtn->show();connect(backBtn, &MyPushButton::clicked, [=]() {backSound->play();qDebug() << QStringLiteral("点击了返回按钮").toUtf8().data();QTimer::singleShot(200, this, [=]() {emit this->chooseSceneBack();});});for (int i = 0; i < 5; i++){for (int j = 0; j < 4; j++){MyPushButton* menuBtn = new MyPushButton(":/icon/LevelIcon.png");menuBtn->setParent(this);menuBtn->move(180 + j * 200, 600 + i * 180);connect(menuBtn, &MyPushButton::clicked, [=]() {chooseSound->play();QString str = QStringLiteral("您选择的是第%1关").arg(4 * i + j + 1);qDebug() << str.toUtf8().data();this->hide();    // 将选关场景隐藏play = new PlayScene(4 * i + j + 1);play->show();play->setGeometry(this->geometry()); // 设置游戏场景的初始位置play->setAttribute(Qt::WA_DeleteOnClose);// 关卡返回connect(play, &PlayScene::chooseSceneBack, [=]() {play->close();   // 关闭关卡场景this->setGeometry(play->geometry());this->show(); });});QLabel* label = new QLabel;label->setParent(menuBtn);QFont ft;ft.setPointSize(20);ft.setBold(true);label->setFont(ft);label->setFixedSize(menuBtn->width(), menuBtn->height());label->setText(QStringLiteral("%1").arg(i * 4 + j + 1));label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);}}
}ChooseLevelScene::~ChooseLevelScene()
{}void ChooseLevelScene::paintEvent(QPaintEvent * event)
{QPainter painter(this);QPixmap pix;pix.load(":/icon/OtherSceneBg.png");painter.drawPixmap(0, 0, this->width(), this->height(), pix);QPixmap pix2;pix2.load(":/icon/Title.png");painter.drawPixmap((this->width() - pix.width()) * 0.5, 30, pix2.width(), pix2.height(), pix2);}// DataConfig.h
#pragma once#include <QObject>
#include <qmap.h>
#include <qvector.h>
using namespace std;
class DataConfig  : public QObject
{Q_OBJECTpublic:DataConfig(QObject *parent = 0);~DataConfig();QMap<int, QVector<QVector<int>>> mData;
};// DataConfig.cpp
#include "DataConfig.h"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);
}DataConfig::~DataConfig()
{}// MainWindow.h
#pragma once#include <QMainWindow>
#include "ui_MainWindow.h"
#include <qpixmap.h>
#include <qstring.h>
#include <qsize.h>
#include <qaction.h>
#include <QPaintEvent>
#include <qpainter.h>
#include <qpixmap.h>
#include <qtimer.h>
#include <qsound.h>
using namespace std;#include "MyPushButton.h"
#include "ChooseLevelScene.h"class MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();void paintEvent(QPaintEvent* event);MyPushButton* startBtn;ChooseLevelScene* chooseScene;
private:Ui::MainWindowClass ui;
};// MainWindow.cpp
#include "MainWindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent)
{ui.setupUi(this);this->resize(QSize(1080, 1920));// this->setFixedSize(1080, 1920);this->setWindowIcon(QPixmap(":/icon/Coin0001.png"));this->setWindowTitle(QStringLiteral("翻金币"));connect(ui.actionout, &QAction::triggered, [=]() {this->close();});// 开始音效QSound* startSound = new QSound(":/icon/TapButtonSound.wav", this);startBtn = new MyPushButton(":/icon/MenuSceneStartButton.png");startBtn->setParent(this);startBtn->move(this->width() * 0.5 - startBtn->width() * 0.5, this->height() * 0.7);chooseScene = new ChooseLevelScene(this);connect(chooseScene, &ChooseLevelScene::chooseSceneBack, this, [=]() {this->setGeometry(chooseScene->geometry());chooseScene->hide();this->show();});connect(startBtn, &MyPushButton::clicked, [=]() {startSound->play();qDebug() << QStringLiteral("点击了开始").toUtf8().data();startBtn->zoom1();startBtn->zoom2();// 进入选择关卡场景中QTimer::singleShot(500, this, [=]() {this->setGeometry(chooseScene->geometry());this->hide();chooseScene->show();});});
}MainWindow::~MainWindow()
{}void MainWindow::paintEvent(QPaintEvent * event)
{QPainter painter(this);QPixmap pix;    // 底图pix.load(":/icon/PlayLevelSceneBg.png");painter.drawPixmap(0, 0, this->width(), this->height(), pix);QPixmap pix2; // 标题图pix2.load(":/icon/Title.png");// 缩放到合适大小float scaleSize = min(((float)this->width() / (float)pix2.width()), (float)0.3 * ((float)this->height() / (float)pix2.height()));pix2 = pix2.scaled((int)(scaleSize * (float)pix2.width() * 0.8), (int)(scaleSize * (float)pix2.height() * 0.8));painter.drawPixmap((int)0.1 * scaleSize, (int)0.3 * scaleSize, pix2.width(), pix2.height(), pix2);
}// MyCoin.h
#pragma once
#include <QPushButton>
#include <qstring.h>
#include <qtimer.h>
#include <qwidget.h>
#include <qpixmap.h>
#include <qsize.h>
#include <qdebug.h>
using namespace std;class MyCoin  : public QPushButton
{Q_OBJECTpublic:// MyCoin(QWidget *parent);MyCoin(QString butImg);~MyCoin();void changeFlag();  // 改变标志,执行翻转效果void mousePressEvent(QMouseEvent* event); // 重写按下事件int posX;  // x坐标位置int posY;   // y坐标位置bool flag;  // 正反标示QTimer* timer1;  // 正面翻反面,定时器QTimer* timer2; // 反面翻正面,定时器int min = 1;   // 最小图片int max = 8;    // 最大图片bool isAnimation = false;   // 执行动画标志bool isWin = false;
signals:};// MyCoin.cpp
#include "MyCoin.h"MyCoin::MyCoin(QString butImg)
{QPixmap pixmap;bool ret = pixmap.load(butImg);if (!ret){qDebug() << butImg.toUtf8().data() << QStringLiteral("加载图片失败");}this->setFixedSize(pixmap.width(), pixmap.height());this->setStyleSheet("QPushButton{border:0px;}");this->setIcon(pixmap);this->setIconSize(QSize(pixmap.width(), pixmap.height()));timer1 = new QTimer(this);timer2 = new QTimer(this);flag = true;posX = 0;posY = 0;// 正面变反面connect(timer1, &QTimer::timeout, [=]() {QPixmap pixmap;QString str = QString(":/icon/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) // 如果翻面了,将min重置为1{this->min = 1;timer1->stop();isAnimation = false;}});// 反面变正面connect(timer2, &QTimer::timeout, [=]() {QPixmap pixmap;QString str = QString(":/icon/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;timer2->stop();isAnimation = false;}});
}MyCoin::~MyCoin()
{}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* event)
{if (this->isAnimation || this->isWin == true){return;}else{return QPushButton::mousePressEvent(event);}
}// MyPushButton.h
#pragma once#include <QObject>
#include <qwidget.h>
#include <qstring.h>
#include <qdebug.h>
#include <qpushbutton.h>
#include <qpropertyanimation.h>
#include <QMouseEvent>
#include <qmouseeventtransition.h>using namespace std;
class MyPushButton  : public QPushButton
{Q_OBJECTpublic:MyPushButton(QString normalImg, QString pressImg = "");~MyPushButton();void zoom1();void zoom2();void mousePressEvent(QMouseEvent* event);void mouseReleaseEvent(QMouseEvent* event);QString normalImgPath;QString pressImgPath;
};// MyPushButton.cpp
#include "MyPushButton.h"MyPushButton::MyPushButton(QString normalImg, QString pressImg)
{normalImgPath = normalImg;pressImgPath = pressImg;QPixmap pixmap;bool ret = pixmap.load(normalImgPath);if (! ret){qDebug() << normalImg.toUtf8().data() << QStringLiteral("加载图片失败");}// 设置图片的固定尺寸this->setFixedSize(pixmap.width(), pixmap.height());// 设置不规则图片的样式表this->setStyleSheet("QPushButton{border:0px;}");this->setIcon(pixmap);this->setIconSize(QSize(pixmap.width(), pixmap.height()));
}MyPushButton::~MyPushButton()
{}void MyPushButton::zoom1()
{QPropertyAnimation* animation1 = new QPropertyAnimation(this, "geometry");animation1->setDuration(200);  // 设置时间间隔,单位为毫秒animation1->setStartValue(QRect(this->x(), this->y(), this->width(), this->height()));animation1->setEndValue(QRect(this->x(), this->y() + 10, this->width(), this->height()));animation1->setEasingCurve(QEasingCurve::OutBounce);animation1->start();
}void MyPushButton::zoom2()
{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()));animation1->setEasingCurve(QEasingCurve::OutBounce);animation1->start();
}void MyPushButton::mousePressEvent(QMouseEvent* event)
{if (this->pressImgPath != ""){QPixmap pix;bool ret = pix.load(this->pressImgPath);if (!ret){qDebug() << QStringLiteral("图片加载失败").toUtf8().data();}this->setFixedSize(pix.width(), pix.height());this->setStyleSheet("QPushButton{border:0px;}");this->setIcon(pix);this->setIconSize(QSize(pix.width(), pix.height()));}// 让父类完成return QPushButton::mousePressEvent(event);
}void MyPushButton::mouseReleaseEvent(QMouseEvent* event)
{if (this->pressImgPath != ""){QPixmap pix;bool ret = pix.load(this->normalImgPath);if (!ret){qDebug() << QStringLiteral("图片加载失败").toUtf8().data();}this->setFixedSize(pix.width(), pix.height());this->setStyleSheet("QPushButton{border:0px;}");this->setIcon(pix);this->setIconSize(QSize(pix.width(), pix.height()));}// 让父类完成其他内容return QPushButton::mouseReleaseEvent(event);
}// PlayScene.h
#pragma once#include <QMainWindow>
#include <qstring.h>
#include <qdebug.h>
#include <qmenu.h>
#include <qmenubar.h>
#include <QMouseEvent>
#include <qpainter.h>
#include <qtimer.h>
#include <qlabel.h>
#include <qfont.h>
#include <qmediaplayer.h>
#include <qpropertyanimation.h>
#include <qsound.h>
using namespace std;#include "MyPushButton.h"
#include "MyCoin.h"
#include "DataConfig.h"class PlayScene  : public QMainWindow
{Q_OBJECTpublic:PlayScene(int levelNum);~PlayScene();// void icomShow(QString path1, QString path2, QPainter* painter);void paintEvent(QPaintEvent* event);int levelIndex; // 记录所选的关卡int gameArray[4][4];map<int, vector<vector<int>>>mData;bool isWin = false;MyCoin* coinBtn[4][4];//QMediaPlayer* endPlayer;
signals:void chooseSceneBack();
};// PlayScene.cpp
#include "PlayScene.h"PlayScene::PlayScene(int levelNum)
{qDebug() << QStringLiteral("进入了第%1关").arg(levelNum).toUtf8().data();this->levelIndex = levelNum;this->setFixedSize(1080, 1920);                             // 设置固定大小this->setWindowIcon(QPixmap(":/icon/Coin0001.png"));      // 设置图标this->setWindowTitle(QStringLiteral("翻金币场景"));          // 设置标题QSound* flipSound = new QSound(":/icon/ConFlipSound.wav");    // 翻金币音效QSound* winSound = new QSound(":/icon/LevelWinSound.wav");   // 胜利音效QMenuBar* bar = menuBar();setMenuBar(bar);QMenu* startMenu = bar->addMenu(QStringLiteral("开始"));QAction* quitAction = startMenu->addAction(QStringLiteral("退出"));connect(quitAction, &QAction::triggered, [=]() {this->close();});DataConfig config;// 初始化每个关卡的二维数组for (int i = 0; i < 4; i++){for (int j = 0; j < 4; j++){gameArray[i][j] = config.mData[this->levelIndex][i][j];// qDebug() << QStringLiteral("coin") << this->levelIndex;}}QLabel* winLabel = new QLabel;QPixmap tmpPix;tmpPix.load(":/icon/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() - 200);// 设置背景与金币for (int i = 0; i < 4; i++){for (int j = 0; j < 4; j++){QLabel label;label.setGeometry(0, 0, 50, 50);QPixmap map1;map1.load(":/icon/BoardNode.png");// QPixmap fitpixmap = map1.scaled(3 * map1.width(), 3 * map1.height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);// map1.scaled(3 * map1.width(), 3 * map1.height());label.setGeometry(0, 0, map1.width(), map1.height());label.setPixmap(map1);label.setParent(this);label.move(400 + i * 50, 800 + j * 50);QString str;if (this->gameArray[i][j] == 1)    // 显示金币{str.append(":/icon/Coin0001.png");}else   // 显示银币{str.append(":/icon/Coin0008.png");}MyCoin* coin = new MyCoin(str);coinBtn[i][j] = coin;coin->setParent(this);coin->move(402 + i * 50, 803 + j * 50);coin->posX = i;coin->posY = j;coin->flag = gameArray[i][j];connect(coin, &MyCoin::clicked, [=]() {flipSound->play();for (int i = 0; i < 4; i++){for (int j = 0; j < 4; j++){this->coinBtn[i][j]->isWin = true;}}coin->changeFlag();this->gameArray[i][j] = this->gameArray[i][j] == 0 ? 1 : 0;QTimer::singleShot(300, this, [=]() {if (coin->posX + 1 <= 3){coinBtn[coin->posX + 1][coin->posY]->changeFlag();// 周围右侧金币翻转this->gameArray[coin->posX + 1][coin->posY] = this->gameArray[coin->posX + 1][coin->posY] == 0 ? 1 : 0;}if (coin->posX - 1 >= 0){coinBtn[coin->posX - 1][coin->posY]->changeFlag();// 周围左侧金币翻转this->gameArray[coin->posX - 1][coin->posY] = this->gameArray[coin->posX - 1][coin->posY] == 0 ? 1 : 0;}if (coin->posY + 1 <= 3){coinBtn[coin->posX][coin->posY + 1]->changeFlag();// 周围下侧金币翻转this->gameArray[coin->posX][coin->posY + 1] = this->gameArray[coin->posX][coin->posY + 1] == 0 ? 1 : 0;}if (coin->posY - 1 >= 0){coinBtn[coin->posX][coin->posY - 1]->changeFlag();// 周围上侧金币翻转this->gameArray[coin->posX][coin->posY - 1] = this->gameArray[coin->posX][coin->posY - 1] == 0 ? 1 : 0;}// 游戏胜利判断this->isWin = true;for (int m = 0; m < 4; m++){for (int n = 0; n < 4; n++){if (coinBtn[m][n]->flag == false){this->isWin = false;break;}}}if (this->isWin == true){winSound->play();qDebug() << QStringLiteral("胜利了").toUtf8().data();QPropertyAnimation* animation = new QPropertyAnimation(winLabel, "geometry");// 设置时间间隔animation->setDuration(1000);// 设置开始位置animation->setStartValue(QRect(winLabel->x(), winLabel->y(), winLabel->width(), winLabel->height()));// 设置结束位置animation->setEndValue(QRect(winLabel->x(), winLabel->y() + 300, winLabel->width(), winLabel->height()));// 设置缓和曲线animation->setEasingCurve(QEasingCurve::OutBounce);animation->start();}else{for (int i = 0; i < 4; i++){for (int j = 0; j < 4; j++){this->coinBtn[i][j]->isWin = false;}}}});});}}
}PlayScene::~PlayScene()
{}void PlayScene::paintEvent(QPaintEvent * event)
{// 创建背景QPainter painter(this);QPixmap pix;pix.load(":/icon/PlayLevelSceneBg.png");painter.drawPixmap(0, 0, this->width(), this->height(), pix);// 加载标题QPixmap pix2;pix2.load(":/icon/Title.png");painter.drawPixmap((this->width() - pix.width()) * 0.5, 30, pix2.width(), pix2.height(), pix2);MyPushButton* backBtn = new MyPushButton(":/icon/BackButton.png", ":/icon/BackButtonSelected.png");backBtn->setParent(this);backBtn->move(this->width() - backBtn->width(), this->height() - backBtn->height());backBtn->show();connect(backBtn, &MyPushButton::clicked, [=]() {qDebug() << QStringLiteral("翻金币中,点击了返回按钮").toUtf8().data();QTimer::singleShot(200, this, [=]() {emit this->chooseSceneBack();});});// 显示当前关卡数QLabel* label = new QLabel;label->setParent(this);QString labelText;labelText.append(QStringLiteral("当前关卡:"));labelText.append(QString::number(this->levelIndex));label->setText(labelText);QFont ft;ft.setPointSize(48);ft.setBold(true);label->setFont(ft);label->move(30, 1600);label->show();
}

在visual studio2019中编写Qt5.14版本的程序 BV1g4411H78N相关推荐

  1. 如何在ASP.NET Core中编写自定义日志记录提供程序

    目录 介绍 如何实现所需的接口 基础类和附件 FileLoggerProvider具体类及其附件 1. ConfigureLogging() 2. appsettings.json文件 介绍 源代码可 ...

  2. Visual Studio中的第一个Django-Python应用程序

    目录 介绍 创建一个项目 添加虚拟环境 结论 介绍 Python是一种非常强大且流行的编程语言,用于机器学习.人工智能.数据科学. 但是,如果您正在考虑使用此python创建一个Web应用程序,它可以 ...

  3. c语言 单词变复数_DEV-C++中编写了一段C程序,其中设置了用文件进行... 请编一个程序,可以将英语规则名词由单数变成复数......

    导航:网站首页 > DEV-C++中编写了一段C程序,其中设置了用文件进行... 请编一个程序,可以将英语规则名词由单数变成复数... DEV-C++中编写了一段C程序,其中设置了用文件进行.. ...

  4. 在VS中编写Qt5涉及到的一点字符串问题

    在VS中开发Qt5程序似乎成了我的一种习惯,因为觉得VS简直不能再溜了,不愧是"宇宙第一IDE". 所以在开发中难免会遇到中文显示乱码的问题,网上找到的很多方法都是类似于下面这样的 ...

  5. vs2019使用python进行数据可视化_在Visual Studio2019中使用汇编语言编写程序

    开始写代码.这里,我们就使用Irvine库提供的过程来实现一些小功能.首先,改变控制台窗口的背景色与前景色,接着每隔一秒,在控制台窗口打印一个随机的英文小写字母,最后弹出一个windows消息弹框.代 ...

  6. 在Visual Studio中编写Epicor10的BPM代码

    1.搭建Visual Studio编写BPM的环境 a.共享或复制服务端Server文件 BPM是运行在Epicor服务器上的,因此编写BPM代码是需要服务端文件支持的, 所以需要把服务端Server ...

  7. Xcode中通过删除原先版本的程序来复位App

    可以在Xcode菜单中点击 Product->Clean Build Folder (按住Option键,在windows键盘中是Alt键.) 此时Xcode将会从设备中删除(卸载uninsta ...

  8. 如何在 VS Code 中编写、运行C语言程序 教程

    本篇目录 前言 1.下载.安装VS Code 2.安装VS code中2个插件 3.下载minGW64 4.配置系统的环境变量 5.C语言配置 6.编写一个测试程序 7.可能存在的问题 总结 前言 折 ...

  9. 如何在Visual Studio中直接使用示例代码浏览器搜索下载和管理代码示例

    今天微软一站式示例代码库团队发布了示例代码浏览器的Visual Studio 扩展.有了它,开发人员就可以在Visual Studio 2010里直接通过Alt+F1快捷键搜索示例代码,并方便地对示例 ...

最新文章

  1. 毕业五年,几个月入百万阿里系大神的公众号!
  2. 公司升级ERP软件的三大诱因
  3. 高性能NIO框架Netty入门篇
  4. 机器运算知识点计算机组成原理,计算机组成原理考研知识点非常全
  5. python推荐系统-python 简易推荐系统实现
  6. matlab中鼠标光标后面的阴影怎么去除,UG在绘图是拖动鼠标出现残影怎么回事?看看这个方法就知道了...
  7. 软件工程专业指导1(定义内涵)
  8. 使用powermock 测试static 方法,jacoco统计覆盖率问题
  9. 206.12.15随笔--最近内心的一些想法
  10. Dubbo学习总结(1)——Dubbo入门基础与实例讲解
  11. lua mysql 事务_为什么在 Redis 实现 Lua 脚本事务?-阿里云开发者社区
  12. Mybatis高级映射一对一查询
  13. 桌面软件开发框架大赏
  14. OA软件详细功能模块列表
  15. openstackdvr模式
  16. 吴伯凡-认知方法论-原始舒适区=0认知
  17. 苹果公司发布iPhone 5s和iPhone 5c
  18. 如何正确接触天使投资人?
  19. 什么是feature map
  20. 一天写多少行代码才算是好程序员?

热门文章

  1. css中设置交叉轴内容为拉伸,CSS-弹性布局2-交叉轴
  2. Armadillo使用介绍(八):第二个Armadillo程序
  3. LENOVO_WIN7_UM_32_ZH_CN_RDVD.iso
  4. 以社交活动的方式做计划-乐高公司的规模化敏捷
  5. MySQL 一条语句实现若记录存在则更新,不存在则插入
  6. Js 之移动端图片上传插件mbUploadify
  7. PHP住院收费系统管理,医院住院管理系统(VB+mssql)
  8. 使用FP-growth算法来高效发现频繁项集
  9. 瀬名 - 想ひ出小往
  10. 新年开单必备:12个国家买家谈判风格差异get,外贸人记得转发收藏...