Archive:Development/Tutorials/Saving and loading (zh CN): Difference between revisions

From KDE TechBase
(New page: {{Template:I18n/Language Navigation Bar|Development/Tutorials/Saving_and_loading}} {{TutorialBrowser| series=Beginner Tutorial| name=Loading and saving files| pre=[[Development/Tutoria...)
 
 
(16 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{Template:I18n/Language Navigation Bar|Development/Tutorials/Saving_and_loading}}


{{TutorialBrowser|


series=Beginner Tutorial|
{{TutorialBrowser (zh_CN)|


name=Loading and saving files|
series=初学者教程|


pre=[[Development/Tutorials/Using_KActions|Tutorial 3 - KActions]]|
name=教程4 - 保存与装载|


next=[[Development/Tutorials/KCmdLineArgs|Tutorial 5 - Using KCmdLineArgs]]|  
pre=[[Development/Tutorials/Using_KActions (zh_CN)|教程3 - KActions]]|


reading=KIO::{{class|NetAccess}} {{qt|QFile}}
next=[[Development/Tutorials/KCmdLineArgs (zh_CN)|教程5 - 使用KCmdLineArgs]]|
 
reading=[[Development/Tutorials/KIO Slaves/Using KIO Slaves in your Program|教学: 在你的程序使用 KIO Slaves]]、KIO::{{class|NetAccess}}{{qt|QFile}}
}}
}}


==Abstract==
==摘要==


Now that we have a basic text editor interface, it's time to make it do something useful. At the most basic, a text editor needs to be able to load files from disc, save files that you've created/edited and create new files.
现在我们拥有了一个基本的文本编辑器的界面,已经到了做一些有用的事情的时候了。从最根本的来说,一个文本编辑器需要能够从磁盘中装载文件,并且能够创建新文件并保存你创建/编辑过的文件。


KDE provides a number of classes for working with files which make life a lot easier for developers. The KIO library allows you to easily access files through network-transparent protocols as well as providing standard file dialogs.
KDE提供了许多开发者易于使用的用于操作文件的类。KIO库允许你十分容易地像使用标准文件对话框一样通过网络访问文件。


[[image:introtokdetutorial4.png|frame|center]]
[[image:introtokdetutorial4.png|frame|center]]


== The Code ==
==代码==


===main.cpp===
===main.cpp===
<code cppqt n>
<syntaxhighlight lang="cpp-qt" line>
#include <KApplication>
#include <KApplication>
#include <KAboutData>
#include <KAboutData>
Line 46: Line 46:
   return app.exec();
   return app.exec();
}
}
</code>
</syntaxhighlight>
<tt>main.cpp</tt> hasn't changed from tutorial 3 except to change any reference to tutorial 3 to tutorial 4.
<tt>main.cpp</tt> 与教程3中的相比没什么变化,除了说明参数从教程3变为了教程4。


===mainwindow.h===
===mainwindow.h===
<code cppqt n>
<syntaxhighlight lang="cpp-qt" line>
#ifndef MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#define MAINWINDOW_H
Line 59: Line 59:
class MainWindow : public KXmlGuiWindow
class MainWindow : public KXmlGuiWindow
{
{
   Q_OBJECT //new from tutorial3
   Q_OBJECT //与教程3相比,新增加的
    
    
   public:
   public:
Line 67: Line 67:
     KTextEdit* textArea;
     KTextEdit* textArea;
     void setupActions();
     void setupActions();
     QString fileName; //new
     QString fileName; //新增加的


   private slots: //new
   private slots: //新增加的
     void newFile(); //new
     void newFile(); //新增加的
     void openFile(); //new
     void openFile(); //新增加的
     void saveFile(); //new
     void saveFile(); //新增加的
     void saveFileAs(); //new
     void saveFileAs(); //新增加的
     void saveFileAs(const QString &outputFileName); //new
     void saveFileAs(const QString &outputFileName); //新增加的
};
};


#endif
#endif
</code>
</syntaxhighlight>
Since we want to add the ability to load and save files, we must add the functions which will do the work. Since the functions will be called through Qt's [http://doc.trolltech.com/latest/signalsandslots.html signal/slot] mechanism we must specify that these functions are slots as we do on line 19. Since we are using slots in this header file, we must also add the [http://doc.trolltech.com/latest/qobject.html#Q_OBJECT <tt>Q_OBJECT</tt>] macro.
由于我们想要添加装载和保存文件的能力,所以我们必须添加将用来完成这些工作的函数。由于这些函数将会通过Qt的[http://doc.trolltech.com/latest/signalsandslots.html 信号/]机制被调用,所以我们必须注明这些函数是槽函数,就像我们在第19行做的那样。由于我们正在头文件中使用槽,所以我们同样必须添加[http://doc.trolltech.com/latest/qobject.html#Q_OBJECT <tt>Q_OBJECT</tt>]宏。


We also want to keep track of the filename of the currently opened file so we declare a <tt>{{qt|QString}} fileName</tt>.
我们同样想要跟踪当前打开的文件的文件名,所以我们申明了一个<tt>{{qt|QString}} fileName</tt>


===mainwindow.cpp===
===mainwindow.cpp===
<code cppqt n>
<syntaxhighlight lang="cpp-qt" line>
#include "mainwindow.h"
#include "mainwindow.h"


Line 92: Line 92:
#include <KActionCollection>
#include <KActionCollection>
#include <KStandardAction>
#include <KStandardAction>
#include <KFileDialog> //new
#include <KFileDialog> //新增加的
#include <KMessageBox> //new
#include <KMessageBox> //新增加的
#include <KIO/NetAccess> //new
#include <KIO/NetAccess> //新增加的
#include <KSaveFile> //new
#include <KSaveFile> //新增加的
#include <QTextStream> //new
#include <QTextStream> //新增加的
   
   
MainWindow::MainWindow(QWidget *parent)
MainWindow::MainWindow(QWidget *parent)
     : KXmlGuiWindow(parent),
     : KXmlGuiWindow(parent),
       fileName(QString()) //new
       fileName(QString()) //新增加的
{
{
   textArea = new KTextEdit;
   textArea = new KTextEdit;
Line 122: Line 122:
   
   
   KStandardAction::open(this, SLOT(openFile()),
   KStandardAction::open(this, SLOT(openFile()),
                         actionCollection()); //new
                         actionCollection()); //新增加的
   
   
   KStandardAction::save(this, SLOT(saveFile()),
   KStandardAction::save(this, SLOT(saveFile()),
                         actionCollection()); //new
                         actionCollection()); //新增加的
   
   
   KStandardAction::saveAs(this, SLOT(saveFileAs()),
   KStandardAction::saveAs(this, SLOT(saveFileAs()),
                         actionCollection()); //new
                         actionCollection()); //新增加的
   
   
   KStandardAction::openNew(this, SLOT(newFile()),
   KStandardAction::openNew(this, SLOT(newFile()),
                         actionCollection()); //new
                         actionCollection()); //新增加的
   
   
   setupGUI();
   setupGUI();
}
}


//New from here on
//从这里开始都是新增加的


void MainWindow::newFile()
void MainWindow::newFile()
Line 196: Line 196:
   }
   }
}
}
</code>
</syntaxhighlight>


