鱼C论坛

 找回密码
 立即注册
查看: 3145|回复: 1

[技术原创] 记事本

[复制链接]
发表于 2014-11-7 16:18:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 ~风介~ 于 2015-10-30 17:42 编辑

代码:
  1. 类名:Notepad
  2. 基类:QWidget
复制代码
==================
  1. //notepad.h
  2. #ifndef NOTEPAD_H
  3. #define NOTEPAD_H

  4. #include <QWidget>
  5. #include <QPushButton>
  6. #include <QTextEdit>
  7. #include <QGridLayout>
  8. #include <QTextStream>
  9. #include <QFile>
  10. #include <QFileDialog>
  11. #include <QMessageBox>

  12. QT_BEGIN_NAMESPACE
  13. class QPushButton;
  14. class QTextEdit;
  15. QT_END_NAMESPACE

  16. class Notepad : public QWidget
  17. {
  18.     Q_OBJECT

  19. public:
  20.     Notepad(QWidget *parent = 0);

  21. protected:
  22.     QPushButton *openButton;
  23.     QPushButton *saveButton;
  24.     QPushButton *exitButton;
  25.     QTextEdit *textEdit;
  26.     QTextEdit *tmpEdit;

  27. private slots:
  28.     void openFile();
  29.     void saveFile();
  30.     void enableSave();


  31. };

  32. #endif // NOTEPAD_H
复制代码
==================
  1. //notepad.cpp
  2. #include "notepad.h"

  3. Notepad::Notepad(QWidget *parent)
  4.     : QWidget(parent)
  5. {

  6.     //设置按钮、文本框
  7.     openButton = new QPushButton(tr("Open"));
  8.     saveButton = new QPushButton(tr("Save"));
  9.     saveButton->setEnabled(false);
  10.     exitButton = new QPushButton(tr("Exit"));
  11.     textEdit = new QTextEdit;
  12.     //用来保存地址信息
  13.     tmpEdit = new QTextEdit;

  14.     //实例化网格布局并加入组件
  15.     QGridLayout *mainLayout = new QGridLayout;

  16.     mainLayout->addWidget(openButton,1,0);
  17.     mainLayout->addWidget(saveButton,1,1);
  18.     mainLayout->addWidget(exitButton,1,2);
  19.     mainLayout->addWidget(openButton,1,0);
  20.     mainLayout->addWidget(textEdit,2,0,2,3);
  21.     setLayout(mainLayout);
  22.     //设置信号和槽
  23.     connect(openButton,SIGNAL(clicked()),this,SLOT(openFile()));
  24.     connect(saveButton,SIGNAL(clicked()),this,SLOT(saveFile()));
  25.     connect(exitButton,SIGNAL(clicked()),this,SLOT(close()));
  26.     connect(textEdit,SIGNAL(textChanged()),this,SLOT(enableSave()));
  27.     //设置标题、大小
  28.     setWindowTitle("Notepad");
  29.     resize(600,600);
  30. }

  31. void Notepad::openFile()
  32. {
  33.     int reply = 0;
  34.     //如果文件已经被修改,弹出对话框
  35.     if(saveButton->isEnabled())
  36.     {
  37.         reply = QMessageBox::question(this,tr("Question"),
  38.                                       tr("What to do about unsaved changes?")
  39.                                       ,QMessageBox::Save | QMessageBox::Discard
  40.                                       | QMessageBox::Cancel,QMessageBox::Save);
  41.         switch(reply)
  42.         {
  43.             case QMessageBox::Save:
  44.                 saveFile();
  45.                 saveButton->setEnabled(false);
  46.                 break;

  47.             case QMessageBox::Discard:
  48.                 saveButton->setEnabled(false);
  49.                 break;

  50.             default:
  51.                 break;
  52.         }
  53.     }
  54.     //
  55.     if(QMessageBox::Cancel != reply)
  56.     {
  57.         //获取文件地址
  58.         QString pathName = QFileDialog::getOpenFileName(this,tr("Open File"),".",tr("*.txt *.py"));
  59.         //如果文件非空
  60.         if(!pathName.isEmpty())
  61.         {
  62.             //把文件地址保存到tmpEdit
  63.             tmpEdit->setText(pathName.toUtf8());
  64.             QFile file(pathName);
  65.             if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
  66.             {
  67.                 QMessageBox::warning(this,tr("Read File"),tr("Cannot open file:\n%1").arg(pathName));
  68.                 return;
  69.             }

  70.             QTextStream out(&file);
  71.             //指定编码方式为utf-8
  72.             out.setCodec("utf-8");
  73.             QString text;
  74.             //当文件为结束时,读入textEdit中,并禁用save按钮
  75.             while(!out.atEnd())
  76.             {
  77.                 text = out.readAll();
  78.                 textEdit->setPlainText(text);
  79.                 saveButton->setEnabled(false);
  80.             }
  81.             file.close();\
  82.         }
  83.         else
  84.         {
  85.             QMessageBox::warning(this,tr("Path"),tr("You did not select any file."));
  86.         }


  87.     }

  88. }

  89. void Notepad::saveFile()
  90. {
  91.     //打开文件并写入,并禁用save按钮
  92.     QFile file(tmpEdit->toPlainText());
  93.     file.open(QIODevice::ReadWrite | QIODevice::Text);
  94.     QByteArray str = textEdit->toPlainText().toUtf8();
  95.     file.write(str);
  96.     file.close();
  97.     saveButton->setEnabled(false);
  98. }

  99. void Notepad::enableSave()
  100. {
  101.    saveButton->setEnabled(true);
  102. }
复制代码
==================
  1. //main.cpp
  2. #include "notepad.h"
  3. #include <QApplication>

  4. int main(int argc, char *argv[])
  5. {
  6.     QApplication a(argc, argv);
  7.     Notepad w;
  8.     w.show();

  9.     return a.exec();
  10. }
复制代码


截图:


1.png

2.png

3.png


评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
拈花小仙 + 5 + 5 感谢楼主无私奉献!

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-4-25 11:58:41 | 显示全部楼层
要了要了!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 19:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表