KDE TechBase
  • Page
  • Discussion
  • Edit
  • History
KDE TechBase is a Wiki - You can help! Please contribute! Questions?

Development/Tutorials/KCmdLineArgs

< Development | Tutorials

Languages: English | Dansk | Deutsch | Français | Italiano | Norwegian | Русский | српски | Slovenščina | Suomi | 简体中文 | Galego | Español | Esperanto | Svenska | 한국어 | Română | Brazilian Portuguese | Česky | عربي | فارسی | Polski | 日本語 | Українська | Català

Command line arguments (Under construction User:milliams)
Tutorial Series   Beginner Tutorial
Prerequisites   Tutorial 4 - Loading and saving
What's Next   Tutorial 6 - ### (TODO User:milliams)
Further Reading   KCmdLineArgs KCmdLineOptions

Contents

  • 1 Abstract
  • 2 The Code
    • 2.1 main.cpp
    • 2.2 mainwindow.h
    • 2.3 mainwindow.cpp
    • 2.4 tutorial5ui.rc
  • 3 Explanation
    • 3.1 mainwindow.h
    • 3.2 mainwindow.cpp
    • 3.3 main.cpp
  • 4 Make, Install And Run
    • 4.1 CMakeLists.txt
  • 5 Moving On

[edit] Abstract

Now that we have a text editor which can open and save files. We will now make the editor act more like a desktop application by enabling it to open files from command line arguments or even using Open with from within Dolphin.

[edit] The Code

[edit] main.cpp

  1. #include <KApplication>
  2. #include <KAboutData>
  3. #include <KCmdLineArgs>
  4. #include <KUrl> //new
  5.  
  6. #include "mainwindow.h"
  7.  
  8. int main (int argc, char *argv[])
  9. {
  10. KAboutData aboutData( "tutorial5", "tutorial5",
  11. ki18n("Tutorial 5"), "1.0",
  12. ki18n("A simple text area which can load and save."),
  13. KAboutData::License_GPL,
  14. ki18n("Copyright (c) 2007 Developer") );
  15. KCmdLineArgs::init( argc, argv, &aboutData );
  16.  
  17. KCmdLineOptions options; //new
  18. options.add("+[file]", ki18n("Document to open")); //new
  19. KCmdLineArgs::addCmdLineOptions(options); //new
  20.  
  21. KApplication app;
  22.  
  23. MainWindow* window = new MainWindow();
  24. window->show();
  25.  
  26. KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); //new
  27. if(args->count()) //new
  28. {
  29. window->openFile(args->url(0).url()); //new
  30. }
  31.  
  32. return app.exec();
  33. }

[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. Q_OBJECT
  10. public:
  11. MainWindow(QWidget *parent=0);
  12. void openFile(const QString &inputFileName); //new
  13. private:
  14. KTextEdit* textArea;
  15. void setupActions();
  16. QString fileName;
  17.  
  18. private slots:
  19. void newFile();
  20. void openFile();
  21. void saveFile();
  22. void saveFileAs();
  23. void saveFileAs(const QString &outputFileName);
  24. };
  25.  
  26. #endif

