Development/Tutorials/Using KXmlGuiWindow (es)

From KDE TechBase
Revision as of 14:45, 19 September 2008 by Edumardo (talk | contribs) (translation to spanish)
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.


Development/Tutorials/Using_KXmlGuiWindow

Como usar usar KXmlGuiWindow
Serie   Tutorial para principiantes
Requisitos previos   Tutorial 1 - Hola Mundo
Siguiente   Tutorial 3 - KActions y XMLGUI
Lectura avanzada   KXmlGuiWindow

Resumen

Este tutorial continua el camino iniciado por el tutorial Hola Mundo, haciendo una introducción a la clase KXmlGuiWindow.

En el tutorial anterior, el programa mostró una "caja de diálogo" (dialog box), ahora vamos a añadir mas funcionalidad al programa.

KXmlGuiWindow

La clase KXmlGuiWindow proporciona una vista de la venta principal con una barra de menú, una barra de herramientas, una barra de estado y un área principial en el centro del widget. La mayoria de las aplicaciones de KDE derivarán de esta clase, que proporciona una manera facil de definir el layout del menú y la barra de herramientas mediante archivos XML (esta tecnología se llama XMLGUI). Si bien no usaremos XMLGUI en este tutorial, lo haremos en el siguiente.

Con el fin de tener un KXmlGuiWindow que pueda usarse, debemos heredar de él. Por lo que creamos dos archivos, mainwindow.cpp y mainwindow.h que contendrán nuestro código.

mainwindow.h

  1. ifndef MAINWINDOW_H
  2. define MAINWINDOW_H
  1. include <KXmlGuiWindow>
  2. include <KTextEdit>

class MainWindow : public KXmlGuiWindow {

 public:
   MainWindow(QWidget *parent=0);
 private:
   KTextEdit* textArea;

};

  1. endif

First we Subclass KXmlGuiWindow on line 7 with class MainWindow : public KXmlGuiWindow.

Then we declare the constructor with MainWindow(QWidget *parent=0);.

And finally we declare a pointer to the object that will make up the bulk of our program. KTextEdit is a generic richtext editor with some KDE niceties like cursor auto-hiding.

mainwindow.cpp

  1. include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : KXmlGuiWindow(parent) {

 textArea = new KTextEdit();
 setCentralWidget(textArea);
 setupGUI();

} First, of course, on line 1 we have to include the header file containing the class declaration.

On line 5, we initialise our text editor with an object. Then on line 6 we use KXmlGuiWindow's built-in setCentralWidget() function which tells the KXmlGuiWindow what should appear in the central section of the window.

Finally, KXmlGuiWindow::setupGUI() is called which does a lot of behind-the-scenes stuff and creates the default menu bars (Settings, Help).

Actualizando main.cpp

Con el fin de ejecutar esta ventana, necesitamos añadir unas pocas lineas a main.cpp:

main.cpp

  1. include <KApplication>
  2. include <KAboutData>
  3. include <KCmdLineArgs>
  1. include "mainwindow.h"

int main (int argc, char *argv[]) {

 KAboutData aboutData( "tutorial2", 0,
     ki18n("Tutorial 2"), "1.0",
     ki18n("A simple text area"),
     KAboutData::License_GPL,
     ki18n("Copyright (c) 2007 Developer") );
 KCmdLineArgs::init( argc, argv, &aboutData );
 
 KApplication app;

 MainWindow* window = new MainWindow();
 window->show();
 return app.exec();

} The only new lines here (compared to Tutorial 1) are 5, 18 and 19. On line 18, we create our MainWindow object and then on line 19, we display it.

CMake

La mejor manera de compilar el programa es usar CMake. Lo único que ha cambiado desde el tutorial 1 es que hemos añadido mainwindow.cpp a la lista de fuentes y hemos cambiado tutorial1 por tutorial2.

CMakeLists.txt

project (tutorial2)

find_package(KDE4 REQUIRED) include_directories(${KDE4_INCLUDES})

set(tutorial2_SRCS

 main.cpp
 mainwindow.cpp

)

kde4_add_executable(tutorial2 ${tutorial2_SRCS}) target_link_libraries(tutorial2 ${KDE4_KDEUI_LIBS})

Compilando

Para compilarlo, enlazarlo y ejecutarlo, usa:

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

Avanzando

Ahora puedes continuar con el Tutorial 3 - Usar KActions.