Difference between revisions of "Development/Tutorials/Using KXmlGuiWindow (pt BR)"
(7 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | + | ||
{{TutorialBrowser_(pt_BR)| | {{TutorialBrowser_(pt_BR)| | ||
Line 8: | Line 8: | ||
pre=[[Development/Tutorials/First_program_(pt_BR)|Tutorial 1 - Alô Mundo]]| | pre=[[Development/Tutorials/First_program_(pt_BR)|Tutorial 1 - Alô Mundo]]| | ||
− | next=[[Development/Tutorials/ | + | next=[[Development/Tutorials/Using_KActions|Tutorial 3 - KActions e XMLGUI]]| |
reading={{class|KXmlGuiWindow}} | reading={{class|KXmlGuiWindow}} | ||
Line 27: | Line 27: | ||
===mainwindow.h=== | ===mainwindow.h=== | ||
− | < | + | <syntaxhighlight lang="cpp-qt" line> |
#ifndef MAINWINDOW_H | #ifndef MAINWINDOW_H | ||
#define MAINWINDOW_H | #define MAINWINDOW_H | ||
Line 44: | Line 44: | ||
#endif | #endif | ||
− | </ | + | </syntaxhighlight> |
First we Subclass KXmlGuiWindow on line 7 with <tt>class MainWindow : public KXmlGuiWindow</tt>. | First we Subclass KXmlGuiWindow on line 7 with <tt>class MainWindow : public KXmlGuiWindow</tt>. | ||
Line 52: | Line 52: | ||
===mainwindow.cpp=== | ===mainwindow.cpp=== | ||
− | < | + | <syntaxhighlight lang="cpp-qt" line> |
#include "mainwindow.h" | #include "mainwindow.h" | ||
Line 61: | Line 61: | ||
setupGUI(); | setupGUI(); | ||
} | } | ||
− | </ | + | </syntaxhighlight> |
Primeiro, é claro, na linha 1 nós temos que incluir o arquivo de cabeçalho contendo a declaração da classe. | Primeiro, é claro, na linha 1 nós temos que incluir o arquivo de cabeçalho contendo a declaração da classe. | ||
Line 69: | Line 69: | ||
==De volta ao main.cpp== | ==De volta ao main.cpp== | ||
− | + | No intuito de executar atualmente esta janela, nós adicionamos umas poucas linhas no main.cpp | |
===main.cpp=== | ===main.cpp=== | ||
− | < | + | <syntaxhighlight lang="cpp-qt" line> |
#include <KApplication> | #include <KApplication> | ||
#include <KAboutData> | #include <KAboutData> | ||
Line 94: | Line 94: | ||
return app.exec(); | return app.exec(); | ||
} | } | ||
− | </ | + | </syntaxhighlight> |
As únicas linhas novas aqui (comparadas com o Tutorial 1) são 5, 18 e 19. Na linha 18, nós criamos o nosso objeto MainWindow e então, na linha 19, nós a exibimos. | As únicas linhas novas aqui (comparadas com o Tutorial 1) são 5, 18 e 19. Na linha 18, nós criamos o nosso objeto MainWindow e então, na linha 19, nós a exibimos. | ||
Line 100: | Line 100: | ||
The best way to build the program is to use CMake. All that's changed since tutorial 1 is that <tt>mainwindow.cpp</tt> has been added to the sources list and any <tt>tutorial1</tt> has become <tt>tutorial2</tt>. | The best way to build the program is to use CMake. All that's changed since tutorial 1 is that <tt>mainwindow.cpp</tt> has been added to the sources list and any <tt>tutorial1</tt> has become <tt>tutorial2</tt>. | ||
===CMakeLists.txt=== | ===CMakeLists.txt=== | ||
− | < | + | <syntaxhighlight lang="ini" line> |
project (tutorial2) | project (tutorial2) | ||
Line 113: | Line 113: | ||
kde4_add_executable(tutorial2 ${tutorial2_SRCS}) | kde4_add_executable(tutorial2 ${tutorial2_SRCS}) | ||
target_link_libraries(tutorial2 ${KDE4_KDEUI_LIBS}) | target_link_libraries(tutorial2 ${KDE4_KDEUI_LIBS}) | ||
− | </ | + | </syntaxhighlight> |
− | ===Compile | + | ===Compile isto=== |
− | + | Para compilar, crias as ligações e rodar, use: | |
mkdir build && cd build | mkdir build && cd build | ||
cmake .. | cmake .. | ||
Line 122: | Line 122: | ||
./tutorial2 | ./tutorial2 | ||
− | == | + | ==Siga em frente== |
− | + | Agora você pode ir ao tutorial [[Development/Tutorials/Using_KActions_(pt_BR)|usando KActions]]. | |
[[Category:C++]] | [[Category:C++]] |
Latest revision as of 16:50, 15 July 2012
Série de Tutoriais | Tutorial para iniciantes |
Pré-requisitos | Tutorial 1 - Alô Mundo |
Qual é o próximo | Tutorial 3 - KActions e XMLGUI |
Leitura adicional | KXmlGuiWindow |
Resumo
Este tutorial dá continuidade ao First Program Tutorial e introduzirá a classe KXmlGuiWindow.
No tutorial anterior, o programa criava uma caixa de diálogo pop up, mas nós agora iremos andar alguns passos em direção ao funcionamento de uma aplicação.
KXmlGuiWindow
KXmlGuiWindow fornece a visão de uma janela principal inteira com barra de menus, barra de ferramentas, uma barra de status e uma área no centro para um widget grande. Muitas aplicações KDE derivam desta classe já que ela fornece um jeito fácil de definir o layout do menu e barra de ferramentas através de arquivos XML (esta tecnologia é chamada de XMLGUI). Por enquanto não usaremos o XMLGUI neste tutorial, mas nós o usaremos no próximo.
In order to have a useful KXmlGuiWindow, we must subclass it. So we create two files, a mainwindow.cpp and a mainwindow.h which will contain our code.
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
First we Subclass KXmlGuiWindow on line 7 with class MainWindow : public KXmlGuiWindow.
Então nós declaramos o construtor com 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"
2
3 MainWindow::MainWindow(QWidget *parent) : KXmlGuiWindow(parent)
4 {
5 textArea = new KTextEdit();
6 setCentralWidget(textArea);
7 setupGUI();
8 }
Primeiro, é claro, na linha 1 nós temos que incluir o arquivo de cabeçalho contendo a declaração da classe.
Na linha 5, nós iniciamos nosso editor de texto com um objeto. Então na linha 6 nós usamos a função setCentralWidget() do KXmlGuiWindow a qual chama o KXmlGuiWindow que deverá aparecer na parte central da janela.
Finalmente, KXmlGuiWindow::setupGUI() é chamado, a qual faz várias coisas por-trás-dos-bastidores e cria a barra de menu padrão (Configurações, Ajuda).
De volta ao main.cpp
No intuito de executar atualmente esta janela, nós adicionamos umas poucas linhas no main.cpp
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( "tutorial2", 0,
10 ki18n("Tutorial 2"), "1.0",
11 ki18n("A simple text area"),
12 KAboutData::License_GPL,
13 ki18n("Copyright (c) 2007 Developer") );
14 KCmdLineArgs::init( argc, argv, &aboutData );
15
16 KApplication app;
17
18 MainWindow* window = new MainWindow();
19 window->show();
20
21 return app.exec();
22 }
As únicas linhas novas aqui (comparadas com o Tutorial 1) são 5, 18 e 19. Na linha 18, nós criamos o nosso objeto MainWindow e então, na linha 19, nós a exibimos.
CMake
The best way to build the program is to use CMake. All that's changed since tutorial 1 is that mainwindow.cpp has been added to the sources list and any tutorial1 has become tutorial2.
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})
Compile isto
Para compilar, crias as ligações e rodar, use:
mkdir build && cd build cmake .. make ./tutorial2
Siga em frente
Agora você pode ir ao tutorial usando KActions.