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/Using KXmlGuiWindow (zh TW)

< 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 | српски | Українська | 简体中文 | 繁體中文

如何使用 KXmlGuiWindow
教學系列   初學者教學
預備課程   教學1 - Hello World
下一課   教學3 - KActions 和 XMLGUI
深入閱讀   KXmlGuiWindow

Contents

  • 1 摘要
  • 2 KXmlGuiWindow
    • 2.1 mainwindow.h
    • 2.2 mainwindow.cpp
  • 3 回到 main.cpp
    • 3.1 main.cpp
  • 4 CMake
    • 4.1 CMakeLists.txt
    • 4.2 編譯
  • 5 繼續前進

[edit] 摘要

本教學承接上一講第一個程式教學,進一步介紹 KXmlGuiWindow 類別的使用。

在上一講中,程式只是彈出了一個對話框。在本講中,我們要讓我們的程式具備更加實際功能。

[edit] KXmlGuiWindow

KXmlGuiWindow 提供完整的主視窗檢視包含選單列、工具列、狀態列和中央的主區域。大多數KDE應用程式會衍生此類別,因為它提供了一個簡單的方法,透過XML檔案定義選單和工具列的排版(這種技術稱為XMLGUI)。雖然我們不會使用 XMLGUI 在本教學中,但下一課我們會用到它。

為了使用 KXmlGuiWindow,我們必須對它子類別化。因此,我們創建兩個檔案, mainwindow.cpp 和 mainwindow.h 放置我們的程式碼。

[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. public:
  10. MainWindow(QWidget *parent=0);
  11.  
  12. private:
  13. KTextEdit* textArea;
  14. };
  15.  
  16. #endif

首先,我們在第7行宣告 KXmlGuiWindow 的子類別:class MainWindow : public KXmlGuiWindow。

接下來我們宣告一個建構子 MainWindow(QWidget *parent=0); 。

最後我們宣告了一個指標,它指向構成我們程式主體的物件。KTextEdit 是一個通用的文字編輯器,並具有很多 KDE 特有的優點,如游標自動隱藏等。

[edit] mainwindow.cpp

  1. #include "mainwindow.h"
  2.  
  3. MainWindow::MainWindow(QWidget *parent) : KXmlGuiWindow(parent)
  4. {
  5. textArea = new KTextEdit();
  6. setCentralWidget(textArea);
  7. setupGUI();
  8. }

首先,很自然的,我們在第一行含入(include)有類別宣告的標頭檔(header file)。

在第五行,我們初始化了文字編輯器物件。接下來在第6行,我們使用 KXmlGuiWindow 內建的 setCentralWidget() 函數,它告訴 KXmlGuiWindow 什麼東西應該顯示在視窗的中央。

最後,呼叫 KXmlGuiWindow::setupGUI(),它會替我們完成很多底層的工作,並創建一個預設的選單列(包含 Settings 和 Help)。

[edit] 回到 main.cpp

為了能夠實際運行前面定義的視窗,我們還需要在 main.cpp 中增加幾行:

[edit] main.cpp

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

與教學1相比,唯一新增的就是第6、18和19行。在第18行,我們生成了一個 MainWindow 物件實例,然後在第19行顯示它。

[edit] CMake

建構此程式的最好方法是使用 CMake。與教學1相比,改變包含增加 mainwindow.cpp 到原始碼列表中,以及 tutorial1 變成了 tutorial2。

[edit] CMakeLists.txt

  1. project (tutorial2)
  2.  
  3. find_package(KDE4 REQUIRED)
  4. include_directories(${KDE4_INCLUDES})
  5.  
  6. set(tutorial2_SRCS
  7. main.cpp
  8. mainwindow.cpp
  9. )
  10.  
  11. kde4_add_executable(tutorial2 ${tutorial2_SRCS})
  12. target_link_libraries(tutorial2 ${KDE4_KDEUI_LIBS})

[edit] 編譯

使用下面的指令編譯、連結以及運行程式:

mkdir build && cd build
cmake ..
make
./tutorial2

[edit] 繼續前進

現在你可以開始學習下一課:使用 KActions。

Retrieved from "http://techbase.kde.org/Development/Tutorials/Using_KXmlGuiWindow_(zh_TW)"
Categories: Tutorial | C++

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.95
  • 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