Adding a data sensitive widget
Prev | Chapter 3. Adding data sensitive widgets | Next

## Adding a data sensitive widget

Back to our example application. We add now a hk_kdelineedit field to display the data of the previously created data source "mydatasource". We want to see the data of the column "name".

Example 3.1. Adding a lineedit field

    #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>

    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");
        mydatasource->enable();

        widget->show();

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

Have a look at the source code. We create a hk_kdelineeditfield and then set the data source with set_datasource. With set_column the name of an existing column in this data source is set. After enabling it you can view and alter the data. What you can't do at the moment is browsing the data source. For this we add a hk_kderowselector. Refer to the knoda tutorial on how to use the row selector.

Example 3.2. Adding a row selector

    #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>

    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);

        mydatasource->enable();

        widget->show();

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

It is up to you to add the other fields of this data source to this widget.

Prev | Up | Next
Chapter 3. Adding data sensitive widgets | Home | Adding a grid with a depending data source