Hello again!
I have following code:
CBrowserWindow
::CBrowserWindow(QWidget *parent, MySQLConnect
* pDataBase,
QString sTableName
){
Q_CHECK_PTR(pDataBase); // if database connection is NULL, we have a big problem
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_pDatabase->record(sTableName); // fetches column names
m_pTableRecord=&pDataBase->m_pDatabase->record(sTableName); // fetches column names
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(pDataBase, m_pColumnNames); // creates browser controls
delete m_pModel; // deletes pModel;
delete m_pTableRecord; // deletes m_pTableRecord
// just to be sure
m_pModel=0;
m_pTableRecord=0;
}
CBrowserWindow::CBrowserWindow(QWidget *parent, MySQLConnect* pDataBase, QString sTableName)
: QWidget(parent)
{
Q_CHECK_PTR(pDataBase); // if database connection is NULL, we have a big problem
m_pModel=new QSqlRelationalTableModel(this); // 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_pDatabase->record(sTableName); // fetches column names
m_pTableRecord=&pDataBase->m_pDatabase->record(sTableName); // fetches column names
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(pDataBase, m_pColumnNames); // creates browser controls
delete m_pModel; // deletes pModel;
delete m_pTableRecord; // deletes m_pTableRecord
// just to be sure
m_pModel=0;
m_pTableRecord=0;
}
To copy to clipboard, switch view to plain text mode
and in the line
m_pTableRecord=&pDataBase->m_pDatabase->record(sTableName); // fetches column names
m_pTableRecord=&pDataBase->m_pDatabase->record(sTableName); // fetches column names
To copy to clipboard, switch view to plain text mode
I get segmentation fault. What I am trying to do is to dynamically fetch table column names from mysql database and then create table widget for record display. Why do I get segfault?
Here is class description:
class CBrowserWindow
: public QWidget{
Q_OBJECT
public:
CBrowserWindow
(QWidget *parent
= 0, MySQLConnect
* pDataBase
=0,
QString sTableName
="");
~CBrowserWindow();
private:
QStringList* m_pColumnNames;
// column names for selected database
private:
// buttons
QPushButton* m_pButtonAdd;
// button for addition of record QPushButton* m_pButtonChange;
// button for change of record QPushButton* m_pButtonDelete;
// button for deletion of record QPushButton* m_pButtonSelect;
// button for selection of record
private:
// layouts
private:
// mebmers needed for column idetification
QSqlRecord* m_pTableRecord;
// pointer to database record quint16 m_iIndex; // index in for loops - this type is guaranteed to be 16-bit on all platforms supported by Qt
private:
// private methods
void createBrowserContens
(MySQLConnect
* pDatabase,
QStringList* pColumnNames
);
// creates table on widget };
#endif
class CBrowserWindow : public QWidget
{
Q_OBJECT
public:
CBrowserWindow(QWidget *parent = 0, MySQLConnect* pDataBase=0, QString sTableName="");
~CBrowserWindow();
private:
QStringList* m_pColumnNames; // column names for selected database
private:
// buttons
QPushButton* m_pButtonAdd; // button for addition of record
QPushButton* m_pButtonChange; // button for change of record
QPushButton* m_pButtonDelete; // button for deletion of record
QPushButton* m_pButtonSelect; // button for selection of record
private:
// layouts
QHBoxLayout* m_pHorizLayout; // horizontal leyout
QVBoxLayout* m_pVertLayout; // vertical layout
private:
// mebmers needed for column idetification
QSqlRelationalTableModel* m_pModel; // pointer to pmodel
QSqlRecord* m_pTableRecord; // pointer to database record
QTableView* m_pView; // pointer to view
QGridLayout* m_pGridLayout; // pointer to grid layout
quint16 m_iIndex; // index in for loops - this type is guaranteed to be 16-bit on all platforms supported by Qt
private:
// private methods
void createBrowserContens(MySQLConnect* pDatabase, QStringList* pColumnNames); // creates table on widget
};
#endif
To copy to clipboard, switch view to plain text mode
Bookmarks