KDE TechBase
  • Page
  • Discussion
  • Edit
  • History
KDE TechBase is a Wiki - You can help! Please contribute! Questions?
Please ask development related questions in the KDE Community Forum.

Development/Tutorials/Saving and loading (zh CN)

< Development | Tutorials

Languages: عربي | Asturianu | Català | Česky | Kaszëbsczi | Dansk | Deutsch | English | Esperanto | Español | فارسی | Suomi | Français | Galego | Italiano | 日本語 | 한국어 | Norwegian | Polski | Português Brasileiro | Română | Русский | Svenska | Slovenščina | српски | Українська | 简体中文 | 繁體中文

教程4 - 保存与装载
教程系列   初学者教程
预备课程   教程3 - KActions
下一课   教程5 - 使用KCmdLineArgs
深入阅读   KIO::NetAccess QFile

Contents

  • 1 摘要
  • 2 代码
    • 2.1 main.cpp
    • 2.2 mainwindow.h
    • 2.3 mainwindow.cpp

[edit] 摘要

现在我们拥有了一个基本的文本编辑器的界面,已经到了做一些有用的事情的时候了。从最根本的来说,一个文本编辑器需要能够从磁盘中装载文件,并且能够创建新文件并保存你创建/编辑过的文件。

KDE提供了许多开发者易于使用的用于操作文件的类。KIO库允许你十分容易地像使用标准文件对话框一样通过网络访问文件。

[edit] 代码

[edit] main.cpp

  1. #include <KApplication>
  2. #include <KAboutData>
  3. #include <KCmdLineArgs>
  4.  
  5. #include "mainwindow.h"
  6.  
  7. int main (int argc, char *argv[])
  8. {
  9. KAboutData aboutData( "tutorial4", "tutorial4",
  10. ki18n("Tutorial 4"), "1.0",
  11. ki18n("A simple text area which can load and save."),
  12. KAboutData::License_GPL,
  13. ki18n("Copyright (c) 2007 Developer") );
  14. KCmdLineArgs::init( argc, argv, &aboutData );
  15. KApplication app;
  16.  
  17. MainWindow* window = new MainWindow();
  18. window->show();
  19. return app.exec();
  20. }

main.cpp 与教程3中的相比没什么变化,除了说明参数从教程3变为了教程4。

[edit] mainwindow.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <KXmlGuiWindow>
  5. #include <KTextEdit>
  6.  
  7. class MainWindow : public KXmlGuiWindow
  8. {
  9. Q_OBJECT //与教程3相比,新增加的
  10.  
  11. public:
  12. MainWindow(QWidget *parent=0);
  13.  
  14. private:
  15. KTextEdit* textArea;
  16. void setupActions();
  17. QString fileName; //新增加的
  18.  
  19. private slots: //新增加的
  20. void newFile(); //新增加的
  21. void openFile(); //新增加的
  22. void saveFile(); //新增加的
  23. void saveFileAs(); //新增加的
  24. void saveFileAs(const QString &outputFileName); //新增加的
  25. };
  26.  
  27. #endif

由于我们想要添加装载和保存文件的能力,所以我们必须添加将用来完成这些工作的函数。由于这些函数将会通过Qt的信号/槽机制被调用,所以我们必须注明这些函数是槽函数,就像我们在第19行做的那样。由于我们正在头文件中使用槽,所以我们同样必须添加Q_OBJECT宏。

我们同样想要跟踪当前打开的文件的文件名,所以我们申明了一个QString fileName。

