Results 1 to 4 of 4

Thread: Multiples QSqlQueryModel and QTableView

  1. #1
    Join Date
    Apr 2010
    Posts
    9
    Thanked 3 Times in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Question Multiples QSqlQueryModel and QTableView

    Hi, I'm making multiples queries to a database by just pressing a QPushButton and reading the query from a QLineEdit. I need to show that queries in a QTabWidget that has multiples QTableViews each one in a different tab. I created a QThread, an in the run() a send a signal with one dynamic model to the QDialog where I have the QTabWidget. Here's the code:

    Qt Code:
    1. class GeneralSearchThread : public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit GeneralSearchThread( QObject *parent = 0 );
    6. ~GeneralSearchThread();
    7. void makeQuery( const QString &query );
    8.  
    9. signals:
    10. void modelReadytoRead( QSqlQueryModel *model ); //this signal will be connected to the QDialog.
    11.  
    12. public slots:
    13.  
    14. protected:
    15. void run();
    16.  
    17. private:
    18. QString nameUser;
    19. QString passUser;
    20. QString queryString;
    21.  
    22. QList< QSqlQueryModel *> listaModels;
    23. };
    24.  
    25. GeneralSearchThread::GeneralSearchThread( QObject *parent ) : QThread( parent )
    26. {
    27. }
    28.  
    29. GeneralSearchThread::~GeneralSearchThread()
    30. {
    31. wait();
    32.  
    33. qDeleteAll(listaModels); /// I get the problem here!!!
    34. listaModels.clear(); /// I get the problem here!!!
    35. }
    36.  
    37. void GeneralSearchThread::makeQuery( const QString &query )
    38. {
    39. queryString = query;
    40. start();
    41. }
    42.  
    43. void GeneralSearchThread::run()
    44. {
    45. QSqlDatabase db = QSqlDatabase::database( "someName" );
    46. if( db.open() )
    47. {
    48. model->setQuery( queryString, db );
    49. listaModels.append( model );
    50. emit modelReadytoRead( model );
    51. db.close();
    52. }
    53. }
    To copy to clipboard, switch view to plain text mode 

    The question here, is that I'm creating the QSqlQueryModels dynamically and trying to store them in a QList (which I call listaModels) and delete it in the destructor of the QThread. But I get a problem of Segmentation fault . But when I put away the lines inside the destructor I get no problems, but I'm afraid of a memory leak. Does anyone know how to solve this problem?, the QSqlQueryModels get deleted when the driver is closed and I'm re-deleting them?. Thanks a lot in advanced.
    Extra Info: Driver: QMYSQL3, OS: Ubuntu 9.04, gcc version 4.4.1, Qt version 4.6.

  2. #2
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiples QSqlQueryModel and QTableView

    Look, your models got a "db" parameter in their constructors, but it's lifitime is only to run function finish. Maybe model's destructor want to read or write to QSqlDatabase ?

  3. #3
    Join Date
    Apr 2010
    Location
    United States
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multiples QSqlQueryModel and QTableView

    I believe your problem may be caused by db.close(); on line 52. Your query model objects are pointers that are remaining in scope with a reference to this QSqlDatabase object after it has been closed.

    void QSqlDatabase::close ()
    Closes the database connection, freeing any resources acquired, and invalidating any existing QSqlQuery objects that are used with the database.

    This will also affect copies of this QSqlDatabase object.

    See also removeDatabase().
    Perhaps using a persistent database connection which remains in scope until after you delete your query models will work better. You can close your database connection in the deconstructor after deleting the queries. Unless you need connections to multiple different databases from the same thread this would also be more efficient as you won't need to constantly open and close connections as well.

  4. #4
    Join Date
    Apr 2010
    Posts
    9
    Thanked 3 Times in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Multiples QSqlQueryModel and QTableView

    Hi borisbn and ajg85 thanks a lot for the answers, they help me to find out the right answer. All I had to do was just like ajg85 said: using a persistent database connection which remains in scope. Also I've found a better way to delete the models QSqlQueryModel and the QTableViews. The QTableViews are deleted when I delete the QTabWidget because the QTabWidget gets the QTableViews as childs by using the inserTab function. For the QSqlQueryModels, I used a QList of QSharedPointers and I don't have to worry for delete them. I include the code for those who had the same problem:

    The .h:
    Qt Code:
    1. class GeneralSearchThread : public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit GeneralSearchThread( QObject *parent = 0 );
    6. ~GeneralSearchThread();
    7. void makeQuery( const QString &query );
    8.  
    9. signals:
    10. void modelReadytoRead( QSqlQueryModel *model );
    11.  
    12. protected:
    13. void run();
    14.  
    15. private:
    16. QString queryString;
    17. QList< QSharedPointer<QSqlQueryModel> > sqlQueryModelList; // God bless templates.
    18. };
    To copy to clipboard, switch view to plain text mode 

    The .cpp:
    Qt Code:
    1. GeneralSearchThread::GeneralSearchThread( QObject *parent ) : QThread( parent )
    2. {
    3. }
    4.  
    5. GeneralSearchThread::~GeneralSearchThread()
    6. {
    7. wait();
    8. }
    9.  
    10. void GeneralSearchThread::makeQuery( const QString &query )
    11. {
    12. queryString = query;
    13. start();
    14. }
    15.  
    16. void GeneralSearchThread::run()
    17. {
    18. QSqlDatabase db = QSqlDatabase::database( "someName" );
    19. if( db.open() )
    20. {
    21. QSharedPointer<QSqlQueryModel> model = QSharedPointer<QSqlQueryModel>( new QSqlQueryModel );
    22. sqlQueryModelList.append( model );
    23. model.data()->setQuery( queryString, db );
    24. emit modelReadytoRead( model.data() );
    25. db.close();
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

    To two things. First: there wasn't any problem by calling the db.close() method at the end of the run() method of the QThread.
    Second: In my QDialog, in the .h I created a QSqlDatabase db; variable, and in the .cpp in the destructor, I call the db.close() method. I did not call any removeDatabase().

    That's all, thanks a lot guys for the answers, Now I run my program and I don't get any nasty message in the console, I just get the beautiful exited with code 0.

Similar Threads

  1. Replies: 2
    Last Post: 2nd March 2010, 04:24
  2. SQL transactions with QSqlQueryModel/QTableView
    By estanisgeyer in forum Qt Programming
    Replies: 2
    Last Post: 15th June 2009, 18:52
  3. QTableView/QSqlQueryModel
    By norobro in forum Qt Programming
    Replies: 7
    Last Post: 15th February 2008, 21:52
  4. QTableView, QSqlQueryModel and delegates
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 29th January 2008, 12:04
  5. QSqlQueryModel and QTableView question
    By wbt_ph in forum Newbie
    Replies: 4
    Last Post: 20th September 2006, 15:40

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.