===tutorial4ui.rc===
===tutorial4ui.rc===
<code xml n>
<syntaxhighlight lang="xml" line>
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE kpartgui SYSTEM "kpartgui.dtd">
<gui name="tutorial4"
<gui name="tutorial4" version="1">
    version="1"
  <ToolBar name="mainToolBar" >
    xmlns="http://www.kde.org/standards/kxmlgui/1.0"
    <text>Main Toolbar</text>
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <Action name="clear" />
    xsi:schemaLocation="http://www.kde.org/standards/kxmlgui/1.0
  </ToolBar>
                        http://www.kde.org/standards/kxmlgui/1.0/kxmlgui.xsd" >
 
   <MenuBar>
   <MenuBar>
     <Menu name="file" >
     <Menu name="file" >
Line 212: Line 213:
     </Menu>
     </Menu>
   </MenuBar>
   </MenuBar>
  <ToolBar name="mainToolBar" >
    <text>Main Toolbar</text>
    <Action name="clear" />
  </ToolBar>
</gui>
</gui>
</code>
</syntaxhighlight>
This is identical to <tt>tutorial3ui.rc</tt> from tutorial 3 except the <tt>name</tt> has changed to 'tutorial4'. We do not need to add any information about any of the <tt>KStandardAction</tt>s since the placement of those actions is handled automatically by KDE.
这和教学 3 的 <tt>tutorial3ui.rc</tt> 相同,除了 <tt>name</tt> 改为 'tutorial4' 以外。我们不需要加入任何 <tt>KStandardAction</tt> 的信息来安置这些动作,KDE 会自动处理。


==Explanation==
==解释==


Okay, now to implement the code that will do the loading and saving. This will all be happening in <tt>mainwindow.cpp</tt>
Okay,现在实现的程序代码将执行加载和储存。这都实做在  <tt>mainwindow.cpp</tt>


The first thing we do is add
首先我们加入
<code cppqt>
 
<syntaxhighlight lang="cpp-qt">
fileName(QString())
fileName(QString())
</code>
</syntaxhighlight>
to the <tt>MainWindow</tt> constructor list on line 16. This makes sure that <tt>fileName</tt> is empty right from the beginning.


===Adding the actions===
在 <tt>MainWindow</tt> 建构子列表的第16行。这可以确保 <tt>fileName</tt>一开始是空的。


The first thing we are going to do is provide the outward interface for the user so they can tell the application to load and save. Like with the <tt>quit</tt> action in tutorial 3, we will use <tt>KStandardActions</tt>. On lines 37 to 47 we add the actions in the same way as for the <tt>quit</tt> action. For each one, we connect it to the appropriate slot that we declared in the header file.
===加入动作===


===Creating a new document===
我们首先要做的是为用户提供操作接口,使他们能够告诉应用程序加载和储存。类似教学3的 <tt>quit</tt> 动作,我们将使用 <tt>KStandardActions</tt>。在37至47行,我们用和 <tt>quit</tt> 动作同样的方式加入动作。我们将每一项连接到适当的槽,并宣告在头文件中。


The first function we create is the <tt>newFile()</tt> function.
===创建新文件===
<code cppqt>
 
我们第一个创建的函数是 <tt>newFile()</tt>
 
<syntaxhighlight lang="cpp-qt">
void MainWindow::newFile()
void MainWindow::newFile()
{
{
Line 239: Line 249:
   textArea->clear();
   textArea->clear();
}
}
</code>
</syntaxhighlight>
<tt>fileName.clear()</tt> sets the <tt>fileName</tt> QString to be empty to reflect the fact that this document does not yet have a presence on disc. <tt>textArea->clear()</tt> then clears the central text area using the same function that we connected the <tt>clear</tt> <tt>KAction</tt> to in tutorial 3.
 
<tt>fileName.clear()</tt> 会设定 <tt>fileName</tt> QString 是空的,以反映该文件还没有存在于硬盘的事实。<tt>textArea->clear()</tt> 会清除中央的文本块,使用在教学 3我们连接 <tt>clear</tt> <tt>KAction</tt> 的相同功能函式。


===Saving a file===
===储存档案===


====saveFileAs(QString)====
====saveFileAs(QString)====


Now we get onto our first file handling code. We're going to implement a function which will save the contents of the text area to the file name given as a parameter. KDE provides a class for safely saving a file called {{class|KSaveFile}} which is derived from Qt's {{qt|QFile}}.
现在,我们有了第一个档案处理的程序代码。我们将实现一个函数,它将储存文本块内容的文件名给作为参数。KDE 提供了一个安全储存文件名的{{class|KSaveFile}}类别,来自 Qt {{qt|QFile}}


The function's prototype is
该函数的原型是
<code cppqt>
<syntaxhighlight lang="cpp-qt">
void MainWindow::saveFileAs(const QString &outputFileName)
void MainWindow::saveFileAs(const QString &outputFileName)
</code>
</syntaxhighlight>


We then create our <tt>KSaveFile</tt> object and open it with
然后,我们创建 <tt>KSaveFile</tt> 对象,并使用以下程序代码打开它
<code cppqt>
<syntaxhighlight lang="cpp-qt">
KSaveFile file(outputFileName);
KSaveFile file(outputFileName);
file.open();
file.open();
</code>
</syntaxhighlight>
 
现在,我们有档案写入,我们需要格式化在文本块中的文字,为可以被写入档案的格式。因此,我们创建了一个{{qt|QByteArray}}并填写它,和任何在文本块中的纯文本版本:


Now that we have our file to write to, we need to format the text in the text area to a format which can be written to file. For this, we create a {{qt|QByteArray}} and fill it with the plain text version of whatever is in the text area:
<syntaxhighlight lang="cpp-qt">
<code cppqt>
QByteArray outputByteArray;
QByteArray outputByteArray;
outputByteArray.append(textArea->toPlainText().toUtf8());
outputByteArray.append(textArea->toPlainText().toUtf8());
</code>
</syntaxhighlight>
Now that we have our <tt>QByteArray</tt>, we use it to write to the file with <tt>KSaveFile::write()</tt>. If we were using a normal <tt>QFile</tt>, this would make the changes immediately. However, if a problem occurred partway through writing, the file would become corrupted. For this reason, <tt>KSaveFile</tt> works by first writing to a temporary file and then, when you call <tt>KSaveFile::finalize()</tt> the changes are made to the actual file.
现在,我们有了 <tt>QByteArray</tt>,使用它和 <tt>KSaveFile::write()</tt> 写入档案。如果我们使用普通的 <tt>QFile</tt>,这将立即进行更改。但是,如果写入中发生问题,该档案将会损坏。为此,<tt>KSaveFile</tt>会先写入文字到一个临时档案,然后,当你调用<tt>KSaveFile::finalize()</tt>再改变实际的档案。
<code cppqt>
<syntaxhighlight lang="cpp-qt">
file.write(outputByteArray);
file.write(outputByteArray);
file.finalize();
file.finalize();
file.close();
file.close();
</code>
</syntaxhighlight>
Finally, we set <tt>MainWindows</tt>'s <tt>fileName</tt> member to point to the file name we just saved to.
 
<code cppqt>
最后,我们设定 <tt>MainWindows</tt> <tt>fileName</tt> 成员指向我们刚刚储存的文件名。
<syntaxhighlight lang="cpp-qt">
fileName = outputFileName;
fileName = outputFileName;
</code>
</syntaxhighlight>


====saveFileAs()====
====saveFileAs()====


This is the function that the <tt>saveAs</tt> slot is connected to. It simply calls the generic <tt>saveFileAs(QString)</tt> function and passes the file name returned by <tt>{{class|KFileDialog}}::[http://api.kde.org/4.0-api/kdelibs-apidocs/kio/html/classKFileDialog.html#8891356c249c5911e1ab15cc2739a89b getSaveFileName()]</tt>.
这是一个连接 <tt>saveAs</tt> 槽的函数。它只是呼叫一般的 <tt>saveFileAs(QString)</tt> 函数并传递<tt>{{class|KFileDialog}}::[http://api.kde.org/4.0-api/kdelibs-apidocs/kio/html/classKFileDialog.html#8891356c249c5911e1ab15cc2739a89b getSaveFileName()]</tt>返回的文件名。


<code cppqt>
<syntaxhighlight lang="cpp-qt">
void MainWindow::saveFileAs()
void MainWindow::saveFileAs()
{
{
   saveFileAs(KFileDialog::getSaveFileName());
   saveFileAs(KFileDialog::getSaveFileName());
}
}
</code>
</syntaxhighlight>


This is our first actual use of the KIO library. {{class|KFileDialog}} provides a number of static functions for displaying the common file dialog that is used by all KDE applications. Calling <tt>KFileDialog::getSaveFileName()</tt> will display a dialog where the user can select the name of the file to save to or choose a new name. The function returns the full file name, which we then pass to <tt>saveFileAs(QString)</tt>.
这是我们第一次实际使用 KIO 函式库。{{class|KFileDialog}} 提供了一些显示常见的档案对话框的静态函数。呼叫 <tt>KFileDialog::getSaveFileName()</tt> 将显示一个对话框,使用者可以选择已储存的文件名或选择一个新名称。该函数返回完整的文件名,我们再传递到<tt>saveFileAs(QString)</tt>


====saveFile()====
====saveFile()====


<code cppqt>
<syntaxhighlight lang="cpp-qt">
void MainWindow::saveFile()
void MainWindow::saveFile()
{
{
Line 302: Line 315:
   }
   }
}
}
</code>
</syntaxhighlight>


There's nothing exciting or new in this function, just the logic to decide whether or not to show the save dialog. If <tt>fileName</tt> is not empty, then the file is saved to <tt>fileName</tt>. But if it is, then the dialog is shown to allow the user to select a file name.
这个没有什么令人兴奋的新功能,只是决定是否显示储存对话框。如果<tt>fileName</tt>不为空,则该档案储存到<tt>fileName</tt>。但是,如果是空的话,该对话框会让用户选择文件名。


===Loading a file===
===载入档案===