[edit] mainwindow.cpp

  1. #include "mainwindow.h"
  2.  
  3. #include <KApplication>
  4. #include <KAction>
  5. #include <KLocale>
  6. #include <KActionCollection>
  7. #include <KStandardAction>
  8. #include <KFileDialog>
  9. #include <KMessageBox>
  10. #include <KIO/NetAccess>
  11. #include <KSaveFile>
  12. #include <QTextStream>
  13.  
  14. MainWindow::MainWindow(QWidget *parent)
  15.  : KXmlGuiWindow(parent),
  16. fileName(QString())
  17. {
  18. textArea = new KTextEdit;
  19. setCentralWidget(textArea);
  20.  
  21. setupActions();
  22. }
  23.  
  24. void MainWindow::setupActions()
  25. {
  26. KAction* clearAction = new KAction(this);
  27. clearAction->setText(i18n("Clear"));
  28. clearAction->setIcon(KIcon("document-new"));
  29. clearAction->setShortcut(Qt::CTRL + Qt::Key_W);
  30. actionCollection()->addAction("clear", clearAction);
  31. connect(clearAction, SIGNAL(triggered(bool)),
  32. textArea, SLOT(clear()));
  33.  
  34. KStandardAction::quit(kapp, SLOT(quit()),
  35. actionCollection());
  36.  
  37. KStandardAction::open(this, SLOT(openFile()),
  38. actionCollection());
  39.  
  40. KStandardAction::save(this, SLOT(saveFile()),
  41. actionCollection());
  42.  
  43. KStandardAction::saveAs(this, SLOT(saveFileAs()),
  44. actionCollection());
  45.  
  46. KStandardAction::openNew(this, SLOT(newFile()),
  47. actionCollection());
  48.  
  49. setupGUI();
  50. }
  51.  
  52. void MainWindow::newFile()
  53. {
  54. fileName.clear();
  55. textArea->clear();
  56. }
  57.  
  58. void MainWindow::saveFileAs(const QString &outputFileName)
  59. {
  60. KSaveFile file(outputFileName);
  61. file.open();
  62. QByteArray outputByteArray;
  63. outputByteArray.append(textArea->toPlainText());
  64. file.write(outputByteArray);
  65. file.finalize();
  66. file.close();
  67. fileName = outputFileName;
  68. }
  69.  
  70. void MainWindow::saveFileAs()
  71. {
  72. saveFileAs(KFileDialog::getSaveFileName());
  73. }
  74.  
  75. void MainWindow::saveFile()
  76. {
  77. if(!fileName.isEmpty())
  78. {
  79. saveFileAs(fileName);
  80. }
  81. else
  82. {
  83. saveFileAs();
  84. }
  85. }
  86.  
  87. void MainWindow::openFile() //changed
  88. {
  89. openFile(KFileDialog::getOpenFileName());
  90. }
  91.  
  92. void MainWindow::openFile(const QString &inputFileName) //new
  93. {
  94. QString tmpFile;
  95. if(KIO::NetAccess::download(inputFileName, tmpFile,
  96. this))
  97. {
  98. QFile file(tmpFile);
  99. file.open(QIODevice::ReadOnly);
  100. textArea->setPlainText(QTextStream(&file).readAll());
  101. fileName = inputFileName;
  102.  
  103. KIO::NetAccess::removeTempFile(tmpFile);
  104. }
  105. else
  106. {
  107. KMessageBox::error(this,
  108. KIO::NetAccess::lastErrorString());
  109. }
  110. }

[edit] tutorial5ui.rc

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE kpartgui SYSTEM "kpartgui.dtd">
  3. <gui name="tutorial5" version="1">
  4. <ToolBar name="mainToolBar" >
  5. <text>Main Toolbar</text>
  6. <Action name="clear" />
  7. </ToolBar>
  8. <MenuBar>
  9. <Menu name="file" >
  10. <Action name="clear" />
  11. </Menu>
  12. </MenuBar>
  13. </gui>

This is identical to the tutorialxui.rc from the last two tutorials except tutorialx is now tutorial5.

[edit] Explanation

[edit] mainwindow.h

Here we have done nothing but add a new openFile function which takes a QString

void openFile(const QString &inputFileName);

[edit] mainwindow.cpp

There's no new code here, only rearranging. Everything from void openFile() has been moved into void openFile(const QString &inputFileName) except the call to KFileDialog::getOpenFileName().

This way, we can call openFile() if we want to display a dialog, or we can call openFile(QString) if we know the name of the file already.

[edit] main.cpp

This is where all the KCmdLineArgs magic happens.

[edit] Make, Install And Run

[edit] CMakeLists.txt

  1. project(tutorial5)
  2.  
  3. find_package(KDE4 REQUIRED)
  4. include_directories( ${KDE4_INCLUDES} )
  5.  
  6. set(tutorial5_SRCS
  7. main.cpp
  8. mainwindow.cpp
  9. )
  10.  
  11. kde4_add_executable(tutorial5 ${tutorial5_SRCS})
  12.  
  13. target_link_libraries(tutorial5 ${KDE4_KDEUI_LIBS}
  14. ${KDE4_KIO_LIBS})
  15.  
  16. install(TARGETS tutorial5 DESTINATION ${BIN_INSTALL_DIR})
  17. install( FILES tutorial5ui.rc
  18. DESTINATION ${DATA_INSTALL_DIR}/tutorial5 )

With this file, the tutorial can built and run in the same way as tutorial 3 and 4. For more information, see tutorial 3.

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

[edit] Moving On

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

Retrieved from "http://techbase.kde.org/Development/Tutorials/KCmdLineArgs"
Categories: Tutorial | C++

Navigation

  • Home
  • Help
  • Recent changes

Sections

  • Getting started
  • Development
  • Schedules
  • Policies
  • Contribute
  • Projects

Toolbox

  • What links here
  • Related changes
  • Upload file
  • Special pages
  • Printable version
  • Permanent link

Personal tools

  • Log in / create account
  • Login with OpenID
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal