马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 ~风介~ 于 2015-10-30 17:42 编辑
代码:==================//notepad.h
#ifndef NOTEPAD_H
#define NOTEPAD_H
#include <QWidget>
#include <QPushButton>
#include <QTextEdit>
#include <QGridLayout>
#include <QTextStream>
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
class QPushButton;
class QTextEdit;
QT_END_NAMESPACE
class Notepad : public QWidget
{
Q_OBJECT
public:
Notepad(QWidget *parent = 0);
protected:
QPushButton *openButton;
QPushButton *saveButton;
QPushButton *exitButton;
QTextEdit *textEdit;
QTextEdit *tmpEdit;
private slots:
void openFile();
void saveFile();
void enableSave();
};
#endif // NOTEPAD_H
==================//notepad.cpp
#include "notepad.h"
Notepad::Notepad(QWidget *parent)
: QWidget(parent)
{
//设置按钮、文本框
openButton = new QPushButton(tr("Open"));
saveButton = new QPushButton(tr("Save"));
saveButton->setEnabled(false);
exitButton = new QPushButton(tr("Exit"));
textEdit = new QTextEdit;
//用来保存地址信息
tmpEdit = new QTextEdit;
//实例化网格布局并加入组件
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(openButton,1,0);
mainLayout->addWidget(saveButton,1,1);
mainLayout->addWidget(exitButton,1,2);
mainLayout->addWidget(openButton,1,0);
mainLayout->addWidget(textEdit,2,0,2,3);
setLayout(mainLayout);
//设置信号和槽
connect(openButton,SIGNAL(clicked()),this,SLOT(openFile()));
connect(saveButton,SIGNAL(clicked()),this,SLOT(saveFile()));
connect(exitButton,SIGNAL(clicked()),this,SLOT(close()));
connect(textEdit,SIGNAL(textChanged()),this,SLOT(enableSave()));
//设置标题、大小
setWindowTitle("Notepad");
resize(600,600);
}
void Notepad::openFile()
{
int reply = 0;
//如果文件已经被修改,弹出对话框
if(saveButton->isEnabled())
{
reply = QMessageBox::question(this,tr("Question"),
tr("What to do about unsaved changes?")
,QMessageBox::Save | QMessageBox::Discard
| QMessageBox::Cancel,QMessageBox::Save);
switch(reply)
{
case QMessageBox::Save:
saveFile();
saveButton->setEnabled(false);
break;
case QMessageBox::Discard:
saveButton->setEnabled(false);
break;
default:
break;
}
}
//
if(QMessageBox::Cancel != reply)
{
//获取文件地址
QString pathName = QFileDialog::getOpenFileName(this,tr("Open File"),".",tr("*.txt *.py"));
//如果文件非空
if(!pathName.isEmpty())
{
//把文件地址保存到tmpEdit
tmpEdit->setText(pathName.toUtf8());
QFile file(pathName);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox::warning(this,tr("Read File"),tr("Cannot open file:\n%1").arg(pathName));
return;
}
QTextStream out(&file);
//指定编码方式为utf-8
out.setCodec("utf-8");
QString text;
//当文件为结束时,读入textEdit中,并禁用save按钮
while(!out.atEnd())
{
text = out.readAll();
textEdit->setPlainText(text);
saveButton->setEnabled(false);
}
file.close();\
}
else
{
QMessageBox::warning(this,tr("Path"),tr("You did not select any file."));
}
}
}
void Notepad::saveFile()
{
//打开文件并写入,并禁用save按钮
QFile file(tmpEdit->toPlainText());
file.open(QIODevice::ReadWrite | QIODevice::Text);
QByteArray str = textEdit->toPlainText().toUtf8();
file.write(str);
file.close();
saveButton->setEnabled(false);
}
void Notepad::enableSave()
{
saveButton->setEnabled(true);
}
==================//main.cpp
#include "notepad.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Notepad w;
w.show();
return a.exec();
}
截图:
|