Development/Tutorials/KCmdLineArgs (de): Difference between revisions

From KDE TechBase
m (Text replace - "<code cppqt>" to "<syntaxhighlight lang="cpp-qt">")
No edit summary
 
(4 intermediate revisions by one other user not shown)
Line 1: Line 1:
{{Template:I18n/Language Navigation Bar|Development/Tutorials/KCmdLineArgs}}


{{TutorialBrowser (de)|
{{TutorialBrowser (de)|
Line 23: Line 22:


===main.cpp===
===main.cpp===
<code cppqt n>
<syntaxhighlight lang="cpp-qt" line>
#include <KApplication>
#include <KApplication>
#include <KAboutData>
#include <KAboutData>
Line 57: Line 56:
   return app.exec();
   return app.exec();
}
}
</code>
</syntaxhighlight>


===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 89: Line 88:
   
   
#endif
#endif
</code>
</syntaxhighlight>


===mainwindow.cpp===
===mainwindow.cpp===
<code cppqt n>
<syntaxhighlight lang="cpp-qt" line>
#include "mainwindow.h"
#include "mainwindow.h"
   
   
Line 205: Line 204:
   }
   }
}
}
</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="tutorial5"
<gui name="tutorial5"
Line 229: Line 228:


</gui>
</gui>
</code>
</syntaxhighlight>
Dies ist identisch mit <tt>tutorial'X'ui.rc</tt> aus den letzten beiden Anleitungen ausser dass <tt>name</tt> zu 'tutorial5' geändert wurde.
Dies ist identisch mit <tt>tutorial'X'ui.rc</tt> aus den letzten beiden Anleitungen ausser dass <tt>name</tt> zu 'tutorial5' geändert wurde.


Line 240: Line 239:
<syntaxhighlight lang="cpp-qt">
<syntaxhighlight lang="cpp-qt">
void openFile(const QString &inputFileName);
void openFile(const QString &inputFileName);
</code>
</syntaxhighlight>


===mainwindow.cpp===
===mainwindow.cpp===
Line 255: Line 254:


===CMakeLists.txt===
===CMakeLists.txt===
<code ini n>
<syntaxhighlight lang="ini" line>
project(tutorial5)
project(tutorial5)
   
   
Line 274: Line 273:
install( FILES tutorial5ui.rc  
install( FILES tutorial5ui.rc  
         DESTINATION  ${DATA_INSTALL_DIR}/tutorial5 )
         DESTINATION  ${DATA_INSTALL_DIR}/tutorial5 )
</code>
</syntaxhighlight>


Mit dieser Datei kann die Anleitung auf die gleiche Weise erstellt und ausgeführt werden wie Anleitung 3 und 4. Für mehr Informationen siehe [[Development/Tutorials/Using KActions (de)|Anleitung 3]].
Mit dieser Datei kann die Anleitung auf die gleiche Weise erstellt und ausgeführt werden wie Anleitung 3 und 4. Für mehr Informationen siehe [[Development/Tutorials/Using KActions (de)|Anleitung 3]].

Latest revision as of 15:45, 14 July 2012

Befehlszeilen Argumente (Under construction)
Anleitungsserie   Anleitung für Anfänger
Voriges Kapitel   Anleitung 4 - Laden und Speichern von Dateien
Nächstes Kapitel   Tutorial 6 - ###)
Weiterführende Texte   KCmdLineArgs KCmdLineOptions
Navigation   Deutsche Startseite

Zusammenfassung

In diesem Kapitel werden sie lernen ihrer Anwendung zu ermöglichen, Dateien über die Befehlszeile zu öffnen oder sogar mit 'Öffnen mit' von Dolphin aus zu verwenden.

The Code

main.cpp

#include <KApplication>
#include <KAboutData>
#include <KCmdLineArgs>
#include <KUrl> //new
 
#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; //new
  options.add("+[file]", ki18n("Document to open")); //new
  KCmdLineArgs::addCmdLineOptions(options); //new

  KApplication app;
 
  MainWindow* window = new MainWindow();
  window->show();

  KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); //new
  if(args->count()) //new
  {
    window->openFile(args->url(0).url()); //new
  }

  return app.exec();
}

mainwindow.h

#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); //new
  
  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

#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());
  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() //changed
{
  openFile(KFileDialog::getOpenFileName());
}

void MainWindow::openFile(const QString &inputFileName) //new
{
  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());
  }
}

tutorial4ui.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="clear" />
    </Menu>
  </MenuBar>

  <ToolBar name="mainToolBar" >
    <text>Main Toolbar</text>
    <Action name="clear" />
  </ToolBar>

</gui>

Dies ist identisch mit tutorial'X'ui.rc aus den letzten beiden Anleitungen ausser dass name zu 'tutorial5' geändert wurde.

Erklärung

mainwindow.h

Hier haben wir nichts weiteres gemacht als eine neue openFile Funktion hinzuzufügen, die einen QString annimmt:

void openFile(const QString &inputFileName);

mainwindow.cpp

Hier gibt es keinen neuen Code, nur Umordnen. Alles aus void openFile() wurde in void openFile(const QString &inputFileName) verschoben, ausser der Aufruf an KFileDialog::getOpenFileName().

Auf diese Weise können wir openFile() aufrufen, wenn wir einen Dialog anzeigen möchten, oder wir können openFile(QString) aufrufen, falls wir den Namen der Datei bereits kennen.

main.cpp

Hier passiert die ganze KCmdLineArgs Hexerei.

Erzeugen, Installieren und Ausführen

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 )

Mit dieser Datei kann die Anleitung auf die gleiche Weise erstellt und ausgeführt werden wie Anleitung 3 und 4. Für mehr Informationen siehe Anleitung 3.

mkdir build && cd build
cmake .. -DCMAKE_INSTALL_PREFIX=$HOME
make install
$HOME/bin/tutorial5

Weiter Geht's

Now you can move on to the ### (TODO User:milliams) tutorial.