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
Bookmarks