Database and QSqlRelationalTableModel problem
Why does the following code:
Code:
CBrowserWindow
::CBrowserWindow(QWidget *parent, CDatabaseFoundation
* pDatabase,
QString sTableName,
QString windowCaption
){
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(..):
Code:
void CBrowserWindow
::createBrowserContens(QWidget* pParent, CDatabaseFoundation
* pDatabase,
QStringList* pColumnNames
) { Q_CHECK_PTR(m_pHorizLayout); // chechks creation of horiz layout
Q_CHECK_PTR(m_pVertLayout); // checks creation of vertical layout
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
Q_CHECK_PTR(m_pButtonAdd); // checks creation of pushbutton
Q_CHECK_PTR(m_pButtonChange); // checks creation of pushbutton
Q_CHECK_PTR(m_pButtonDelete); // checks creation of pushbutton
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??
Re: Database and QSqlRelationalTableModel problem
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!
Re: Database and QSqlRelationalTableModel problem
Quote:
Originally Posted by
harryf
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.
Re: Database and QSqlRelationalTableModel problem [SOLVED]
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?
Re: Database and QSqlRelationalTableModel problem
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).