Results 1 to 3 of 3

Thread: Sqlite data base

  1. #1
    Join Date
    Apr 2011
    Posts
    27
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Post Sqlite data base

    sir i have a database i want to take city name in combobox when i select city then according to city all sector in combobox .please help me my code is this
    /////header//////////////
    #ifndef WINDOW_H
    #define WINDOW_H

    #include <QWidget>

    QT_BEGIN_NAMESPACE
    class QComboBox;
    class QDataWidgetMapper;
    class QItemSelectionModel;
    class QLabel;
    class QSqlRelationalTableModel;
    class QStandardItemModel;
    class QStringListModel;
    class QTextEdit;
    class QSqlTableModel;
    QT_END_NAMESPACE

    //! [Window definition]
    class Window : public QWidget
    {
    Q_OBJECT

    public:
    Window(QWidget *parent = 0);

    private slots:


    private:
    void setupModel();

    QLabel *nameLabel;
    QLabel *typeLabel;
    QComboBox *cityComboBox;
    QComboBox *typeComboBox;
    QSqlRelationalTableModel *model;
    QItemSelectionModel *selectionModel;
    QDataWidgetMapper *mapper;
    QSqlTableModel *relModel;
    QSqlTableModel *re;
    int index;
    int typeIndex;
    };
    //! [Window definition]

    #endif
    /////////////////////.cpp//////////////////
    #include <QtGui>
    #include <QtSql>

    #include "window.h"

    //! [Set up widgets]
    Window::Window(QWidget *parent)
    : QWidget(parent)
    {
    setupModel();

    nameLabel = new QLabel(tr("Na&me:"));
    cityComboBox = new QComboBox();
    typeLabel = new QLabel(tr("&Type:"));
    typeComboBox = new QComboBox();


    nameLabel->setBuddy( cityComboBox);
    typeLabel->setBuddy(typeComboBox);
    //! [Set up widgets]

    //! [Set up the mapper]
    re=new QSqlTableModel ();
    re=model->relationModel(index);
    cityComboBox->setModel(re );
    cityComboBox->setModelColumn(re->fieldIndex("name"));
    relModel=new QSqlTableModel() ;
    relModel = model->relationModel(typeIndex);
    typeComboBox->setModel(relModel);
    typeComboBox->setModelColumn(relModel->fieldIndex("area"));

    mapper = new QDataWidgetMapper(this);
    mapper->setModel(model);
    mapper->setItemDelegate(new QSqlRelationalDelegate(this));
    //mapper->addMapping( areaComboBox, model->fieldIndex("name"));
    // mapper->addMapping(addressEdit, model->fieldIndex("address"));
    mapper->addMapping(cityComboBox,index);
    mapper->addMapping(typeComboBox, typeIndex);




    //! [Set up the mapper]

    //! [Set up connections and layouts]


    QGridLayout *layout = new QGridLayout();
    layout->addWidget(nameLabel, 0, 0, 1, 1);
    layout->addWidget( cityComboBox, 0, 1, 1, 1);
    layout->addWidget(typeLabel,1, 0, 1, 1);
    layout->addWidget(typeComboBox, 1, 1, 1, 1);
    setLayout(layout);



    }
    //! [Set up connections and layouts]

    //! [Set up the main table]
    void Window::setupModel()
    {
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(":memory:");
    if (!db.open()) {
    QMessageBox::critical(0, tr("Cannot open database"),
    tr("Unable to establish a database connection.\n"
    "This example needs SQLite support. Please read "
    "the Qt SQL driver documentation for information how "
    "to build it."), QMessageBox::Cancel);
    return;
    }

    QSqlQuery query;
    query.exec("create table person (Id int primary key,name varchar(20), typeid int)");
    query.exec("insert into person values(1, 'Delhi',101)");
    query.exec("insert into person values(2, 'Gurgaon',102)");
    query.exec("insert into person values(3, 'Noida',103)");
    query.exec("insert into person values(4, 'Gaziabad',104)");
    query.exec("insert into person values(5, 'faridabad',105)");
    //! [Set up the main table]

    //! [Set up the address type table]
    query.exec("create table addresstype (id int, area varchar(20))");
    query.exec("insert into addresstype values(101, 'susantlok')");
    query.exec("insert into addresstype values(101, 'dlf phase2')");
    query.exec("insert into addresstype values(101, 'phase1')");

    model = new QSqlRelationalTableModel(this);
    model->setTable("person");


    index=model->fieldIndex("Id");
    model->setRelation(index,QSqlRelation("person", "Id", "name"));

    typeIndex = model->fieldIndex("typeid");
    model->setRelation(typeIndex,QSqlRelation("addresstype ", "id", "area"));
    model->select();
    }
    please help me i am tired

  2. #2
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sqlite data base

    Generally, people are not going to help you if you provide some code and 'here you go'.

    some tips:
    • state the problem clearly
    • show what you have already tried
    • use code tags so your source code is more readable
    • check out some of the Qt examples
    Last edited by schnitzel; 19th May 2011 at 18:01. Reason: updated contents

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Sqlite data base

    Generally, people are not going to help you if if you provide some code and 'here you go'
    ... or ask a series of the same question in various guises:
    http://www.qtcentre.org/threads/4170...ht=#post190898
    http://www.qtcentre.org/threads/4124...ht=#post188944
    http://www.qtcentre.org/threads/4130...ht=#post189242

    and largely ignore the responses.

    The forum participants do not exist to do your learning for you, or to do your paying work for you. Both of those things are up to you.


    Added after 16 minutes:


    To help you help yourself, try to correct this summary of your question.

    This is the problem:
    • You have a list of cities
    • You have a related list of sectors for each city
    • You have a form with two combo boxes
    • One combo box should display the list of cities and allow selection of one.
    • When you select a city in the combo box, you want the other combo box to show the sectors related to that city. When no city is selected the other combo box should be empty.
    Last edited by ChrisW67; 19th May 2011 at 23:43.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

Similar Threads

  1. Update changes in QTableView to sqlite data base
    By nagabathula in forum Qt Programming
    Replies: 2
    Last Post: 12th November 2011, 01:18
  2. Sqlite data base
    By sabbu in forum Qt Programming
    Replies: 5
    Last Post: 19th May 2011, 11:57
  3. Replies: 4
    Last Post: 19th December 2010, 06:15
  4. QTable - Data Base Problem
    By esq in forum Qt Tools
    Replies: 9
    Last Post: 27th May 2007, 23:08
  5. program for working with Data Base(i need it)
    By banakil in forum Qt Programming
    Replies: 2
    Last Post: 10th January 2007, 22:58

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.