xiongxiongchuan
7th July 2010, 17:15
i am using the Qt 4.6.2 and i can't update the databases,but after i inserted ,i can update the new row into the dabasese;
i have asked the question before,but nobody would liket to help me, i am confused by this question for a long time ,anyone can help me ?plz!
void WellDataManage::loadWellData(QString projectAddress)
{
QString address=projectAddress;
address+="//";
address+="Data";
address+="//";
address+="SystemData";
address+=".db3";
if (!QFile::exists(address))
{
QMessageBox::information(this,tr("Information"),tr("There is something wrong with the wells databases"),QMessageBox::Ok,QMessageBox::Ok);
}
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(address);
db.open();
welldatamodel = new QSqlTableModel;
welldatamodel->setEditStrategy(QSqlTableModel::OnManualSubmit);
welldatamodel->setTable("wells");
welldatamodel->setSort(0,Qt::AscendingOrder);
welldatamodel->select();
//set Comobox model
//wellTypeComoBox->setModel(welldatamodel);
//wellTypeComoBox->setModelColumn(welldatamodel->fieldIndex("welltype"));
//set header name
welldatamodel->setHeaderData(0,Qt::Horizontal,tr("Name"));
welldatamodel->setHeaderData(1,Qt::Horizontal,tr("Group"));
welldatamodel->setHeaderData(2,Qt::Horizontal,tr("X"));
welldatamodel->setHeaderData(3,Qt::Horizontal,tr("Y"));
welldatamodel->setHeaderData(4,Qt::Horizontal,tr("Type"));
welldatamodel->setHeaderData(5,Qt::Horizontal,tr("Production"));
welldatamodel->setHeaderData(6,Qt::Horizontal,tr("Depth"));
welldatamodel->setHeaderData(7,Qt::Horizontal,tr("BeginDepth"));
welldatamodel->setHeaderData(8,Qt::Horizontal,tr("EndDepth"));
welldatamodel->setHeaderData(9,Qt::Horizontal,tr("Altitude"));
welldatamodel->removeColumn(0);
wellTableView->setModel(welldatamodel);
wellTableView->setSelectionBehavior(QAbstractItemView::SelectRows );
wellTableView->resizeRowsToContents();
welldatamapper=new QDataWidgetMapper(this);
welldatamapper->setModel(welldatamodel);
welldatamapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
//welldatamapper->setItemDelegate(new QSqlRelationalDelegate(welldatamapper));
welldatamapper->addMapping(wellNameEdit,0);
welldatamapper->addMapping(wellGroupEdit,1);
welldatamapper->addMapping(wellXPositionEdit,2);
welldatamapper->addMapping(wellYPositionEdit,3);
welldatamapper->addMapping(wellTypeComoBox,4);
welldatamapper->addMapping(wellProductionEdit,5);
welldatamapper->addMapping(wellBeginDepthEdit,7);
welldatamapper->addMapping(wellEndDepthEdit,8);
welldatamapper->addMapping(wellAltitudeEdit,9);
//create connect of the buttons
welldatamapper->toFirst();//åˆå§‹åŒ–时选择选 择第一行
connect(wellTableView->selectionModel(),SIGNAL(currentRowChanged(QModelIn dex, QModelIndex)),welldatamapper,SLOT(setCurrentModelI ndex(QModelIndex) ));
connect(welldatamapper,SIGNAL(currentIndexChanged( int)),wellTableView,SLOT(selectRow (int)));
connect(firstAction,SIGNAL(triggered()),welldatama pper,SLOT(toFirst()));
connect(lastAction,SIGNAL(triggered()),welldatamap per,SLOT(toLast()));
connect(previousAction,SIGNAL(triggered()),welldat amapper,SLOT(toPrevious()));
connect(nextAction,SIGNAL(triggered()),welldatamap per,SLOT(toNext()));
connect(newAction,SIGNAL(triggered()),this,SLOT(ad dWellRecord()));
connect(deleteAction,SIGNAL(triggered()),this,SLOT (deleteWellRecord()));
connect(updateAction,SIGNAL(triggered()),this,SLOT (updateWellRecord()));
}
void WellDataManage::addWellRecord()
{
int row=welldatamapper->currentIndex();
if (row==-1)
{
row=0;
}
welldatamapper->submit();
welldatamodel->insertRow(row);
welldatamapper->setCurrentIndex(row);
wellNameEdit->clear();
wellGroupEdit->clear();
wellXPositionEdit->clear();
wellYPositionEdit->clear();
wellBeginDepthEdit->clear();
wellEndDepthEdit->clear();
wellProductionEdit->clear();
wellTypeComoBox->clear();
wellNameEdit->setFocus();
}
void WellDataManage::deleteWellRecord()
{
int row=welldatamapper->currentIndex();
welldatamodel->removeRow(row);
welldatamapper->submit();
welldatamodel->submit();
welldatamodel->submitAll();
//welldatamapper->setCurrentIndex(QMIN(row,welldatamodel->rowCount()-1));
}
void WellDataManage::updateWellRecord()
{
welldatamapper->submit();
welldatamodel->submit();
welldatamodel->submitAll();
}
i have asked the question before,but nobody would liket to help me, i am confused by this question for a long time ,anyone can help me ?plz!
void WellDataManage::loadWellData(QString projectAddress)
{
QString address=projectAddress;
address+="//";
address+="Data";
address+="//";
address+="SystemData";
address+=".db3";
if (!QFile::exists(address))
{
QMessageBox::information(this,tr("Information"),tr("There is something wrong with the wells databases"),QMessageBox::Ok,QMessageBox::Ok);
}
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(address);
db.open();
welldatamodel = new QSqlTableModel;
welldatamodel->setEditStrategy(QSqlTableModel::OnManualSubmit);
welldatamodel->setTable("wells");
welldatamodel->setSort(0,Qt::AscendingOrder);
welldatamodel->select();
//set Comobox model
//wellTypeComoBox->setModel(welldatamodel);
//wellTypeComoBox->setModelColumn(welldatamodel->fieldIndex("welltype"));
//set header name
welldatamodel->setHeaderData(0,Qt::Horizontal,tr("Name"));
welldatamodel->setHeaderData(1,Qt::Horizontal,tr("Group"));
welldatamodel->setHeaderData(2,Qt::Horizontal,tr("X"));
welldatamodel->setHeaderData(3,Qt::Horizontal,tr("Y"));
welldatamodel->setHeaderData(4,Qt::Horizontal,tr("Type"));
welldatamodel->setHeaderData(5,Qt::Horizontal,tr("Production"));
welldatamodel->setHeaderData(6,Qt::Horizontal,tr("Depth"));
welldatamodel->setHeaderData(7,Qt::Horizontal,tr("BeginDepth"));
welldatamodel->setHeaderData(8,Qt::Horizontal,tr("EndDepth"));
welldatamodel->setHeaderData(9,Qt::Horizontal,tr("Altitude"));
welldatamodel->removeColumn(0);
wellTableView->setModel(welldatamodel);
wellTableView->setSelectionBehavior(QAbstractItemView::SelectRows );
wellTableView->resizeRowsToContents();
welldatamapper=new QDataWidgetMapper(this);
welldatamapper->setModel(welldatamodel);
welldatamapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
//welldatamapper->setItemDelegate(new QSqlRelationalDelegate(welldatamapper));
welldatamapper->addMapping(wellNameEdit,0);
welldatamapper->addMapping(wellGroupEdit,1);
welldatamapper->addMapping(wellXPositionEdit,2);
welldatamapper->addMapping(wellYPositionEdit,3);
welldatamapper->addMapping(wellTypeComoBox,4);
welldatamapper->addMapping(wellProductionEdit,5);
welldatamapper->addMapping(wellBeginDepthEdit,7);
welldatamapper->addMapping(wellEndDepthEdit,8);
welldatamapper->addMapping(wellAltitudeEdit,9);
//create connect of the buttons
welldatamapper->toFirst();//åˆå§‹åŒ–时选择选 择第一行
connect(wellTableView->selectionModel(),SIGNAL(currentRowChanged(QModelIn dex, QModelIndex)),welldatamapper,SLOT(setCurrentModelI ndex(QModelIndex) ));
connect(welldatamapper,SIGNAL(currentIndexChanged( int)),wellTableView,SLOT(selectRow (int)));
connect(firstAction,SIGNAL(triggered()),welldatama pper,SLOT(toFirst()));
connect(lastAction,SIGNAL(triggered()),welldatamap per,SLOT(toLast()));
connect(previousAction,SIGNAL(triggered()),welldat amapper,SLOT(toPrevious()));
connect(nextAction,SIGNAL(triggered()),welldatamap per,SLOT(toNext()));
connect(newAction,SIGNAL(triggered()),this,SLOT(ad dWellRecord()));
connect(deleteAction,SIGNAL(triggered()),this,SLOT (deleteWellRecord()));
connect(updateAction,SIGNAL(triggered()),this,SLOT (updateWellRecord()));
}
void WellDataManage::addWellRecord()
{
int row=welldatamapper->currentIndex();
if (row==-1)
{
row=0;
}
welldatamapper->submit();
welldatamodel->insertRow(row);
welldatamapper->setCurrentIndex(row);
wellNameEdit->clear();
wellGroupEdit->clear();
wellXPositionEdit->clear();
wellYPositionEdit->clear();
wellBeginDepthEdit->clear();
wellEndDepthEdit->clear();
wellProductionEdit->clear();
wellTypeComoBox->clear();
wellNameEdit->setFocus();
}
void WellDataManage::deleteWellRecord()
{
int row=welldatamapper->currentIndex();
welldatamodel->removeRow(row);
welldatamapper->submit();
welldatamodel->submit();
welldatamodel->submitAll();
//welldatamapper->setCurrentIndex(QMIN(row,welldatamodel->rowCount()-1));
}
void WellDataManage::updateWellRecord()
{
welldatamapper->submit();
welldatamodel->submit();
welldatamodel->submitAll();
}