Results 1 to 5 of 5

Thread: Database and QSqlRelationalTableModel problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Red face Database and QSqlRelationalTableModel problem

    Why does the following code:
    Qt Code:
    1. CBrowserWindow::CBrowserWindow(QWidget *parent, CDatabaseFoundation* pDatabase, QString sTableName, QString windowCaption)
    2. : QDialog(parent)
    3. {
    4. m_pModel=new QSqlRelationalTableModel(parent); // creates new model
    5. Q_CHECK_PTR(m_pModel); // checks for succesful creation
    6. m_pModel->setTable(sTableName); // sets table name
    7.  
    8. m_pColumnNames=new QStringList(); // creates new string list
    9. Q_CHECK_PTR(m_pColumnNames); // checks for succesful creation
    10.  
    11. // fetches record
    12. m_pTableRecord=new QSqlRecord(); // creates new record
    13. Q_CHECK_PTR(m_pTableRecord); // checks for susccesful creation
    14. m_pTableRecord=&pDatabase->m_Database.record(sTableName); // fetches column names
    15. QSqlRecord tempRec=pDatabase->m_Database.record(sTableName); // fetches column names
    16. m_pTableRecord=new QSqlRecord(tempRec); // read column names
    17. Q_CHECK_PTR(m_pTableRecord);
    18.  
    19. if (!m_pTableRecord->isEmpty()) {
    20. for (m_iIndex=0; m_iIndex<m_pTableRecord->count(); m_iIndex++) {
    21. m_pColumnNames->append(m_pTableRecord->fieldName(m_iIndex));
    22. }
    23. } // if
    24.  
    25. createBrowserContens(this, pDatabase, m_pColumnNames); // creates browser controls
    26.  
    27. setWindowTitle(windowCaption); // sets window title
    28.  
    29. /*
    30.   delete m_pModel; // deletes pModel;
    31.   delete m_pTableRecord; // deletes m_pTableRecord
    32.   // just to be sure
    33.   m_pModel=0;
    34.   m_pTableRecord=0;
    35. */
    36. }
    To copy to clipboard, switch view to plain text mode 
    Here is also createBrowserContens(..):
    Qt Code:
    1. void CBrowserWindow::createBrowserContens(QWidget* pParent, CDatabaseFoundation* pDatabase, QStringList* pColumnNames) {
    2. m_pHorizLayout=new QHBoxLayout();
    3. Q_CHECK_PTR(m_pHorizLayout); // chechks creation of horiz layout
    4. m_pVertLayout=new QVBoxLayout();
    5. Q_CHECK_PTR(m_pVertLayout); // checks creation of vertical layout
    6.  
    7. m_pView=new QTableView(this);
    8. Q_CHECK_PTR(m_pView); // checks creation of view
    9.  
    10. for(m_iIndex=0; m_iIndex<pColumnNames->count(); m_iIndex++) {
    11. pDatabase->m_pModel->setHeaderData(m_iIndex, Qt::Horizontal, m_pColumnNames->at(m_iIndex)); // sets column names
    12. qDebug() << m_iIndex << ": " << m_pColumnNames->at(m_iIndex);
    13. //pView->showColumn(m_iIndex); // shows the selected column
    14. }
    15.  
    16. m_pView->setModel(pDatabase->m_pModel); // sets model
    17. m_pView->setItemDelegate(new QSqlRelationalDelegate(m_pView));
    18.  
    19. m_pButtonAdd=new QPushButton(QObject::trUtf8("Dodaj"));
    20. Q_CHECK_PTR(m_pButtonAdd); // checks creation of pushbutton
    21.  
    22. m_pButtonChange=new QPushButton(QObject::trUtf8("Spremeni"));
    23. Q_CHECK_PTR(m_pButtonChange); // checks creation of pushbutton
    24.  
    25. m_pButtonDelete=new QPushButton(QObject::trUtf8("Briši"));
    26. Q_CHECK_PTR(m_pButtonDelete); // checks creation of pushbutton
    27.  
    28. m_pButtonClose=new QPushButton(QObject::trUtf8("Zapri"));
    29. Q_CHECK_PTR(m_pButtonClose);
    30.  
    31. // leyout setup
    32. m_pHorizLayout->addWidget(m_pButtonAdd);
    33. m_pHorizLayout->addWidget(m_pButtonChange);
    34. m_pHorizLayout->addWidget(m_pButtonDelete);
    35. m_pHorizLayout->addWidget(m_pButtonClose);
    36.  
    37. m_pVertLayout->addWidget(m_pView);
    38.  
    39. m_pVertLayout->addLayout(m_pHorizLayout);
    40.  
    41. pParent->setLayout(m_pVertLayout);
    42.  
    43. // button connectors
    44. connect(m_pButtonClose, SIGNAL(clicked()), this, SLOT(close()));
    45. }
    To copy to clipboard, switch view to plain text mode 
    does NOT display data fields in window??
    Qt 5.3 Opensource & Creator 3.1.2

  2. #2
    Join Date
    Jun 2007
    Location
    Munich
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default 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!

  3. #3
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Database and QSqlRelationalTableModel problem

    Quote Originally Posted by harryf View Post
    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!
    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.
    Qt 5.3 Opensource & Creator 3.1.2

  4. #4
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default 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?
    Qt 5.3 Opensource & Creator 3.1.2

  5. #5
    Join Date
    Jun 2007
    Location
    Munich
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default 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).

Similar Threads

  1. Threads and database connection
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 7th August 2013, 08:30
  2. [QMYSQL] connection problem
    By chaos_theory in forum Installation and Deployment
    Replies: 5
    Last Post: 2nd July 2007, 09:52
  3. problem in doing database program
    By wei243 in forum Qt Programming
    Replies: 0
    Last Post: 4th April 2007, 12:01
  4. Oracle Database Problem
    By magikalpnoi in forum Qt Programming
    Replies: 3
    Last Post: 27th September 2006, 21:19
  5. Filling combobox from database
    By Philip_Anselmo in forum Qt Programming
    Replies: 3
    Last Post: 11th May 2006, 17:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.