editer.pro
#-------------------------------------------------
#
# Project created by QtCreator 2020-04-30T00:49:52
#
#-------------------------------------------------
QT += core gui printsupport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = editer
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as 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 you use 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
CONFIG += c++11
SOURCES +=
main.cpp
fileEditer.cpp
about.cpp
HEADERS +=
fileEditer.h
about.h
FORMS +=
fileEditer.ui
about.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 +=
imgs.qrc
RC_FILE = app.rc
文件列表
fileEditer.ui
about.ui
fileEditer.h
#ifndef EDITER_H
#define EDITER_H
#include <QMainWindow>
#include <QCloseEvent>
#include "about.h"
namespace Ui {
class fileEditer;
}
class fileEditer : public QMainWindow
{
Q_OBJECT
public:
explicit fileEditer(QWidget *parent = nullptr);
~fileEditer();
private:
virtual void closeEvent(QCloseEvent* event);
private slots:
void oNewFileTriggered();
void onOpenFileTriggered();
void onSaveFileTriggered();
void onSaveFileAsTriggered();
void onPrintFileTriggered();
void onDateTimeTriggered();
void onFontTriggered();
void onColorTriggered();
void onAboutWebTriggered();
void onAboutTriggered();
private:
Ui::fileEditer *ui;
QString m_saveFileName;//当前打开的文件名,也是保存时默认的文件名
};
#endif // EDITER_H
fileEditer.cpp
#include "fileEditer.h"
#include "ui_fileEditer.h"
#include <QString>
#include <QDebug>
#include <QFile>
#include <QDir>
#include <QFileDialog>
#include <QMessageBox>
#include <QPrinter>
#include <QPrintDialog>
#include <QFont>
#include <QFontDialog>
#include <QColor>
#include <QColorDialog>
#include <QDateTime>
#include <QDesktopServices>
#include <QUrl>
#include "about.h"
fileEditer::fileEditer(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::fileEditer)
{
ui->setupUi(this);
//this->setMinimumSize(QSize(300, 300));
this->setWindowTitle(QString("UnTitled.txt"));
connect(ui->newFile, SIGNAL(triggered()), this, SLOT(oNewFileTriggered()));
connect(ui->openFile, SIGNAL(triggered()), this, SLOT(onOpenFileTriggered()));
connect(ui->saveFile, SIGNAL(triggered()), this, SLOT(onSaveFileTriggered()));
connect(ui->saveFileAs, SIGNAL(triggered()), this, SLOT(onSaveFileAsTriggered()));
connect(ui->printFile, SIGNAL(triggered()), this, SLOT(onPrintFileTriggered()));
connect(ui->exitFile, SIGNAL(triggered()), this, SLOT(close()));
connect(ui->cancel, SIGNAL(triggered()), ui->fileContent, SLOT(undo()));
connect(ui->redo, SIGNAL(triggered()), ui->fileContent, SLOT(redo()));
connect(ui->copy, SIGNAL(triggered()), ui->fileContent, SLOT(copy()));
connect(ui->cut, SIGNAL(triggered()), ui->fileContent, SLOT(cut()));
connect(ui->paste, SIGNAL(triggered()), ui->fileContent, SLOT(paste()));
connect(ui->selectAll, SIGNAL(triggered()), ui->fileContent, SLOT(selectAll()));
connect(ui->date, SIGNAL(triggered()), this, SLOT(onDateTimeTriggered()));
connect(ui->font, SIGNAL(triggered()), this, SLOT(onFontTriggered()));
connect(ui->color, SIGNAL(triggered()), this, SLOT(onColorTriggered()));
connect(ui->aboutQt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
connect(ui->aboutWeb, SIGNAL(triggered()), this, SLOT(onAboutWebTriggered()));
connect(ui->about, SIGNAL(triggered()), this, SLOT(onAboutTriggered()));
}
fileEditer::~fileEditer()
{
delete ui;
}
void fileEditer::oNewFileTriggered()
{
if(ui->fileContent->document()->isModified())
{
qDebug() << "currrent file modified";
}
else
{
qDebug() << "not modified";
}
}
void fileEditer::onOpenFileTriggered()
{
QString fileName = QFileDialog::getOpenFileName(this, "Open File", QDir::currentPath());
qDebug() << "fileName " << fileName;
if(fileName.isEmpty())
{
QMessageBox::information(this, "Error Message", "Please select a text file");
return;
}
else
{
m_saveFileName = fileName;
QFile* file = new QFile();
file->setFileName(fileName);
bool ok = file->open(QIODevice::ReadOnly);
if(ok)
{
QTextStream in(file);
ui->fileContent->setText(in.readAll());
}
else
{
QMessageBox::information(this, "Error Message", "File open failed" + file->errorString());
}
file->close();
delete file;
}
}
void fileEditer::onSaveFileTriggered()
{
if(m_saveFileName.isEmpty())
{
this->onSaveFileAsTriggered();
return;
}
QFile* file = new QFile();
file->setFileName(m_saveFileName);
bool ok = file->open(QIODevice::WriteOnly);
if(ok)
{
QTextStream out(file);
out << ui->fileContent->toPlainText();
}
else
{
QMessageBox::information(this, "Error Message", "File save to " + m_saveFileName + "failed");
}
file->close();
delete file;
}
void fileEditer::onSaveFileAsTriggered()
{
m_saveFileName = QFileDialog::getSaveFileName(this, "Save File", QDir().currentPath());
if(m_saveFileName.isEmpty())
{
QMessageBox::information(this, "Error Message", "please input a file name");
return;
}
QFile* file = new QFile();
file->setFileName(m_saveFileName);
bool ok = file->open(QIODevice::WriteOnly);
if(ok)
{
QTextStream out(file);
out << ui->fileContent->toPlainText();
}
else
{
QMessageBox::information(this, "Error Message", "File save to " + m_saveFileName + "failed");
}
file->close();
delete file;
}
void fileEditer::onPrintFileTriggered()
{
QPrinter printer;
QPrintDialog printDialog(&printer, this);
if(printDialog.exec() == QDialog::Accepted)
{
qDebug() << "Printing ... ";
}
}
void fileEditer::onDateTimeTriggered()
{
QDateTime current = QDateTime::currentDateTime();
QString str = current.toString("yyyy-M-d hh:mm:ss");
ui->fileContent->append(str);
}
void fileEditer::onFontTriggered()
{
bool ok;
QFont font = QFontDialog::getFont(&ok, this);
if(ok)
{
ui->fileContent->setFont(font);
}
else
{
QMessageBox::information(this, "Error Message", "Font set Error");
}
}
void fileEditer::onColorTriggered()
{
QColor color = QColorDialog::getColor(Qt::red, this, "Color");
if(color.isValid())
{
ui->fileContent->setTextColor(color);
}
else
{
QMessageBox::information(this, "Error Message", "Color set Error");
}
}
void fileEditer::onAboutWebTriggered()
{
QDesktopServices::openUrl(QUrl("http://www.baidu.com"));
}
void fileEditer::onAboutTriggered()
{
about* ab = new about();
qDebug() << "this = " << ab;
ab->setAttribute(Qt::WA_DeleteOnClose);//非模式对话框关闭事件时,自动销毁该对象
ab->show();
}
void fileEditer::closeEvent(QCloseEvent* event)
{
if(ui->fileContent->document()->isModified())
{
QMessageBox msgBox(this);
msgBox.setMinimumSize(QSize(259, 116));//设置最小对话框,否则可能存在警告
msgBox.setText("The document has been modified.");
msgBox.setInformativeText("Do you want to save your changes?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();
switch (ret)
{
case QMessageBox::Save:
// Save was clicked
this->onSaveFileTriggered();
//这里如果没有正常保存需要额外处理,暂时还是会直接退出
break;
case QMessageBox::Discard:
// Don't Save was clicked
event->accept();
break;
case QMessageBox::Cancel:
// Cancel was clicked
event->ignore();
break;
default:
// should never be reached
break;
}
}
else
{
event->accept();
}
}
about.h
#ifndef ABOUT_H
#define ABOUT_H
#include <QDialog>
#include <QMovie>
namespace Ui {
class about;
}
class about : public QDialog
{
Q_OBJECT
public:
explicit about(QWidget *parent = nullptr);
~about();
private slots:
void onStart();
void onStop();
private:
Ui::about *ui;
QMovie* m_gif;
};
#endif // ABOUT_H
about.cpp
#include "about.h"
#include "ui_about.h"
#include <QString>
#include <QDebug>
about::about(QWidget *parent) :
QDialog(parent),
ui(new Ui::about)
{
ui->setupUi(this);
this->setWindowTitle(QString("about"));
this->setAttribute(Qt::WA_QuitOnClose,false);//非模式对话框关闭时不退出程序
m_gif = new QMovie("about.gif", nullptr, this);
ui->gifLabel->setMovie(m_gif);
m_gif->start();
connect(ui->start, SIGNAL(clicked()), this, SLOT(onStart()));
connect(ui->stop, SIGNAL(clicked()), this, SLOT(onStop()));
}
about::~about()
{
m_gif->stop();
delete ui;
qDebug() << "delete " << this;
}
void about::onStart()
{
m_gif->start();
}
void about::onStop()
{
m_gif->stop();
}
main.cpp
#include "fileEditer.h"
#include <QApplication>
#include <QPixmap>
#include <QSplashScreen>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//加载APP启动界面
QPixmap pixmap("boot.jpg");
QSplashScreen splash(pixmap);
splash.show();
//模拟部分初始化
for(uint32_t k = 0; k < 800000000; k++);
fileEditer w;
w.setAttribute(Qt::WA_QuitOnClose,true);//该窗口退出关闭所有其他窗口,主要是非模式对话框
w.show();
splash.finish(&w);
return a.exec();
}