Results 1 to 3 of 3

Thread: Strange problem with mysql and QTableView.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    4

    Default Strange problem with mysql and QTableView.

    Hi everyone, a newbie at qt requesting a bit of assistance here.

    My problem is that I am trying to connect to a mysql database, and then load data from a table into a QTableView. Unfortunately, I have run into some difficulties.

    I have done all the necessary mysql bits such as compiling a plugin and such, and I believe I did things correctly since I have managed to create a connection. My problem is that while the code works in one specific circumstance, it does not in another.

    My test program is composed of two files, main.cpp and mainwindow.cpp. I am using qtcreator to make this program. I have looked up on how to load data into QTableView and if I do the loading in the main.cpp, it works right. I get a window with the correct data.

    However, if I try to do the EXACT same thing with the exact same code from mainwindow.cpp, it fails to work at all. I get no errors or such, the data simply fails to load.

    Here is a sample of the code I am using to make a connection that works:

    main.cpp:
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. void initializeModel(QSqlTableModel *model)
    5. {
    6. model->setTable("TABLE");
    7. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    8. model->select();
    9. }
    10.  
    11.  
    12. QTableView *createView(const QString &title, QSqlTableModel *model)
    13. {
    14. QTableView *view = new QTableView;
    15. view->setModel(model);
    16. view->setWindowTitle(title);
    17. return view;
    18. }
    19.  
    20.  
    21. int main(int argc, char *argv[])
    22. {
    23. QApplication a(argc, argv);
    24.  
    25.  
    26. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    27. db.setHostName("HOSTNAME");
    28. db.setDatabaseName("DATABASENAME");
    29. db.setUserName("USERNAME");
    30. db.setPassword("PASSWORD");
    31.  
    32. if (!db.open()) {
    33. qDebug() << db.lastError();
    34. return 1;
    35. }
    36.  
    37.  
    38. initializeModel(&model);
    39.  
    40. QTableView *view1 = createView(QObject::tr("Table Model"), &model);
    41. view1->show();
    42.  
    43. // MainWindow w;
    44. // w.show();
    45.  
    46.  
    47. return a.exec();
    48. }
    To copy to clipboard, switch view to plain text mode 

    The code above works. However, if I comment the sql code here, and remove the comments that open the MainWindow, and put the same code there, things no longer work.

    Mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. #include <QtGui>
    5. #include <QtSql>
    6.  
    7.  
    8. MainWindow::MainWindow(QWidget *parent) :
    9. QMainWindow(parent),
    10. ui(new Ui::MainWindow)
    11. {
    12. ui->setupUi(this);
    13. }
    14.  
    15. MainWindow::~MainWindow()
    16. {
    17. delete ui;
    18. }
    19.  
    20. void initializeModel(QSqlTableModel *model)
    21. {
    22. model->setTable("TABLE");
    23. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    24. model->select();
    25. }
    26.  
    27. QTableView *createView(const QString &title, QSqlTableModel *model)
    28. {
    29. QTableView *view = new QTableView;
    30. view->setModel(model);
    31. view->setWindowTitle(title);
    32. return view;
    33. }
    34.  
    35.  
    36. void MainWindow::on_pushButton_clicked()
    37. {
    38. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    39. db.setHostName("HOSTNAME");
    40. db.setDatabaseName("DATABASENAME");
    41. db.setUserName("USERNAME");
    42. db.setPassword("PASSWORD");
    43.  
    44.  
    45. if (!db.open()) {
    46. qDebug() << db.lastError();
    47. }
    48.  
    49.  
    50. initializeModel(&model);
    51.  
    52. QTableView *view1 = createView(QObject::tr("Table Model"), &model);
    53. view1->show();
    54. }
    To copy to clipboard, switch view to plain text mode 


    The code is exactly the same, except it runs from a button click rather than automatically. The information (hostname,databasename,etc are correct).
    I have no idea whats wrong. Any advice or ideas would be much appreciated.
    Thanks

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Strange problem with mysql and QTableView.

    The QSqlTableModel you create at line 49 is on the stack, goes out of scope at the end of that routine, and leaves the view without a model to display. Create it on the heap.

    It works in the first example because the model stays in scope long fro the entire lifetime of the view.

  3. The following user says thank you to ChrisW67 for this useful post:

    Datakim (11th September 2011)

  4. #3
    Join Date
    Jun 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Strange problem with mysql and QTableView.

    in other words, add the following line to mainwindow.h

    QTableView *view1;
    QSqlTableModel model;

    and assign them in mainwindow.cpp just with the names view1 and model

  5. The following user says thank you to CroOm for this useful post:

    Datakim (11th September 2011)

Similar Threads

  1. MySQL driver - Very strange problem. PLEASE HELP ME. Thanks.
    By diego in forum General Programming
    Replies: 15
    Last Post: 3rd March 2011, 01:33
  2. Strange problem with a QTableView
    By pippo42 in forum Qt Programming
    Replies: 0
    Last Post: 3rd May 2010, 13:46
  3. Replies: 7
    Last Post: 13th August 2009, 16:11
  4. Replies: 2
    Last Post: 2nd June 2009, 14:57
  5. MySQL driver - Very strange problem. PLEASE HELP ME. Thanks.
    By diego in forum Installation and Deployment
    Replies: 0
    Last Post: 2nd June 2009, 01:56

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.