代码拉取完成,页面将自动刷新
#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;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。