Development/Tutorials/KCmdLineArgs (es): Difference between revisions
Neverendingo (talk | contribs) m (Text replace - "<code cppqt>" to "<syntaxhighlight lang="cpp-qt">") |
Neverendingo (talk | contribs) m (Text replace - "<code cppqt n>" to "<syntaxhighlight lang="cpp-qt" line>") |
||
Line 23: | Line 23: | ||
===main.cpp=== | ===main.cpp=== | ||
< | <syntaxhighlight lang="cpp-qt" line> | ||
#include <KApplication> | #include <KApplication> | ||
#include <KAboutData> | #include <KAboutData> | ||
Line 60: | Line 60: | ||
===mainwindow.h=== | ===mainwindow.h=== | ||
< | <syntaxhighlight lang="cpp-qt" line> | ||
#ifndef MAINWINDOW_H | #ifndef MAINWINDOW_H | ||
#define MAINWINDOW_H | #define MAINWINDOW_H | ||
Line 92: | Line 92: | ||
===mainwindow.cpp=== | ===mainwindow.cpp=== | ||
< | <syntaxhighlight lang="cpp-qt" line> | ||
#include "mainwindow.h" | #include "mainwindow.h" | ||
Revision as of 20:32, 29 June 2011
Development/Tutorials/KCmdLineArgs
Languages: عربي | Asturianu | Català | Česky | Kaszëbsczi | Dansk | Deutsch | English | Esperanto | Español | Eesti | فارسی | Suomi | Français | Galego | Italiano | 日本語 | 한국어 | Norwegian | Polski | Português Brasileiro | Română | Русский | Svenska | Slovenčina | Slovenščina | српски | Türkçe | Tiếng Việt | Українська | 简体中文 | 繁體中文
Serie | Tutorial para principiantes |
Requisitos previos | Tutorial 4 - Cargar y Guardar |
Siguiente | Tutorial 6 - ### (TODO User:milliams) |
Lectura avanzada | KCmdLineArgs KCmdLineOptions |
Resumen
Ahora que tenemos el editor de texto con el cual podemos cargar y guardar archivos, vamos a hacer que actúe mas como una aplicación de escritorio, permitiendo cargar archivos desde la linea de ordenes e incluso usar Abrir con en Dolphin.
El código
main.cpp
<syntaxhighlight lang="cpp-qt" line>
- include <KApplication>
- include <KAboutData>
- include <KCmdLineArgs>
- include <KUrl> //nuevo
- include "mainwindow.h"
int main (int argc, char *argv[]) { KAboutData aboutData( "tutorial5", "tutorial5", ki18n("Tutorial 5"), "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 );
KCmdLineOptions options; //nuevo options.add("+[file]", ki18n("Document to open")); //nuevo KCmdLineArgs::addCmdLineOptions(options); //nuevo
KApplication app;
MainWindow* window = new MainWindow(); window->show();
KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); //nuevo if(args->count()) //nuevo { window->openFile(args->url(0).url()); //nuevo }
return app.exec(); }
mainwindow.h
<syntaxhighlight lang="cpp-qt" line>
- ifndef MAINWINDOW_H
- define MAINWINDOW_H
- include <KXmlGuiWindow>
- include <KTextEdit>
class MainWindow : public KXmlGuiWindow { Q_OBJECT
public: MainWindow(QWidget *parent=0); void openFile(const QString &inputFileName); //nuevo
private: KTextEdit* textArea; void setupActions(); QString fileName;
private slots: void newFile(); void openFile(); void saveFile(); void saveFileAs(); void saveFileAs(const QString &outputFileName); };
- endif
mainwindow.cpp
<syntaxhighlight lang="cpp-qt" line>
- 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("Limpiar")); clearAction->setIcon(KIcon("document-new")); clearAction->setShortcut(Qt::CTRL + Qt::Key_W); actionCollection()->addAction("limpiar", 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()); 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() //cambiado { openFile(KFileDialog::getOpenFileName()); }
void MainWindow::openFile(const QString &inputFileName) //nuevo { QString tmpFile; if(KIO::NetAccess::download(inputFileName, tmpFile, this)) { QFile file(tmpFile); file.open(QIODevice::ReadOnly); textArea->setPlainText(QTextStream(&file).readAll()); fileName = inputFileName;
KIO::NetAccess::removeTempFile(tmpFile); } else { KMessageBox::error(this, KIO::NetAccess::lastErrorString()); } }
tutorial5ui.rc
<?xml version="1.0" encoding="UTF-8"?>
<gui name="tutorial5"
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="limpiar" />
</Menu>
</MenuBar>
<ToolBar name="mainToolBar" >
<text>Main Toolbar</text>
<Action name="limpiar" />
</ToolBar>
</gui>
Es idéntico al tutorialxui.rc de los dos tutoriales anteriores excepto que tutorialx ahora es tutorial5.
Explicación
mainwindow.h
Lo único que hemos añadido a partir del tutorial 4 es la función openFile que tiene como parámetro un QString. <syntaxhighlight lang="cpp-qt"> void openFile(const QString &inputFileName);
mainwindow.cpp
No tenemos nuevo código, solo lo hemos reordenado. Todo lo de void openFile() se ha movido a void openFile(const QString &inputFileName) excepto la llamada a KFileDialog::getOpenFileName().
De esta manera, podemos llamar a openFile() si queremos mostrar un diálogo, o podemos llamar a openFile(QString) si ya conocemos el nombre del archivo.
main.cpp
Aqui es donde la clase KCmdLineArgs realiza todo el trabajo.
Make, Instalar y Ejecutar
CMakeLists.txt
project(tutorial5)
find_package(KDE4 REQUIRED)
include_directories( ${KDE4_INCLUDES} )
set(tutorial5_SRCS
main.cpp
mainwindow.cpp
)
kde4_add_executable(tutorial5 ${tutorial5_SRCS})
target_link_libraries(tutorial5 ${KDE4_KDEUI_LIBS}
${KDE4_KIO_LIBS})
install(TARGETS tutorial5 DESTINATION ${BIN_INSTALL_DIR})
install( FILES tutorial5ui.rc
DESTINATION ${DATA_INSTALL_DIR}/tutorial5 )
Con este archivo, podemos construir y ejecutar este tutorial de la misma forma que los tutoriales 3 y 4. Para mas información, mira el tutorial 3.
mkdir build && cd build cmake .. -DCMAKE_INSTALL_PREFIX=$HOME make install $HOME/bin/tutorial5