Adding a grid with a depending data source

In the next step we want to display all the literature that Goethe (or any other author) has written. The data is in table "literature" so we have to create a new data source. Have a closer look at the code. The data source "literaturetable" is created and also a hk_kdegrid which is connected to "literaturetable". We only want to see the literature of the author who is actually displayed. The data source "literaturetable" is depending on the data source "mydatasource".

We have to inform the system which columns have connected values. In the master data source (mydatasource) it is the field "author_id" and in the actual data source (literaturetable) it is the field "author".

Important

Notice that the data source "literaturetable" is not enabled. Whenever the master data source (mydatasource) will be enabled, the depending data sources will also be enabled. When the master data source is disabled so are the depending data sources.

Example 3.3. Adding a grid with a depending data source

#define HAVE_SSTREAM 1
#include <kapp.h>
#include <kaboutdata.h>
#include <kcmdlineargs.h>
#include <klocale.h>
#include <qwidget.h>
#include <hk_classes.h>
#include <iostream>
#include <hk_kdemessages.h>
#include <hk_kdelineedit.h>
#include <hk_kderowselector.h>
#include <hk_kdegrid.h>

static const char *description =
    I18N_NOOP("A hk_kdeclasses example Application");

static const char *version = "v0.1";


int main(int argc, char **argv)
{
    KAboutData about("hk_kdeexample", I18N_NOOP("hk_kdeexample"), version, description, KAboutData::License_GPL, "(C) 2001 Horst Knorr", 0, 0, "hk_classes@knoda.org");
    about.addAuthor( "Horst Knorr", 0, "hk_classes@knoda.org" );
    KCmdLineArgs::init(argc, argv, &about);
    KApplication app;
    QWidget* widget = new QWidget;
    app.setMainWidget(widget);
    set_kdestandarddialogs();

    hk_drivermanager* mydrivermanager = new hk_drivermanager(false);
    if (mydrivermanager==NULL) {cout <<"error creating mydrivermanager"<<endl;exit(1);}
    hk_connection* myconnection = mydrivermanager->new_connection();
    if (myconnection==NULL) {cout <<"error creating myconnection"<<endl;exit(1);}
    myconnection->connect();

    hk_database* mydatabase=myconnection->new_database("exampledb");
    if (mydatabase==NULL) {cout <<"error creating mydatabase"<<endl;exit(1);}
    hk_datasource* mydatasource= mydatabase->new_table("authors");
    if (mydatasource==NULL) {cout <<"error creating mydatasource"<<endl;exit(1);}
    hk_kdelineedit* namefield = new hk_kdelineedit(widget);
    namefield->setGeometry(50,50,150,30);
    namefield->set_datasource(mydatasource);
    namefield->set_columnname("name");

    hk_kderowselector* rowselector = new hk_kderowselector(widget);
    rowselector->setGeometry(250,50,150,30);
    rowselector->set_datasource(mydatasource);


    hk_datasource* literaturetable = mydatabase->new_table("literature");
    literaturetable->set_depending_on(mydatasource);
    literaturetable->add_depending_fields("author","author_id");

    hk_kdegrid* grid = new hk_kdegrid(widget);
    grid->setGeometry(50,100,250,200);
    grid->set_datasource(literaturetable);


    mydatasource->enable();

    widget->show();

    int res= app.exec();
    delete mydrivermanager;
    return res;
}