
QT实例3文本编辑器
第一步,
创建工程添加一个TextEdit 文本编辑框 选中MainWindow然后Ctrl+L垂直布局
输入 文件(&F)
第二步:设置munuBar 中Action的对象名 newAction openAction saveAction saveAsAction separator 分割符rintActiioon exitAction
查找 QAction类中的SIGNAL 信号
选择其中的触发函数 void triggered(bool checked =false);
第三步加上
第三步 查找QFile类的方法 setFileName QTODevice 中的属性
查找QFileDialog 利用静态方法调用Static Public Members getOpenFileName getSaveFileName
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),"/home",tr("Images (*.png*.xpm*.jpg)"));
tr("Images (*.png*.xpm*.jpg)")
后续步骤直接看代码
头文件
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMessageBox> //对话框头文件
#include <QtDebug> //打印部分调试信息
#include <QFile> //文件处理类库
#include <QFileDialog> //文本对话框
#include <QDir> //文本目录
#include <QTextStream> //文本输出流
#include <QPrintDialog> //文件打印对话框
#include <QFontDialog> //字体对话框
#include <QFont> //字体本身的类
#include <QColor> //颜色的类
#include <QColorDialog> //颜色对话框
#include <QDateTime> //时间类
#include <QUrl> //网页链接的类
#include <QDesktopServices> //用于访问桌面服务的类
#include <about.h> //about Dialog对话框的头文件
#include <QCloseEvent> //当程序需要被关闭所需要处理的内容 所有的事件都是受保护的成员
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void closeEvent(QCloseEvent *);
private:
Ui::MainWindow *ui;
QString saveFileName;
private slots:
void newFileSlot();
void openFileSlot(); //打开一个已经存在的文本文件
void saveAsFileSlot();
void saveFileSlot(); //保存
void printFileSlot();
void setFontSlot(); //设置字体
void setColorSlot(); //设置颜色
void setDateTimeSlot(); //获得当前系统时间,并且打印出来
void aboutWebsiteSlot(); //打开主页 通过QDesktop
void aboutSoftwareSlot();
};
#endif // MAINWINDOW_H2
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("文本编辑器");
//文件菜单
QObject::connect(ui->newAction,SIGNAL(triggered()),this,SLOT(newFileSlot()));
QObject::connect(ui->openAction,SIGNAL(triggered()),this,SLOT(openFileSlot()));
QObject::connect(ui->saveAction,SIGNAL(triggered()),this,SLOT(saveFileSlot()));
QObject::connect(ui->saveAsAction,SIGNAL(triggered()),this,SLOT(saveAsFileSlot()));
QObject::connect(ui->printAction,SIGNAL(triggered()),this,SLOT(printFileSlot()));
QObject::connect(ui->exitAction,SIGNAL(triggered()),this,SLOT(close()));
//编辑菜单
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->pasteAction,SIGNAL(triggered()),ui->textEdit,SLOT(paste()));
QObject::connect(ui->cutAction,SIGNAL(triggered()),ui->textEdit,SLOT(cut()));
QObject::connect(ui->selectAction,SIGNAL(triggered()),ui->textEdit,SLOT(selectAll()));
QObject::connect(ui->fontAction,SIGNAL(triggered()),this,SLOT(setFontSlot()));
QObject::connect(ui->colorAction,SIGNAL(triggered()),this,SLOT(setColorSlot()));
QObject::connect(ui->datetimeAction,SIGNAL(triggered()),this,SLOT(setDateTimeSlot()));
//帮助
QObject::connect(ui->aboutWebsiteAction,SIGNAL(triggered()),this,SLOT(aboutWebsiteSlot()));
QObject::connect(ui->aoubtSoftwareAction,SIGNAL(triggered()),this,SLOT(aboutSoftwareSlot()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::closeEvent(QCloseEvent *event)
{
//event->ignore();
//event->accept();
if(ui->textEdit->document()->isModified())
{
QMessageBox msgBox;
//显示的文本
msgBox.setText("文件内容已经被改变了");
//你想要做的操作
msgBox.setInformativeText("是否保存?");
//对话框中的button
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;
}
}
else
{
event->accept();
}
}
void MainWindow::newFileSlot()
{
//判断当前的文本编辑框中的doucument是否被改变
if(ui->textEdit->document()->isModified())
{
//调试 qDebug()<<"current file modified!";
this->saveAsFileSlot();
}
else
{
//调试 qDebug()<<"current flie no modified!";
ui->textEdit->clear();
this->setWindowTitle("无标题-文本编辑器");//设置标题提示新建的文件
}
}
void MainWindow::openFileSlot()
{
//获得fileName
/* QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
"/home",默认打开路径
tr("Images (*.png *.xpm *.jpg)"));
*/
saveFileName=QFileDialog::getOpenFileName(this,"打开文件",QDir::currentPath(),tr("TEXT (*.txt)"));
// qDebug() << fileName;
//判断文件选择是否被取消了
if(saveFileName.isEmpty())
{
QMessageBox::information(this,"错误信息","请选择一个文件!");
return;
}
else
{
//创建一个新的文件对象
QFile * file = new QFile;
//设置新建文件对象的文件名
file->setFileName(saveFileName);
//打开文件file 已QIODevice方式 只读 返回值bool判断是否打开
bool ok=file->open(QIODevice::ReadOnly);
if(ok)
{
//关联文件与文本流
QTextStream in(file);
//in.readAll()将file中的所有的文件全部读进去 然后将所有文件全部setTet到textEdit
ui->textEdit->setText(in.readAll());
this->setWindowTitle(saveFileName+"-文本编辑器");
file->close();
delete file;
}
else
{
//提示打开错误信息
QMessageBox::information(this,"错误信息","打开文件失败!");
return;
}
}
}
void MainWindow::saveFileSlot()
{
if(saveFileName.isEmpty())
{
//qDebug()<<"无文件名";
this->saveAsFileSlot();
}
else
{
//qDebug()<<"有文件名";
QFile *file =new QFile;
file->setFileName(saveFileName);
bool ok=file->open(QIODevice::WriteOnly);
if(ok)
{
//将file和文本流相关联
QTextStream out(file);
//转换成纯文本存入磁盘
out<<ui->textEdit->toPlainText();
file->close();
delete file;
}
}
}
void MainWindow::saveAsFileSlot()
{
QString saveFileName= QFileDialog::getSaveFileName(this,"保存文件",QDir::currentPath(),tr("TEXT (*.txt)"));
if(saveFileName.isEmpty())
{
QMessageBox::information(this,"错误信息","请选择一个存放位置!");
return;
}
QFile *file =new QFile;
file->setFileName(saveFileName);
bool ok=file->open(QIODevice::WriteOnly);
if(ok)
{
//将file和文本流相关联
QTextStream out(file);
//转换成纯文本存入磁盘
out<<ui->textEdit->toPlainText();
file->close();
this->setWindowTitle(saveFileName+"-文本编辑器");
delete file;
}
else
{
QMessageBox::information(this,"错误信息","保存失败!");
return;
}
}
void MainWindow::printFileSlot()
{
//QPrintDialog
}
void MainWindow::setFontSlot()
{
//获得用户选择的字体
/*
bool ok;
QFont font = QFontDialog::getFont(
&ok, QFont("Helvetica [Cronyx]", 10), this);
if (ok) {
// the user clicked OK and font is set to the font the user selected
} else {
// the user canceled the dialog; font is set to the initial
// value, in this case Helvetica [Cronyx], 10
}
*/
bool ok;
QFont font =QFontDialog::getFont(&ok,this);
if(ok)
{
ui->textEdit->setFont(font);
}
else
{
QMessageBox::information(this,"错误信息","设置字体失败!");
return;
}
}
void MainWindow::setColorSlot()
{
/*
void Dialog::setColor()
{
QColor color;
if (native->isChecked())
color = QColorDialog::getColor(Qt::green, this);
else
color = QColorDialog::getColor(Qt::green, this, "Select Color", QColorDialog::DontUseNativeDialog);
if (color.isValid()) {
colorLabel->setText(color.name());
colorLabel->setPalette(QPalette(color));
colorLabel->setAutoFillBackground(true);
}
}
*/
QColor color=QColorDialog::getColor(Qt::red,this);
if(color.isValid())
{
ui->textEdit->setTextColor(color);
}
else
{
QMessageBox::information(this,"错误信息","当前设置的颜色不可用");
return;
}
}
void MainWindow::setDateTimeSlot()
{
//获取当前系统的时间到current对象中
QDateTime current=QDateTime::currentDateTime();
//以一定的时间格式转换成字符
QString time =current.toString("yyyy-M-dd hh::mm::ss");
//在文本框的末尾追加上textEdit
ui->textEdit->append(time);
}
void MainWindow::aboutWebsiteSlot()
{
QDesktopServices::openUrl(QUrl("http://www.rainfly.cn"));
}
void MainWindow::aboutSoftwareSlot()
{
about *dialog=new about;
dialog->show(); //显示一个非模态对话框
//dialog.exec() 显示modal dialog
}
about.h
#ifndef ABOUT_H
#define ABOUT_H
#include <QDialog>
namespace Ui {
class about;
}
class about : public QDialog
{
Q_OBJECT
public:
explicit about(QWidget *parent = 0);
~about();
private slots:
void on_pushButton_clicked();
private:
Ui::about *ui;
};
#endif // ABOUT_H
about.cpp
#include "about.h"
#include "ui_about.h"
about::about(QWidget *parent) :
QDialog(parent),
ui(new Ui::about)
{
ui->setupUi(this);
this->setWindowTitle("关于软件");
}
about::~about()
{
delete ui;
}
void about::on_pushButton_clicked()
{
this->close();
}
about.ui
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//将QString转换成UTF-8 编码 解决中文乱码问题
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
MainWindow w;
w.show();
return a.exec();
}



还有小板凳哦!