Difference between revisions of "Development/Tutorials/KIO Slaves/Using KIO Slaves in your Program"

m (Text replace - "<code cppqt>" to "<syntaxhighlight lang="cpp-qt">")
m (Text replace - "<code>" to "<syntaxhighlight lang="text">")
Line 85: Line 85:
== CMakeLists.txt ==
== CMakeLists.txt ==
<syntaxhighlight lang="text">
PROJECT( browser )
PROJECT( browser )

Revision as of 20:44, 29 June 2011

In this chapter we write a simple browser to show how easy it is to use kioslaves in KDE 4. The browser will download the page www.kde.org and show the html sourcecode. It will not render html - remember this is a programming demo. The purpose of kioslaves is to have access to a given storage or data representation without the need for blocking calls. This is solved via QT's signal/slot mechanism: Once the data has arrived, e.g. from a web page, it triggers a signal. The signal is connected to the respective slot and can start processing the data.

The files


<syntaxhighlight lang="cpp-qt">

  1. include <QString>
  2. include <kapplication.h>
  3. include <kaboutdata.h>
  4. include <klocalizedstring.h>
  5. include <kmessagebox.h>
  6. include <kcmdlineargs.h>
  7. include <KMainWindow>
  8. include <browser.h>

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

 // KAboutData (const QByteArray &appName, const QByteArray &catalogName, const KLocalizedString &programName, const QByteArray &version,
 const QByteArray& ba=QByteArray("test");
 const KLocalizedString name=ki18n("myName");
 KAboutData aboutData( ba, ba, name, ba, name);
 KCmdLineArgs::init( argc, argv, &aboutData );
 KApplication khello;
 Browser *mw = new Browser();

} The purpose of main is to create a Browser instance and hold it open while the KDE event loop is running (i.e. the KDE program is ready).


<syntaxhighlight lang="cpp-qt">

  1. ifndef KDE4START_H__
  2. define KDE4START_H__
  1. include <kmainwindow.h>
  2. include <kio/scheduler.h>
  3. include <kurl.h>
  4. include <kio/jobclasses.h>

class Browser : public QWidget {

 public slots:
   void slotButtonClicked();
   void dataIsHere(KIO::Job *,const QByteArray &);


  1. endif


<syntaxhighlight lang="cpp-qt">

  1. include <kio/scheduler.h>
  2. include <kurl.h>
  3. include <kio/jobclasses.h>
  4. include <kdebug.h>
  5. include <browser.h>

class TransferJob;

Browser::Browser() : QWidget(NULL) {



void Browser::slotButtonClicked() {

 kDebug() << "entering function";
 // creating a kioslave
 KIO::TransferJob *job = KIO::get(KUrl("http://www.kde.org"));
 connect (job, SIGNAL(  data(KIO::Job *, const QByteArray & )), this, SLOT(dataIsHere(KIO::Job *,const QByteArray &)));


void Browser::dataIsHere(KIO::Job *,const QByteArray & data ) {

 kDebug() << "data is here";
 kDebug() << data;

} As discussed, the Browser class does not do a busy wait for the data to arrive from the web. Instead, it uses the http kioslave's signal data by connecting it to the slot dataishere. dataIsHere gets the http transmission via its parameter data and just outputs it. That's it - no blocking, no busy wait!


<syntaxhighlight lang="text"> PROJECT( browser ) FIND_PACKAGE(KDE4 REQUIRED) INCLUDE_DIRECTORIES( ${KDE4_INCLUDES} . )

SET(kde4startSources main.cpp browser.cpp )

KDE4_ADD_EXECUTABLE(browser ${kde4startSources} )



Kompile, link and run it

cmake . && make && ./browser

Content is available under Creative Commons License SA 4.0 unless otherwise noted.