PDA

View Full Version : Database and QSqlRelationalTableModel problem



MarkoSan
19th October 2007, 00:27
Why does the following code:

CBrowserWindow::CBrowserWindow(QWidget *parent, CDatabaseFoundation* pDatabase, QString sTableName, QString windowCaption)
: QDialog(parent)
{
m_pModel=new QSqlRelationalTableModel(parent); // creates new model
Q_CHECK_PTR(m_pModel); // checks for succesful creation
m_pModel->setTable(sTableName); // sets table name

m_pColumnNames=new QStringList(); // creates new string list
Q_CHECK_PTR(m_pColumnNames); // checks for succesful creation

// fetches record
m_pTableRecord=new QSqlRecord(); // creates new record
Q_CHECK_PTR(m_pTableRecord); // checks for susccesful creation
m_pTableRecord=&pDatabase->m_Database.record(sTableName); // fetches column names
QSqlRecord tempRec=pDatabase->m_Database.record(sTableName); // fetches column names
m_pTableRecord=new QSqlRecord(tempRec); // read column names
Q_CHECK_PTR(m_pTableRecord);

if (!m_pTableRecord->isEmpty()) {
for (m_iIndex=0; m_iIndex<m_pTableRecord->count(); m_iIndex++) {
m_pColumnNames->append(m_pTableRecord->fieldName(m_iIndex));
}
} // if

createBrowserContens(this, pDatabase, m_pColumnNames); // creates browser controls

setWindowTitle(windowCaption); // sets window title

/*
delete m_pModel; // deletes pModel;
delete m_pTableRecord; // deletes m_pTableRecord
// just to be sure
m_pModel=0;
m_pTableRecord=0;
*/
}Here is also createBrowserContens(..):

void CBrowserWindow::createBrowserContens(QWidget* pParent, CDatabaseFoundation* pDatabase, QStringList* pColumnNames) {
m_pHorizLayout=new QHBoxLayout();
Q_CHECK_PTR(m_pHorizLayout); // chechks creation of horiz layout
m_pVertLayout=new QVBoxLayout();
Q_CHECK_PTR(m_pVertLayout); // checks creation of vertical layout

m_pView=new QTableView(this);
Q_CHECK_PTR(m_pView); // checks creation of view

for(m_iIndex=0; m_iIndex<pColumnNames->count(); m_iIndex++) {
pDatabase->m_pModel->setHeaderData(m_iIndex, Qt::Horizontal, m_pColumnNames->at(m_iIndex)); // sets column names
qDebug() << m_iIndex << ": " << m_pColumnNames->at(m_iIndex);
//pView->showColumn(m_iIndex); // shows the selected column
}

m_pView->setModel(pDatabase->m_pModel); // sets model
m_pView->setItemDelegate(new QSqlRelationalDelegate(m_pView));

m_pButtonAdd=new QPushButton(QObject::trUtf8("Dodaj"));
Q_CHECK_PTR(m_pButtonAdd); // checks creation of pushbutton

m_pButtonChange=new QPushButton(QObject::trUtf8("Spremeni"));
Q_CHECK_PTR(m_pButtonChange); // checks creation of pushbutton

m_pButtonDelete=new QPushButton(QObject::trUtf8("Briši"));
Q_CHECK_PTR(m_pButtonDelete); // checks creation of pushbutton

m_pButtonClose=new QPushButton(QObject::trUtf8("Zapri"));
Q_CHECK_PTR(m_pButtonClose);

// leyout setup
m_pHorizLayout->addWidget(m_pButtonAdd);
m_pHorizLayout->addWidget(m_pButtonChange);
m_pHorizLayout->addWidget(m_pButtonDelete);
m_pHorizLayout->addWidget(m_pButtonClose);

m_pVertLayout->addWidget(m_pView);

m_pVertLayout->addLayout(m_pHorizLayout);

pParent->setLayout(m_pVertLayout);

// button connectors
connect(m_pButtonClose, SIGNAL(clicked()), this, SLOT(close()));
}does NOT display data fields in window??

harryf
19th October 2007, 17:26
Are you calling select() on your model? setTableName() only populates the headers, but does not execute a query to fetch the data.

Also:

m_pTableRecord=&pDatabase->m_Database.record(sTableName); // fetches column names

this line is scary - you are taking the address of a temporary object. m_pTableRecord becomes a dangling pointer!

MarkoSan
19th October 2007, 18:09
Are you calling select() on your model? setTableName() only populates the headers, but does not execute a query to fetch the data.

Also:

m_pTableRecord=&pDatabase->m_Database.record(sTableName); // fetches column names

this line is scary - you are taking the address of a temporary object. m_pTableRecord becomes a dangling pointer!

:D:D:D:D I am dealing with this line right now, marcel helped me a lot with it. But for now, I've chcked, column names are fetched correctly. I will check now for select() method execution. Thanks a lot for now.

MarkoSan
20th October 2007, 00:48
I finally solved the problem, I had m_pModel on two different places (in CDatabaseFoundation and in CBrowserWindow). I've deleted the one in CBrowserWindow and works fine. For future references, what do you think, where should be m_pModel (QSqlRelationTableModel) declared in class that handles database connection or in class that handles record browsing?

harryf
22nd October 2007, 10:21
A database model encapsulates a result set sent from the database. I would always keep it as local as possible (in your case, in the record browsing class) to prevent concurrency errors (e.g. when refactoring, two views might end up accessing the same model leading to strange race conditions).