PDA

View Full Version : things on QSqlRelationalTableModel , QDataWidgetMapper and QTableView



xiongxiongchuan
13th June 2010, 09:33
i create a QTableView to show databases conten,and use some QLineEdits to eidit the record,i use the QDataWidgetMapper to connect the QSqlRelationalTableModel with the QLineEidt,i am wondering is there any ways to have this function that when i click the QTableView rows and the QLineEdit will show the relatived record.

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 QSqlRelationalTableModel;
welldatamodel->setTable("wells");
welldatamodel->setEditStrategy(QSqlTableModel::OnManualSubmit);
welldatamodel->setSort(0,Qt::AscendingOrder);

//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->select();
welldatamodel->removeColumn(0);

wellTableView->setModel(welldatamodel);
wellTableView->setSelectionBehavior(QAbstractItemView::SelectRows );
// wellTableView->resizeColumnsToContents();

// wellTableView->horizontalHeader()->setStretchLastSection(true);

welldatamapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
welldatamapper->setModel(welldatamodel);
welldatamapper->setItemDelegate(new QSqlRelationalDelegate(this));

welldatamapper->addMapping(wellNameEdit,0);
welldatamapper->addMapping(wellGroupEdit,1);
welldatamapper->addMapping(wellXPositionEdit,2);
welldatamapper->addMapping(wellYPositionEdit,3);

welldatamapper->addMapping(wellProductionEdit,5);
/*welldatamapper->addMapping(wellDepthEdit,6);*/
welldatamapper->addMapping(wellBeginDepthEdit,7);
welldatamapper->addMapping(wellEndDepthEdit,8);
welldatamapper->addMapping(wellAltitudeEdit,9);

//create connect of the buttons
connect(firstAction,SIGNAL(triggered()),welldatama pper,SLOT(toFirst()));
connect(lastAction,SIGNAL(triggered()),welldatamap per,SLOT(toLast()));
connect(previousAction,SIGNAL(triggered()),welldat amapper,SLOT(toLast()));
connect(nextAction,SIGNAL(triggered()),welldatamap per,SLOT(toNext()));
//connect(addAction,SIGNAL(triggered()),welldatamapp er,SLOT())

wysota
13th June 2010, 09:42
Use QDataWidgetMapper::setCurrentIndex() or QDataWidgetMapper::setCurrentModelIndex() as a reaction to an appropriate signal from the table or its selection model.

xiongxiongchuan
13th June 2010, 12:07
i used the connect as this below

connect(welldatamapper,SIGNAL(setCurrentModelIndex ()),wellTableView,SLOT(currentChanged()));
connect(wellTableView,SIGNAL(currentChanged()),wel ldatamapper,SLOT(setCurrentModelIndex()));

but it din't work!

tbscope
13th June 2010, 12:30
SIGNAL(setCurrentModelIndex())
That's not a signal.



SLOT(currentChanged())
That's not a slot.

To quote the documentation:

The following example illustrates how to update all widgets with new data whenever the selection of a QTableView named myTableView changes:


QDataWidgetMapper *mapper = new QDataWidgetMapper();

connect(myTableView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)) ,
mapper, SLOT(setCurrentModelIndex(QModelIndex)));

wysota
13th June 2010, 12:31
The first statement is obviously incorrect.