Finally, we get round to being able to load a file from disc. The code for this is all contained in <tt>MainWindow::openFile()</tt>.
最后,加载硬盘的档案的功能。所有程序代码,包含在<tt>MainWindow::openFile()</tt>


First we must ask the user for the name of the file they wish to open. We do this using another one of the <tt>KFileDialog</tt> functions, this time <tt>getOpenFileName()</tt>:
首先,我们必须询问使用者他们希望打开的文件名。我们是用另一个 <tt>KFileDialog</tt> 的函式,这次用 <tt>getOpenFileName()</tt>
<code cppqt>
<syntaxhighlight lang="cpp-qt">
QString fileNameFromDialog = KFileDialog::getOpenFileName();
QString fileNameFromDialog = KFileDialog::getOpenFileName();
</code>
</syntaxhighlight>


Then we use the KIO library to retrieve our file. This allows us to open the file with QFile even if it's stored in a remote location like an FTP site. We make the following call to {{class|NetAccess}}'s <tt>download()</tt> function
然后,我们使用 KIO 函式库来检索档案。这使我们能够用 QFile 打开档案,即使它储存在远程例如 FTP 网站。我们呼叫 {{class|NetAccess}} <tt>download()</tt> 函数
<code cppqt>
<syntaxhighlight lang="cpp-qt">
KIO::NetAccess::download(fileNameFromDialog, tmpFile, this)
KIO::NetAccess::download(fileNameFromDialog, tmpFile, this)
</code>
</syntaxhighlight>
The first argument is the name of the file you wish to download. The second is a QString which, after the download is complete, will contain the location of the temporary copy of the file. It is this <tt>tmpFile</tt> we will work with from now on.
第一个参数是您要下载文件的名称。第二个是仅次于下载完成后的 QString,将包含档案暂时备份的位置。我们将从现在开始工作这个 <tt>tmpFile</tt>


The function returns <tt>true</tt> or <tt>false</tt> depending on whether the transfer was successful. If it failed, we display a message box giving the error:
该函数返回<tt>true</tt><tt>false</tt>取决于转换是否成功。如果失败,我们显示一个讯息框提供错误:
<code cppqt>
<syntaxhighlight lang="cpp-qt">
KMessageBox::error(this, KIO::NetAccess::lastErrorString());
KMessageBox::error(this, KIO::NetAccess::lastErrorString());
</code>
</syntaxhighlight>


Otherwise, we continue with opening the file.
否则,我们继续打开档案。


We create a QFile by passing the temporary file created by <tt>NetAccess::download()</tt> to its constructor and then open it in read-only mode
我们传递<tt>NetAccess::download()</tt>创建的临时档案,给新创建 QFile 的建构子,然后在只读模式打开它
<code cppqt>
<syntaxhighlight lang="cpp-qt">
QFile file(tmpFile);
QFile file(tmpFile);
file.open(QIODevice::ReadOnly);
file.open(QIODevice::ReadOnly);
</code>
</syntaxhighlight>


In order to display the contents of the file, we must use a {{class|QTextStream}}. We create one by passing the contents of our file to its constructor and then call QFile's <tt>readAll()</tt> function to get the text from the file. This is then passed to the <tt>setPlainText()</tt> function of our text area.
为了显示档案的内容,我们必须使用{{qt|QTextStream}}。创建他并将传递的档案的内容给它的建构子,然呼叫 QFile <tt>readAll()</tt> 函数从档案中取得文字。然后传递给文本块的 <tt>setPlainText()</tt> 函数。


<code cppqt>
<syntaxhighlight lang="cpp-qt">
textArea->setPlainText(QTextStream(&file).readAll());
textArea->setPlainText(QTextStream(&file).readAll());
</code>
</syntaxhighlight>


We then store the path of the file we just opened:
然后,存储我们刚才打开档案的路径:
<code cppqt>
<syntaxhighlight lang="cpp-qt">
fileName = fileNameFromDialog;
fileName = fileNameFromDialog;
</code>
</syntaxhighlight>
and finally, we remove the temporary file that was created by <tt>NetAccess::download()</tt>:
最后,删除 <tt>NetAccess::download()</tt> 建立的临时档案:
<code cppqt>
<syntaxhighlight lang="cpp-qt">
KIO::NetAccess::removeTempFile(tmpFile);
KIO::NetAccess::removeTempFile(tmpFile);
</code>
</syntaxhighlight>


==Make, Install And Run==
==编译、安装与执行==