[edit] mainwindow.cpp

  1. #include "mainwindow.h"
  2.  
  3. #include <KApplication>
  4. #include <KAction>
  5. #include <KLocale>
  6. #include <KActionCollection>
  7. #include <KStandardAction>
  8. #include <KFileDialog> //新增加的
  9. #include <KMessageBox> //新增加的
  10. #include <KIO/NetAccess> //新增加的
  11. #include <KSaveFile> //新增加的
  12. #include <QTextStream> //新增加的
  13.  
  14. MainWindow::MainWindow(QWidget *parent)
  15. : KXmlGuiWindow(parent),
  16. fileName(QString()) //新增加的
  17. {
  18. textArea = new KTextEdit;
  19. setCentralWidget(textArea);
  20.  
  21. setupActions();
  22. }
  23.  
  24. void MainWindow::setupActions()
  25. {
  26. KAction* clearAction = new KAction(this);
  27. clearAction->setText(i18n("Clear"));
  28. clearAction->setIcon(KIcon("document-new"));
  29. clearAction->setShortcut(Qt::CTRL + Qt::Key_W);
  30. actionCollection()->addAction("clear", clearAction);
  31. connect(clearAction, SIGNAL(triggered(bool)),
  32. textArea, SLOT(clear()));
  33.  
  34. KStandardAction::quit(kapp, SLOT(quit()),
  35. actionCollection());
  36.  
  37. KStandardAction::open(this, SLOT(openFile()),
  38. actionCollection()); //新增加的
  39.  
  40. KStandardAction::save(this, SLOT(saveFile()),
  41. actionCollection()); //新增加的
  42.  
  43. KStandardAction::saveAs(this, SLOT(saveFileAs()),
  44. actionCollection()); //新增加的
  45.  
  46. KStandardAction::openNew(this, SLOT(newFile()),
  47. actionCollection()); //新增加的
  48.  
  49. setupGUI();
  50. }
  51.  
  52. //从这里开始都是新增加的
  53.  
  54. void MainWindow::newFile()
  55. {
  56. fileName.clear();
  57. textArea->clear();
  58. }
  59.  
  60. void MainWindow::saveFileAs(const QString &outputFileName)
  61. {
  62. KSaveFile file(outputFileName);
  63. file.open();
  64.  
  65. QByteArray outputByteArray;
  66. outputByteArray.append(textArea->toPlainText().toUtf8());
  67. file.write(outputByteArray);
  68. file.finalize();
  69. file.close();
  70.  
  71. fileName = outputFileName;
  72. }
  73.  
  74. void MainWindow::saveFileAs()
  75. {
  76. saveFileAs(KFileDialog::getSaveFileName());
  77. }
  78.  
  79. void MainWindow::saveFile()
  80. {
  81. if(!fileName.isEmpty())
  82. {
  83. saveFileAs(fileName);
  84. }
  85. else
  86. {
  87. saveFileAs();
  88. }
  89. }
  90.  
  91. void MainWindow::openFile()
  92. {
  93. QString fileNameFromDialog = KFileDialog::getOpenFileName();
  94.  
  95. QString tmpFile;
  96. if(KIO::NetAccess::download(fileNameFromDialog, tmpFile,
  97. this))
  98. {
  99. QFile file(tmpFile);
  100. file.open(QIODevice::ReadOnly);
  101. textArea->setPlainText(QTextStream(&file).readAll());
  102. fileName = fileNameFromDialog;
  103.  
  104. KIO::NetAccess::removeTempFile(tmpFile);
  105. }
  106. else
  107. {
  108. KMessageBox::error(this,
  109. KIO::NetAccess::lastErrorString());
  110. }
  111. }
Retrieved from "http://techbase.kde.org/Development/Tutorials/Saving_and_loading_(zh_CN)"
Category: Tutorial (zh CN)

Navigation

  • Home
  • Help
  • Recent changes

Sections

  • Getting started
  • Development
  • Schedules
  • Policies
  • Contribute
  • Projects

Toolbox

  • What links here
  • Related changes
  • Special pages
  • Printable version
  • Permanent link

Personal tools

  • 38.107.191.98
  • Talk for this IP
  • Log in / create account
  • Login with OpenID
Creative Commons License SA 3.0 as well as the GNU Free Documentation License 1.2
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal