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

    From KDE TechBase
    No edit summary
     
    (14 intermediate revisions by 2 users not shown)
    Line 1: Line 1:
    {{Template:I18n/Language Navigation Bar|Development/Tutorials/Saving_and_loading}}
    {{Template:I18n/Language Navigation Bar_(zh_TW)|Development/Tutorials/Saving_and_loading}}


    {{TutorialBrowser (zh TW)|
    {{TutorialBrowser (zh TW)|
    Line 9: Line 9:
    pre=[[Development/Tutorials/Using_KActions (zh TW)|教學 3 - KActions]]|
    pre=[[Development/Tutorials/Using_KActions (zh TW)|教學 3 - KActions]]|


    next=[[Development/Tutorials/KCmdLineArgs|教學 5 - 使用 KCmdLineArgs]]|  
    next=[[Development/Tutorials/KCmdLineArgs (zh TW)|教學 5 - 使用 KCmdLineArgs]]|  


    reading=[[Development/Tutorials/KIO Slaves/Using KIO Slaves in your Program|教學: 在你的程式使用 KIO Slaves]]、KIO::{{class|NetAccess}}、{{qt|QFile}}
    reading=[[Development/Tutorials/KIO Slaves/Using KIO Slaves in your Program|教學: 在你的程式使用 KIO Slaves]]、KIO::{{class|NetAccess}}、{{qt|QFile}}
    Line 25: Line 25:


    ===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> 與教學 3中的相比沒什麼變化,除了說明參數從 tutorial 3 變為了 tutorial 4。
    <tt>main.cpp</tt> 與教學3中的相比沒什麼變化,除了說明參數從 tutorial 3 變為了 tutorial 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 //與教學 3相比,新增加的
       Q_OBJECT //與教學3相比,新增加的
        
        
       public:
       public:
    Line 78: Line 78:


    #endif
    #endif
    </code>
    </syntaxhighlight>
    由於我們想要加上載入和儲存檔案的能力,所以我們必須加入用來完成這些工作的函數。由於這些函數將會透過Qt的[http://doc.qt.nokia.com/latest/signalsandslots.html 訊號/槽(signal/slot)] 機制被呼叫,所以我們必須註明這些函數是槽(slots),就像我們在第19行做的那樣。由於我們在標頭檔中使用槽,所以我們同樣必須加入[http://doc.qt.nokia.com/latest/qobject.html#Q_OBJECT <tt>Q_OBJECT</tt>] 巨集。
    由於我們想要加上載入和儲存檔案的能力,所以我們必須加入用來完成這些工作的函式。由於這些函式將會透過 Qt 的 [http://doc.qt.nokia.com/latest/signalsandslots.html 訊號/槽(signal/slot)] 機制被呼叫,所以我們必須註明這些函數是槽(slots),就像我們在第19行做的那樣。由於我們在標頭檔中使用槽,所以我們同樣必須加入[http://doc.qt.nokia.com/latest/qobject.html#Q_OBJECT <tt>Q_OBJECT</tt>] 巨集。


    我們同樣想要跟踪當前打開檔案的名稱,所以我們宣告了一個{{qt|QString}} fileName 。
    我們同樣想要跟踪當前打開檔案的名稱,所以我們宣告了一個 {{qt|QString}} fileName 。


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


    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"?>
    <gui name="tutorial4"
    <gui name="tutorial4"
    Line 220: Line 220:


    </gui>
    </gui>
    </code>
    </syntaxhighlight>
    這和教學 3 的 <tt>tutorial3ui.rc</tt> 相同,除了 <tt>name</tt> 改為 'tutorial4' 以外。我們不需要加入任何 <tt>KStandardAction</tt> 的資訊來安置這些動作,KDE 會自動處理。
    這和教學 3 的 <tt>tutorial3ui.rc</tt> 相同,除了 <tt>name</tt> 改為 'tutorial4' 以外。我們不需要加入任何 <tt>KStandardAction</tt> 的資訊來安置這些動作,KDE 會自動處理。


    Line 229: Line 229:
    首先我們加入
    首先我們加入


    <code cppqt>
    <syntaxhighlight lang="cpp-qt">
    fileName(QString())
    fileName(QString())
    </code>
    </syntaxhighlight>


    在 <tt>MainWindow</tt> 建構子列表的第16行。這可以確保 <tt>fileName</tt>一開始是空的。
    在 <tt>MainWindow</tt> 建構子列表的第16行。這可以確保 <tt>fileName</tt>一開始是空的。
    Line 237: Line 237:
    ===加入動作===
    ===加入動作===


    我們首先要做的是為使用者提供操作介面,使他們能夠告訴應用程式載入和儲存。類似教學 3的 <tt>quit</tt> 動作,我們將使用 <tt>KStandardActions</tt>。在37至47行,我們用和 <tt>quit</tt> 動作同樣的方式加入動作。我們將每一項連接到適當的槽,並宣告在標頭檔中。
    我們首先要做的是為使用者提供操作介面,使他們能夠告訴應用程式載入和儲存。類似教學3的 <tt>quit</tt> 動作,我們將使用 <tt>KStandardActions</tt>。在37至47行,我們用和 <tt>quit</tt> 動作同樣的方式加入動作。我們將每一項連接到適當的槽,並宣告在標頭檔中。


    ===創建新文件===
    ===建立新文件===


    我們第一個創建的函數是 <tt>newFile()</tt> 。
    我們第一個建立的函數是 <tt>newFile()</tt> 。


    <code cppqt>
    <syntaxhighlight lang="cpp-qt">
    void MainWindow::newFile()
    void MainWindow::newFile()
    {
    {
    Line 249: Line 249:
       textArea->clear();
       textArea->clear();
    }
    }
    </code>
    </syntaxhighlight>


    <tt>fileName.clear()</tt> 會設定 <tt>fileName</tt> QString 是空的,以反映該文件還沒有存在於硬碟的事實。<tt>textArea->clear()</tt> 會清除中央的文字區,使用在教學 3我們連接 <tt>clear</tt> <tt>KAction</tt> 的相同功能函式。
    <tt>fileName.clear()</tt> 會設定 <tt>fileName</tt> QString 是空的,以反映該文件還沒有存在於硬碟的事實。<tt>textArea->clear()</tt> 會清除中央的文字區塊,使用在教學3我們連接 <tt>clear</tt> <tt>KAction</tt> 的相同功能函式。


    ===儲存檔案===
    ===儲存檔案===
    Line 260: Line 260:


    該函數的原型是
    該函數的原型是
    <code cppqt>
    <syntaxhighlight lang="cpp-qt">
    void MainWindow::saveFileAs(const QString &outputFileName)
    void MainWindow::saveFileAs(const QString &outputFileName)
    </code>
    </syntaxhighlight>


    然後,我們創建 <tt>KSaveFile</tt> 物件,並使用以下程式碼打開它
    然後,我們建立 <tt>KSaveFile</tt> 物件,並使用以下程式碼打開它
    <code cppqt>
    <syntaxhighlight lang="cpp-qt">
    KSaveFile file(outputFileName);
    KSaveFile file(outputFileName);
    file.open();
    file.open();
    </code>
    </syntaxhighlight>


    現在,我們有檔案寫入,我們需要格式化在文字區塊中的文字,為可以被寫入檔案的格式。因此,我們創建了一個{{qt|QByteArray}}並填寫它,和任何在文字區塊中的純文字版本:
    現在,我們有檔案寫入,我們需要格式化在文字區塊中的文字,為可以被寫入檔案的格式。因此,我們建立了一個 {{qt|QByteArray}} 並填寫它,和任何在文字區塊中的純文字版本:


    <code cppqt>
    <syntaxhighlight lang="cpp-qt">
    QByteArray outputByteArray;
    QByteArray outputByteArray;
    outputByteArray.append(textArea->toPlainText().toUtf8());
    outputByteArray.append(textArea->toPlainText().toUtf8());
    </code>
    </syntaxhighlight>
    現在,我們有了 <tt>QByteArray</tt>,使用它和 <tt>KSaveFile::write()</tt> 寫入檔案。如果我們使用普通的 <tt>QFile</tt>,這將立即進行更改。但是,如果寫入中發生問題,該檔案將會損壞。為此,<tt>KSaveFile</tt>會先寫入文字到一個臨時檔案,然後,當你調用<tt>KSaveFile::finalize()</tt>再改變實際的檔案。
    現在,我們有了 <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>


    最後,我們設定 <tt>MainWindows</tt> 的 <tt>fileName</tt> 成員指向我們剛剛儲存的檔案名。
    最後,我們設定 <tt>MainWindows</tt> 的 <tt>fileName</tt> 成員指向我們剛剛儲存的檔案名。
    <code cppqt>
    <syntaxhighlight lang="cpp-qt">
    fileName = outputFileName;
    fileName = outputFileName;
    </code>
    </syntaxhighlight>


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


    這是一個連接 <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>返回的檔案名。
    這是一個連接 <tt>saveAs</tt> 槽的函式。它只是呼叫一般的 <tt>saveFileAs(QString)</tt> 函式並傳遞 <tt>{{class|KFileDialog}}::[http://api.kde.org/4.x-api/kdelibs-apidocs/kio/html/classKFileDialog.html#a8891356c249c5911e1ab15cc2739a89b getSaveFileName()]</tt> 返回的檔案名。


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


    這是我們第一次實際使用 KIO 函式庫。{{class|KFileDialog}} 提供了一些顯示常見的檔案對話框的靜態函數。呼叫 <tt>KFileDialog::getSaveFileName()</tt> 將顯示一個對話框,使用者可以選擇已儲存的檔案名稱或選擇一個新名稱。該函數返回完整的檔案名稱,我們再傳遞到<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 315: 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>。但是,如果是空的話,這個對話框會讓用戶選擇檔案名。


    ===載入檔案===
    ===載入檔案===
    最後,載入硬碟的檔案的功能。所有程式碼,包含在 <tt>MainWindow::openFile()</tt>。


    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>KFileDialog</tt> 的函式,這次用 <tt>getOpenFileName()</tt>
     
    <syntaxhighlight lang="cpp-qt">
    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>:
    <code cppqt>
    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 {{qt|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>


    ==編譯、安裝與執行==
    ==編譯、安裝與執行==


    ===CMakeLists.txt===
    ===CMakeLists.txt===
    <code ini n>
    <syntaxhighlight lang="make" line>
    project(tutorial4)
    project(tutorial4)
       
       
    Line 384: Line 383:
    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。
    <code>
    <syntaxhighlight lang="text">
    mkdir build && cd build
    mkdir build && cd build
    cmake .. -DCMAKE_INSTALL_PREFIX=$HOME
    cmake .. -DCMAKE_INSTALL_PREFIX=$HOME
    make install
    make install
    $HOME/bin/tutorial4
    $HOME/bin/tutorial4
    </code>
    </syntaxhighlight>


    {{note (zh TW)|Changed settings are saved in your KDE directory, in this case into $HOME/.kde/share/apps/tutorial4.}}
    {{note (zh TW)|修改的設定儲存在您的 KDE 目錄,這個例子是在  $HOME/.kde/share/apps/tutorial4}}


    ==繼續前進==
    ==繼續前進==
    現在你可以開始學習下一課:[[Development/Tutorials/KCmdLineArgs|KCmdLineArgs]] 教學。
    現在你可以開始學習下一課:[[Development/Tutorials/KCmdLineArgs_(zh_TW)|KCmdLineArgs]] 教學。


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

    Latest revision as of 13:26, 23 June 2013

    Template:I18n/Language Navigation Bar (zh TW)

    Template:TutorialBrowser (zh TW)

    摘要

    現在我們擁有了一個基本的文字編輯器界面,開始要讓他做一些有用的事情了。最根本的來說,一個文字編輯器需要能夠從硬碟中載入檔案,儲存你新增或編輯過的檔案。

    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中的相比沒什麼變化,除了說明參數從 tutorial 3 變為了 tutorial 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 的 訊號/槽(signal/slot) 機制被呼叫,所以我們必須註明這些函數是槽(slots),就像我們在第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
    

    Template:Note (zh TW)

    繼續前進

    現在你可以開始學習下一課:KCmdLineArgs 教學。