KDE Tutorial - p8

p8

Next Previous Table of Contents

p8 is an upgraded p6 with the ability to send to p7 the url in the selected item of the list. This will allow p7 to open the favourite urls.


#include <kapp.h>
#include "p8.h"
 
int main( int argc, char **argv )
{
    KApplication a( argc, argv, "p8" );
 
    MainList *mylist=new MainList;
    mylist->resize( 300, 200 );
 
    a.setMainWidget( mylist );
    mylist->show();
 
    return a.exec();
}

main.cpp


#include <dcopobject.h>
#include <qstring.h>
 
class p8Iface : virtual public DCOPObject
{
    K_DCOP

k_dcop:
    virtual void add( const QString s ) = 0;
 
};

p8Iface.h


#include "p8Iface.h"
#include <qlistview.h>
 
class MainList : public QListView, virtual public p8Iface
{
 Q_OBJECT
 
public:
 
 MainList();
 
 void add ( const QString url );
public slots:
 
 void setURLInBrowser ( QListViewItem *item );
 
};

p8.h


#include "p8.h"
#include <klocale.h>
#include <kapp.h>
#include <dcopclient.h>
#include <kdebug.h>
 
MainList::MainList() : QListView ( 0L, "Bookmarks" ),
                        DCOPObject ( "bookmarkList" )
{
  addColumn( i18n("My Bookmarks") );
  connect( this, SIGNAL(clicked(QListViewItem *)), 
		this, SLOT(setURLInBrowser(QListViewItem *)));
 
  DCOPClient *client=kapp->dcopClient();
  client->attach();
  client->registerAs("p8");
}
 
void MainList::add( const QString url )
{
  insertItem ( new QListViewItem ( this , url ) );
}
 
void MainList::setURLInBrowser( QListViewItem *item )
{
  if (item==0L) return;
  DCOPClient *client=kapp->dcopClient();
  QByteArray params;
  QDataStream stream(params, IO_WriteOnly);
  stream << item->text(0);
  if (!client->send("p7-*", "browser", "setURL(QString)", params))
    kdDebug() << "Error with DCOP\n";
}

p8.cpp

main.cpp, p8Iface.h are very similar to the respective files in p6, so I won't comment them.

  connect( this, SIGNAL(clicked(QListViewItem *)), 
		this, SLOT(setURLInBrowser(QListViewItem *)));

We've added a new slot, setURLInBrowser(QListViewItem *), which passes the url in the QListViewItem to the browser (p7).

This is a curious example of how you can even connect a signal in an object to a slot in the same object. Of course, this may not be the best way to do it, but at least it's clean to be used on a tutorial like this one.

Just in case you don't remember from p6, QListViewItem is the class that represents an item on a QListView. When you click on a item, the QListView object emits a signal with a parameter that is the QListViewItem that the user clicked on.

void MainList::setURLInBrowser( QListViewItem *item )
{
  if (item==0L) return;

In this slot, we first make sure that the user clicked on an item. If the user clicks on an empty area, the widget also emits the clicked signal (the user clicked on it after all), but with a NULL parameter. In this case we just do nothing.

  DCOPClient *client=kapp->dcopClient();
  QByteArray params;
  QDataStream stream(params, IO_WriteOnly);
  stream << item->text(0);

If we have an url to send, we get the DCOPCLient object as we do in p5 or p7 to send a DCOP message and then prepare the data stream.

With item->text(0) we get the text in the first column of the clicked item (the first column is the only one we have created), and store it on the stream.

  if (!client->send("p7-*", "browser", "setURL(QString)", params))
    kdebugError( "Error with DCOP");
}

Finally, we send a DCOP call to the application p7, object browser and method setURL(QString) with parameter params.

We are now reaching the end of the tutorial, but first, we will make some modifications to simplify the sources while using the latest available technologies.

Next Previous Table of Contents


© 1999,2000,2001 Antonio Larrosa