===CMakeLists.txt===
===CMakeLists.txt===
<code ini n>
<syntaxhighlight lang="ini" line>
project(tutorial4)
project(tutorial4)
   
   
Line 371: Line 384:
install(FILES tutorial4ui.rc  
install(FILES tutorial4ui.rc  
         DESTINATION ${DATA_INSTALL_DIR}/tutorial4)
         DESTINATION ${DATA_INSTALL_DIR}/tutorial4)
</code>
</syntaxhighlight>
Since we are now using the KIO library, we must tell CMake to link against it. We do this by passing <tt>${KDE4_KIO_LIBS}</tt> to the <tt>target_link_libraries()</tt> function.
由于我们使用 KIO 函式库,我们必须告诉 CMake 链接。我们这样做,传递 <tt>${KDE4_KIO_LIBS}</tt> <tt>target_link_libraries()</tt> 函数。


With this file, the tutorial can be built and run in the same way as tutorial 3. For more information, see tutorial 3.
有了这个档案,该教学可以用和教学3相同的方式建构和执行。有关更多信息,请参阅教学3。
<syntaxhighlight lang="text">
mkdir build && cd build
cmake .. -DCMAKE_INSTALL_PREFIX=$HOME
make install
$HOME/bin/tutorial4
</syntaxhighlight>


mkdir build && cd build
{{note|修改的设定储存在您的 KDE 目录,这个例子是在 $HOME/.kde/share/apps/tutorial4}}
cmake .. -DCMAKE_INSTALL_PREFIX=$HOME
make install
  $HOME/bin/tutorial4


==Moving On==
==继续前进==
Now you can move on to the [[Development/Tutorials/KCmdLineArgs|KCmdLineArgs]] tutorial.
现在你可以开始学习下一课:[[Development/Tutorials/KCmdLineArgs_(zh_CN)|KCmdLineArgs]] 教学。


[[Category:C++]]
[[Category:C++]]

Latest revision as of 12:42, 23 June 2013


Template:TutorialBrowser (zh CN)

摘要

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

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

代码

main.cpp

#include <KApplication>
#include <KAboutData>
#include <KCmdLineArgs>
 
#include "mainwindow.h"
 
int main (int argc, char *argv[])
{
  KAboutData aboutData( "tutorial4", "tutorial4",
      ki18n("Tutorial 4"), "1.0",
      ki18n("A simple text area which can load and save."),
      KAboutData::License_GPL,
      ki18n("Copyright (c) 2007 Developer") );
  KCmdLineArgs::init( argc, argv, &aboutData );
  KApplication app;
 
  MainWindow* window = new MainWindow();
  window->show();
  return app.exec();
}

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

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <KXmlGuiWindow>
#include <KTextEdit>

class MainWindow : public KXmlGuiWindow
{
  Q_OBJECT //与教程3相比,新增加的
  
  public:
    MainWindow(QWidget *parent=0);
  
  private:
    KTextEdit* textArea;
    void setupActions();
    QString fileName; //新增加的

  private slots: //新增加的
    void newFile(); //新增加的
    void openFile(); //新增加的
    void saveFile(); //新增加的
    void saveFileAs(); //新增加的
    void saveFileAs(const QString &outputFileName); //新增加的
};

#endif

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

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

mainwindow.cpp

#include "mainwindow.h"

#include <KApplication>
#include <KAction>
#include <KLocale>
#include <KActionCollection>
#include <KStandardAction>
#include <KFileDialog> //新增加的
#include <KMessageBox> //新增加的
#include <KIO/NetAccess> //新增加的
#include <KSaveFile> //新增加的
#include <QTextStream> //新增加的
 
MainWindow::MainWindow(QWidget *parent)
    : KXmlGuiWindow(parent),
      fileName(QString()) //新增加的
{
  textArea = new KTextEdit;
  setCentralWidget(textArea);
 
  setupActions();
}
 
void MainWindow::setupActions()
{
  KAction* clearAction = new KAction(this);
  clearAction->setText(i18n("Clear"));
  clearAction->setIcon(KIcon("document-new"));
  clearAction->setShortcut(Qt::CTRL + Qt::Key_W);
  actionCollection()->addAction("clear", clearAction);
  connect(clearAction, SIGNAL(triggered(bool)),
          textArea, SLOT(clear()));
 
  KStandardAction::quit(kapp, SLOT(quit()),
                        actionCollection());
 
  KStandardAction::open(this, SLOT(openFile()),
                        actionCollection()); //新增加的
 
  KStandardAction::save(this, SLOT(saveFile()),
                        actionCollection()); //新增加的
 
  KStandardAction::saveAs(this, SLOT(saveFileAs()),
                        actionCollection()); //新增加的
 
  KStandardAction::openNew(this, SLOT(newFile()),
                        actionCollection()); //新增加的
 
  setupGUI();
}

//从这里开始都是新增加的

void MainWindow::newFile()
{
  fileName.clear();
  textArea->clear();
}

void MainWindow::saveFileAs(const QString &outputFileName)
{
  KSaveFile file(outputFileName);
  file.open();
  
  QByteArray outputByteArray;
  outputByteArray.append(textArea->toPlainText().toUtf8());
  file.write(outputByteArray);
  file.finalize();
  file.close();
  
  fileName = outputFileName;
}

void MainWindow::saveFileAs()
{
  saveFileAs(KFileDialog::getSaveFileName());
}

void MainWindow::saveFile()
{
  if(!fileName.isEmpty())
  {
    saveFileAs(fileName);
  }
  else
  {
    saveFileAs();
  }
}

void MainWindow::openFile()
{
  QString fileNameFromDialog = KFileDialog::getOpenFileName();

  QString tmpFile;
  if(KIO::NetAccess::download(fileNameFromDialog, tmpFile, 
         this))
  {
    QFile file(tmpFile);
    file.open(QIODevice::ReadOnly);
    textArea->setPlainText(QTextStream(&file).readAll());
    fileName = fileNameFromDialog;

    KIO::NetAccess::removeTempFile(tmpFile);
  }
  else
  {
    KMessageBox::error(this, 
        KIO::NetAccess::lastErrorString());
  }
}

tutorial4ui.rc

<?xml version="1.0" encoding="UTF-8"?>
<gui name="tutorial4"
     version="1"
     xmlns="http://www.kde.org/standards/kxmlgui/1.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.kde.org/standards/kxmlgui/1.0
                         http://www.kde.org/standards/kxmlgui/1.0/kxmlgui.xsd" >

  <MenuBar>
    <Menu name="file" >
      <Action name="clear" />
    </Menu>
  </MenuBar>

  <ToolBar name="mainToolBar" >
    <text>Main Toolbar</text>
    <Action name="clear" />
  </ToolBar>

</gui>

这和教学 3 的 tutorial3ui.rc 相同,除了 name 改为 'tutorial4' 以外。我们不需要加入任何 KStandardAction 的信息来安置这些动作,KDE 会自动处理。

解释

Okay,现在实现的程序代码将执行加载和储存。这都实做在 mainwindow.cpp

首先我们加入

fileName(QString())

MainWindow 建构子列表的第16行。这可以确保 fileName一开始是空的。

加入动作

我们首先要做的是为用户提供操作接口,使他们能够告诉应用程序加载和储存。类似教学3的 quit 动作,我们将使用 KStandardActions。在37至47行,我们用和 quit 动作同样的方式加入动作。我们将每一项连接到适当的槽,并宣告在头文件中。

创建新文件

我们第一个创建的函数是 newFile()

void MainWindow::newFile()
{
  fileName.clear();
  textArea->clear();
}

fileName.clear() 会设定 fileName QString 是空的,以反映该文件还没有存在于硬盘的事实。textArea->clear() 会清除中央的文本块,使用在教学 3我们连接 clear KAction 的相同功能函式。

储存档案

saveFileAs(QString)

现在,我们有了第一个档案处理的程序代码。我们将实现一个函数,它将储存文本块内容的文件名给作为参数。KDE 提供了一个安全储存文件名的KSaveFile类别,来自 Qt 的QFile

该函数的原型是

void MainWindow::saveFileAs(const QString &outputFileName)

然后,我们创建 KSaveFile 对象,并使用以下程序代码打开它

KSaveFile file(outputFileName);
file.open();

现在,我们有档案写入,我们需要格式化在文本块中的文字,为可以被写入档案的格式。因此,我们创建了一个QByteArray并填写它,和任何在文本块中的纯文本版本:

QByteArray outputByteArray;
outputByteArray.append(textArea->toPlainText().toUtf8());

现在,我们有了 QByteArray,使用它和 KSaveFile::write() 写入档案。如果我们使用普通的 QFile,这将立即进行更改。但是,如果写入中发生问题,该档案将会损坏。为此,KSaveFile会先写入文字到一个临时档案,然后,当你调用KSaveFile::finalize()再改变实际的档案。

file.write(outputByteArray);
file.finalize();
file.close();

最后,我们设定 MainWindowsfileName 成员指向我们刚刚储存的文件名。

fileName = outputFileName;

saveFileAs()

这是一个连接 saveAs 槽的函数。它只是呼叫一般的 saveFileAs(QString) 函数并传递KFileDialog::getSaveFileName()返回的文件名。

void MainWindow::saveFileAs()
{
  saveFileAs(KFileDialog::getSaveFileName());
}

这是我们第一次实际使用 KIO 函式库。KFileDialog 提供了一些显示常见的档案对话框的静态函数。呼叫 KFileDialog::getSaveFileName() 将显示一个对话框,使用者可以选择已储存的文件名或选择一个新名称。该函数返回完整的文件名,我们再传递到saveFileAs(QString)

saveFile()

void MainWindow::saveFile()
{
  if(!fileName.isEmpty())
  {
    saveFileAs(fileName);
  }
  else
  {
    saveFileAs();
  }
}

这个没有什么令人兴奋的新功能,只是决定是否显示储存对话框。如果fileName不为空,则该档案储存到fileName。但是,如果是空的话,该对话框会让用户选择文件名。

载入档案

最后,加载硬盘的档案的功能。所有程序代码,包含在MainWindow::openFile()

首先,我们必须询问使用者他们希望打开的文件名。我们是用另一个 KFileDialog 的函式,这次用 getOpenFileName()

QString fileNameFromDialog = KFileDialog::getOpenFileName();

然后,我们使用 KIO 函式库来检索档案。这使我们能够用 QFile 打开档案,即使它储存在远程例如 FTP 网站。我们呼叫 NetAccessdownload() 函数

KIO::NetAccess::download(fileNameFromDialog, tmpFile, this)

第一个参数是您要下载文件的名称。第二个是仅次于下载完成后的 QString,将包含档案暂时备份的位置。我们将从现在开始工作这个 tmpFile

该函数返回truefalse取决于转换是否成功。如果失败,我们显示一个讯息框提供错误:

KMessageBox::error(this, KIO::NetAccess::lastErrorString());

否则,我们继续打开档案。

我们传递NetAccess::download()创建的临时档案,给新创建 QFile 的建构子,然后在只读模式打开它

QFile file(tmpFile);
file.open(QIODevice::ReadOnly);

为了显示档案的内容,我们必须使用QTextStream。创建他并将传递的档案的内容给它的建构子,然呼叫 QFile 的 readAll() 函数从档案中取得文字。然后传递给文本块的 setPlainText() 函数。

textArea->setPlainText(QTextStream(&file).readAll());

然后,存储我们刚才打开档案的路径:

fileName = fileNameFromDialog;

最后,删除 NetAccess::download() 建立的临时档案:

KIO::NetAccess::removeTempFile(tmpFile);

编译、安装与执行

CMakeLists.txt

project(tutorial4)
 
find_package(KDE4 REQUIRED)
include_directories(${KDE4_INCLUDES})
 
set(tutorial4_SRCS 
  main.cpp
  mainwindow.cpp
)
 
kde4_add_executable(tutorial4 ${tutorial4_SRCS})
 
target_link_libraries(tutorial4 ${KDE4_KDEUI_LIBS} 
                                ${KDE4_KIO_LIBS})
 
install(TARGETS tutorial4 DESTINATION ${BIN_INSTALL_DIR})
install(FILES tutorial4ui.rc 
        DESTINATION ${DATA_INSTALL_DIR}/tutorial4)

由于我们使用 KIO 函式库,我们必须告诉 CMake 链接。我们这样做,传递 ${KDE4_KIO_LIBS}target_link_libraries() 函数。

有了这个档案,该教学可以用和教学3相同的方式建构和执行。有关更多信息,请参阅教学3。

mkdir build && cd build
cmake .. -DCMAKE_INSTALL_PREFIX=$HOME
make install
$HOME/bin/tutorial4
Note
修改的设定储存在您的 KDE 目录,这个例子是在 $HOME/.kde/share/apps/tutorial4


继续前进

现在你可以开始学习下一课:KCmdLineArgs 教学。