1 Star 1 Fork 0

nullptr/基于Qt开发的推箱子游戏(内含自制地图)

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
makemap.cpp 3.75 KB
一键复制 编辑 原始数据 按行查看 历史
nullptr 提交于 1年前 . Tenth
#include "makemap.h"
#include "ui_makemap.h"
#include"QString"
#include"qfiledialog.h"
#include"QMessageBox"
#include"qpainter.h"
#include"QPushButton"
makeMap::makeMap(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::makeMap)
{
ui->setupUi(this);
this->resize(868,740);
this->setWindowIcon(QIcon(":/image/Man.png"));
this->setWindowTitle("自制地图");
but=new QPushButton(this);
but->move(110,550);
but->resize(108,51);
connect(but,&QPushButton::clicked,this,&QMainWindow::close);
but->setText("退出");
but1=new QPushButton(this);
but1->move(110,360);
but1->resize(108,51);
connect(but1,&QPushButton::clicked,this,[this](){this->onActionSaveTriggered(1);});
but1->setText("保存");
mPainter=new QPainter(this);
//功能连接
this->connect(ui->actionNew,SIGNAL(triggered(bool)),this,SLOT(onActionNewTriggered(bool)));
this->connect(ui->actionOpen,SIGNAL(triggered(bool)),this,SLOT(onActionOpenTriggered(bool)));
this->connect(ui->actionPaste,SIGNAL(triggered(bool)),this,SLOT(onActionPasteTriggered(bool)));
this->connect(ui->actionRedo,SIGNAL(triggered(bool)),this,SLOT(onActionRedoTriggered(bool)));
this->connect(ui->actionUndo,SIGNAL(triggered(bool)),this,SLOT(onActionUndoTriggered(bool)));
this->connect(ui->actionSave,SIGNAL(triggered(bool)),this,SLOT(onActionSaveTriggered(bool)));
this->connect(ui->actionSaveas,SIGNAL(triggered(bool)),this,SLOT(onActionSaveasTriggered(bool)));
this->saved=true;
this->connect(ui->textEdit,SIGNAL(textChanged()),this,SLOT(onEdited()));
}
void makeMap::paintEvent(QPaintEvent *event)
{
mPainter->begin(this);
QPainter BackgroundPainter(this);
BackgroundPainter.drawImage(QRect(321,35,540,710),QImage(":/image/tips.png"));
mPainter->end();
}
void makeMap::onActionSaveTriggered(bool checked)
{
if(savepath.isEmpty())
{
QString file_path;
if(file_path.isEmpty())
{
file_path=QFileDialog::getSaveFileName(this,"选择路径",".","*.txt");
}
this->savepath=file_path;
}
QFile txt(this->savepath);
bool tag=txt.open(QIODevice::WriteOnly);
if(tag)
{
txt.write(ui->textEdit->toPlainText().toStdString().data());
this->saved=true;
}
else
QMessageBox::critical(nullptr,"错误","无法打开文件,请检查文件目录",QMessageBox::Yes,QMessageBox::Yes);
txt.close();
}
void makeMap::onEdited()
{
saved=false;
}
void makeMap::onActionNewTriggered(bool checked)
{
if(this->saved)
{
ui->textEdit->clear();
this->saved=true;
}
else{
int tag=QMessageBox::question(nullptr,"注意","是否保存文件",QMessageBox::Yes|QMessageBox::No,QMessageBox::Yes);
if(tag==QMessageBox::Yes)
{
this->onActionSaveTriggered(false);
}
ui->textEdit->clear();
this->saved=true;
}
}
void makeMap::onActionOpenTriggered(bool checked)
{
QString txt;
txt=QFileDialog::getOpenFileName(this,"打开地图","./","*.txt");
if(false==txt.isEmpty())
{
QFile file(txt);
bool ok=file.open(QIODevice::ReadOnly);
if(ok==true)
{
QByteArray arr=file.readAll();
ui->textEdit->setText(QString(arr));
file.close();
}
}
}
void makeMap::onActionSaveasTriggered(bool checked)
{
this->savepath.clear();
this->onActionSaveTriggered(checked);
}
void makeMap::onActionPasteTriggered(bool checked)
{
ui->textEdit->paste();
}
void makeMap::onActionRedoTriggered(bool checkeed)
{
ui->textEdit->redo();
}
void makeMap::onActionUndoTriggered(bool checked)
{
ui->textEdit->undo();
}
makeMap::~makeMap()
{
delete ui;
delete but;
delete but1;
delete mPainter;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/wang-yong416/push-box.git
git@gitee.com:wang-yong416/push-box.git
wang-yong416
push-box
基于Qt开发的推箱子游戏(内含自制地图)
master

搜索帮助