QT学习笔记
用QT实现简单的notepad的功能(上)
运行界面如图所示,其中控件用到了TextEdit控件,将其进行垂直布局。本篇主要实现new(新建一个文件)、Open(打开一个文件)、Save(保存一个文件)这三个功能。项目截图如下图所示。
一、首先在头文件中添加三个槽
private slots:
void newFileSlot(); //新建一个文件
private slots:
void openFileSlot(); //打开一个已经存在的文本文件
private slots:
void saveFileSlot(); //保存文件到磁盘
void newFileSlot(); //新建一个文件
private slots:
void openFileSlot(); //打开一个已经存在的文本文件
private slots:
void saveFileSlot(); //保存文件到磁盘
并将如下图所示添加到文件包含中
二、在mainwindow.cpp文件中编写新建、打开、保存的代码如下:
void MainWindow::newFileSlot()
{
//如果当前文档的内容已经改变
if(ui->textEdit->document()->isModified())
{
qDebug()<<"current file modified";
}
else
{
//qDebug()<<"not modified";
ui->textEdit->clear();
this->setWindowTitle("Untitled.text------shankezhineng");
}
}
//打开文件
void MainWindow::openFileSlot()
{
//get the file name
QString fileName=QFileDialog::getOpenFileName(this,"Open File",QDir::currentPath());
// qDebug()<<"file name is:"<<fileName;
if(fileName.isEmpty())
{
QMessageBox::information(this,"Error message","Please Select a Text File");
return;
}
QFile *file=new QFile();
file->setFileName(fileName);//set file name
bool ok=file->open(QIODevice::ReadOnly);
//open file as read only mode
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());
return;
}
}
//保存文件到磁盘
void MainWindow::saveFileSlot()
{
QString fileName=QFileDialog::getSaveFileName(this,"save file",QDir::currentPath());
if(fileName.isEmpty())
{
QMessageBox::information(this,"Error Message","Please select a file");
return;
}
QFile *file=new QFile;
file->setFileName(fileName);
bool ok=file->open(QIODevice::WriteOnly);
if(ok)
{
QTextStream out(file);
out<<ui->textEdit->toPlainText();//这里是去除textEdit当中的纯文本
file->close();
delete file;
}
else
{
QMessageBox::information(this,"Error Message","Save file Error");
return;
}
}
{
//如果当前文档的内容已经改变
if(ui->textEdit->document()->isModified())
{
qDebug()<<"current file modified";
}
else
{
//qDebug()<<"not modified";
ui->textEdit->clear();
this->setWindowTitle("Untitled.text------shankezhineng");
}
}
//打开文件
void MainWindow::openFileSlot()
{
//get the file name
QString fileName=QFileDialog::getOpenFileName(this,"Open File",QDir::currentPath());
// qDebug()<<"file name is:"<<fileName;
if(fileName.isEmpty())
{
QMessageBox::information(this,"Error message","Please Select a Text File");
return;
}
QFile *file=new QFile();
file->setFileName(fileName);//set file name
bool ok=file->open(QIODevice::ReadOnly);
//open file as read only mode
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());
return;
}
}
//保存文件到磁盘
void MainWindow::saveFileSlot()
{
QString fileName=QFileDialog::getSaveFileName(this,"save file",QDir::currentPath());
if(fileName.isEmpty())
{
QMessageBox::information(this,"Error Message","Please select a file");
return;
}
QFile *file=new QFile;
file->setFileName(fileName);
bool ok=file->open(QIODevice::WriteOnly);
if(ok)
{
QTextStream out(file);
out<<ui->textEdit->toPlainText();//这里是去除textEdit当中的纯文本
file->close();
delete file;
}
else
{
QMessageBox::information(this,"Error Message","Save file Error");
return;
}
}
建立信号与槽之间的连接。
this->setWindowTitle("Untitled ---notepad ");
QObject::connect(ui->actionNew_N,SIGNAL(triggered()),this,SLOT(newFileSlot()));
QObject::connect(ui->actionOpen_O,SIGNAL(triggered()),this,SLOT(openFileSlot()));
QObject::connect(ui->actionNew_N,SIGNAL(triggered()),this,SLOT(newFileSlot()));
QObject::connect(ui->actionOpen_O,SIGNAL(triggered()),this,SLOT(openFileSlot()));
保存运行就可以了。其中有一些点还没有明白,等我整明白了回来补充。