近期跟着网上的视频实现简单的Mynotepad,其功能如下:
1、新建文件
2、打开文件
3、文件另存为
4、保存文件
5、编辑菜单栏设计
6、设置字体与颜色
7、设置时间
8、实现编辑的功能
9、添加动作图标
10、工具栏添加图标
11、设置文本编辑区域北背景
12、设置程序图标
13、实现GIF动画播放
14、程序启动画面
//解决Qt中乱码问题
QTextCodec *gbk = QTextCodec::codecForName(“gb18030”);
QTextCodec::setCodecForTr(gbk);
QTextCodec::setCodecForLocale(gbk);
QTextCodec::setCodecForCStrings(gbk);
第一行:定义gb18030编码格式
第二行: 这个函数的作用是设置传给tr函数时的默认字符串编码,GUI设计中最常用的一种。
第三行:这个函数主要用于设置和对本地文件系统读写时候的默认编码格式。比如通过流读取一个文件内容时的编码格式。或者通过qDebug()输出打印信息时的编码。
第四行:这个函数主要是用在字符常量或者QByteArray构造QString对象时使用的一种编码方式。
about.h文件
#ifndef ABOUT_H
#define ABOUT_H
#include <QDialog>
#include <QMovie>
#include <QDebug>
namespace Ui {
class about;
}
class about : public QDialog
{
Q_OBJECT
public:
explicit about(QWidget *parent = 0);
~about();
private:
Ui::about *ui;
QMovie *movie;
private slots:
void startMovieSlot();
void stopMovieSlot();
};
#endif // ABOUT_H
mainwindow.h文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMessageBox>
#include <QtDebug>
#include <QFile>
#include <QFileDialog>
#include <QDir>
#include <QTextStream>
#include <QFont>
#include <QFontDialog>
#include <QColor>
#include <QColorDialog>
#include <QDateTime>
#include <QUrl>
#include <QDesktopServices>//用于访问桌面服务的类
#include "about.h"//包含头文件
#include <QCloseEvent>//当程序需要关闭的时候,所处理的内容
//所有的事件都是受保护的成员函数
//QWidget CloseEvent //QMainWindow CloseEvent
#include <QTextEdit>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected :
void closeEvent(QCloseEvent *event);
private:
Ui::MainWindow *ui;
private slots:
void newFileSlot();
void openFileSlot();
void saveFileSlot();
void saveAsFileSlot();
// void printFileSlot();
void datetimeSlot();//获得当前系统时间,并采用一定格式输出
void setFontSlot();
void setColorSlot();
void aboutWebsiteSlot();//打开一个网站,使用的事默认浏览器
void aboutSoftwareSlot();//弹出子对话框
};
#endif // MAINWINDOW_H
about.cpp文件
#include "about.h"
#include "ui_about.h"
about::about(QWidget *parent) :
QDialog(parent),
ui(new Ui::about)
{
ui->setupUi(this);
this->movie = new QMovie("mv.gif");
qDebug() << "current movie has " << this->movie->frameCount();//测GIF图片的帧数
ui->movieLabel->setMovie(this->movie);
this->movie->start();
QObject::connect(ui->startButton, SIGNAL(clicked()), this, SLOT(startMovieSlot()));
QObject::connect(ui->stopButton, SIGNAL(clicked()), this, SLOT(stopMovieSlot()));
}
about::~about()
{
delete ui;
}
void about::startMovieSlot()
{
this->movie->start();
}
void about::stopMovieSlot()
{
this->movie->stop();
}
mainwindow.cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("Untitled.txt ---------WH");//
this->setWindowIcon(QIcon(":new/icon/txt"));//
//file menu
QObject::connect(ui->newAction, SIGNAL(triggered()), this, SLOT(newFileSlot()));
QObject::connect(ui->openAction, SIGNAL(triggered()), this, SLOT(openFileSlot()));
QObject::connect(ui->saveasAction, SIGNAL(triggered()), this, SLOT(saveAsFileSlot()));
QObject::connect(ui->saveAction, SIGNAL(triggered()), this, SLOT(saveFileSlot()));
QObject::connect(ui->exitAction, SIGNAL(triggered()), this, SLOT(close()));
//edit menu
QObject::connect(ui->undoAction, SIGNAL(triggered()), ui->textEdit, SLOT(undo()));
QObject::connect(ui->redoAction, SIGNAL(triggered()),ui->textEdit, SLOT(redo()));
QObject::connect(ui->copyAction, SIGNAL(triggered()), ui->textEdit, SLOT(copy()));
QObject::connect(ui->cutAction, SIGNAL(triggered()), ui->textEdit, SLOT(cut()));
QObject::connect(ui->pasteAction, SIGNAL(triggered()), ui->textEdit, SLOT(paste()));
QObject::connect(ui->selectallAction, SIGNAL(triggered()), ui->textEdit, SLOT(selectAll()));
QObject::connect(ui->datetimeAction, SIGNAL(triggered()), this, SLOT(datetimeSlot()));
QObject::connect(ui->fontAction, SIGNAL(triggered()), this, SLOT(setFontSlot()));
QObject::connect(ui->colorAction, SIGNAL(triggered()), this, SLOT(setColorSlot()));
QObject::connect(ui->aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
//提问:qApp到底是什么?是不是整个程序的this指针?
//回答:QApplication::instance();应用程序实例化全局指針
//这个槽函数直接调用Qt相关文档,不用自己实现
QObject::connect(ui->aboutWebAction, SIGNAL(triggered()), this, SLOT(aboutWebsiteSlot()));
QObject::connect(ui->aboutSoftWareAction, SIGNAL(triggered()), this, SLOT(aboutSoftwareSlot()));
}
void MainWindow::closeEvent(QCloseEvent *event)
{
//event->ignore();
//event->accept();
if (ui->textEdit->document()->isModified())
{
QMessageBox msgBox;
msgBox.setText("文件已经变更了");//显示的文本
msgBox.setInformativeText("您是否要保存文件?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();//继承自QDialog exec
switch (ret)
{
case QMessageBox::Save:
// Save was clicked
this->saveFileSlot();
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;
}
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::newFileSlot()
{
if (ui->textEdit->document()->isModified())
{
qDebug() << "Current file modified!";
}
else
{
// qDebug() << "Not modified!";
ui->textEdit->clear();
this->setWindowTitle("Untitled.txt ---------WHW");
}
}
void MainWindow::openFileSlot()
{
//get the file name
QString fileName = QFileDialog::getOpenFileName(this, "Open File", QDir::currentPath());//程序运行的当前路径
// qDebug() << "fileName is" << fileName;
if (fileName.isEmpty())
{
QMessageBox::information(this, "Error Message", "Please Select a Text File!");//第二个参数为窗口名称
return;
}
QFile *file = new QFile;
file->setFileName(fileName);//
bool ok = file->open(QIODevice::ReadOnly);
if (ok)
{
QTextStream in(file);//文件与文本流相关联
ui->textEdit->setText(in.readAll());//read all context from the file
file->close();
delete file;
}
else
{
QMessageBox::information(this, "Error Message", "File Open Error" + file->errorString());
// delete file;
return;
}
}
void MainWindow::saveFileSlot()
{
QString saveFileName = QFileDialog::getSaveFileName(this, "Save File", QDir::currentPath());
if (saveFileName.isEmpty())
{
this->saveAsFileSlot();
}
else
{
QFile *file = new QFile;
file->setFileName(saveFileName);
bool ok = file->open(QIODevice::WriteOnly);
if (ok)
{
QTextStream out(file);
out << ui->textEdit->toPlainText();//去除纯文本?
file->close();
// this->setWindowTitle(saveFileName + "-------notepad");
delete file;
}
}
}
void MainWindow::saveAsFileSlot()
{
QString saveFileName = QFileDialog::getSaveFileName(this, "Save File", QDir::currentPath());
if (saveFileName.isEmpty())
{
QMessageBox::information(this, "Error Message", "Please Select A File!");
return;
}
QFile *file = new QFile;
file->setFileName(saveFileName);
bool ok = file->open(QIODevice::WriteOnly);
if (ok)
{
QTextStream out(file);
out << ui->textEdit->toPlainText();//这里是去除textEdit当中的纯文本
file->close();
//this->setWindowTitle(saveFileName + "-----notepad");
delete file;
}
else
{
QMessageBox::information(this, "Error Message", "Save File Error");
return;//不需要这步吧
}
}
/*void MainWindow::printFileSlot()
{
//QPrintDialog static member call
}*/
void MainWindow::setFontSlot()
{
//get user selected font
bool ok;
QFont font = QFontDialog::getFont(&ok,QFont("Times", 12), this);
if (ok)
{
ui->textEdit->setFont(font);
}
else
{
QMessageBox::information(this, "Error Message", "Error Set Font");
return;//不需要这步吧
}
}
void MainWindow::setColorSlot()
{
QColor color = QColorDialog::getColor(Qt::red, this);
if (color.isValid())
{
ui->textEdit->setTextColor(color);
}
else
{
QMessageBox::information(this, "Error Message", "Color Is Unvalid");
return;//????????????
}
}
void MainWindow::datetimeSlot()
{
QDateTime current = QDateTime::currentDateTime();
QString time = current.toString("yyyy-MM-dd hh:mm:ss");
ui->textEdit->append(time);//追加方式显示时间
}
void MainWindow::aboutWebsiteSlot()
{
QDesktopServices::openUrl(QUrl("http://www.baidu.com"));//构造一个QUrl,再打开QUrl
}
void MainWindow::aboutSoftwareSlot()//当用户触发这个槽函数后,about一个非模态对话框,然后在about.cpp中的构造函数中实现
{
about *dialog = new about;
dialog->show();//unmodal dialog 非模态对话框
//dialog.exec() modal dialog 模态对话框
}
mian.cpp文件
#include <QtGui/QApplication>
#include "mainwindow.h"
//#include <QTextCodec>
#include <QPixmap>
#include <QSplashScreen>
#include <QTextCodec>
void sleep(unsigned int msec)//延时函数
{
QTime dieTime = QTime::currentTime().addMSecs(msec);
while( QTime::currentTime() < dieTime )
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextCodec *gbk = QTextCodec::codecForName("gb2312");
QTextCodec::setCodecForLocale(gbk);
QTextCodec::setCodecForTr(gbk);
QTextCodec::setCodecForCStrings(gbk);
QPixmap pixmap("wzj.png");//运行程序,加载图片
QSplashScreen splash(pixmap);//启动窗口
/*
在这里写这个是设置启动窗口大小(最大值与最小值设置相同,则窗口大小不能变化),但是窗口大小是设定好了,却有其他细节问题。。。
splash.setMaximumSize(500, 399);
splash.setMinimumSize(500, 399);
*/
splash.show();
sleep(3000);
// for(long index = 0; index <= 800000000; index++)
//初始化执行程序的过程,我们只能选择循环,(像空转),让我们看下效果
MainWindow w;
/*
在这里写这个是设置主程序窗口大小(最大值与最小值设置相同,则窗口大小不能变化)
w.setMaximumSize(500, 399);
w.setMinimumSize(500, 399);
*/
w.show();
splash.finish(&w);//关闭初始化屏幕
return a.exec();
}