I have a form with 2 PuchButtons and a TableView created using QTCreator. Now I want to populate TableView with data from SQL database using QSqlTableModel.
My question is: Why when i allocate QSqlTableModel in PushButton1Click everything is ok, and when I allacate it in MainWindow constructor the data from database doesn't show on TableView.
(First I click button 1 to open database, then I click button 2 to fetch data)
Here is declaration of mainwindow class:
Qt Code:
  1. class MainWindow : public QMainWindow
  2. {
  3. Q_OBJECT
  4.  
  5. public:
  6. MainWindow(QWidget *parent = 0);
  7. ~MainWindow();
  8.  
  9. private:
  10. Ui::MainWindow *ui;
  11.  
  12. private slots:
  13. bool PushButton1Click();
  14. bool PushButton2Click();
  15.  
  16. };
To copy to clipboard, switch view to plain text mode 

Here is code which works:
Qt Code:
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3.  
  4. MainWindow::MainWindow(QWidget *parent)
  5. : QMainWindow(parent), ui(new Ui::MainWindow)
  6. {
  7. ui->setupUi(this);
  8. // m = new QSqlTableModel(this); // NOT OK !!!!!!!!
  9. }
  10.  
  11.  
  12. MainWindow::~MainWindow()
  13. {
  14. delete ui;
  15. }
  16.  
  17.  
  18. bool MainWindow::PushButton1Click()
  19. {
  20. QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
  21. db.setHostName("localhost");
  22. db.setDatabaseName("axis");
  23. db.setUserName("root");
  24. db.setPassword("kosa1980");
  25. bool ret = db.open();
  26.  
  27. m = new QSqlTableModel(this); // OK !!!!!!!!
  28. return ret;
  29. }
  30.  
  31. bool MainWindow::PushButton2Click()
  32. {
  33. m->setTable("wagi");
  34. m->setEditStrategy(QSqlTableModel::OnManualSubmit);
  35. m->select();
  36. m->removeColumn(0);
  37. m->setHeaderData(0, Qt::Horizontal, QObject::tr("numer_seryjny"));
  38. m->setHeaderData(1, Qt::Horizontal, QObject::tr("typ"));
  39.  
  40. ui->tableView->setModel(m);
  41.  
  42. return true;
  43. }
To copy to clipboard, switch view to plain text mode 


Here is code which doesn't work:
Qt Code:
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3.  
  4. MainWindow::MainWindow(QWidget *parent)
  5. : QMainWindow(parent), ui(new Ui::MainWindow)
  6. {
  7. ui->setupUi(this);
  8. m = new QSqlTableModel(this); // NOT OK !!!!!!!!
  9. }
  10.  
  11. MainWindow::~MainWindow()
  12. {
  13. delete ui;
  14. }
  15.  
  16. bool MainWindow::PushButton1Click()
  17. {
  18. QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
  19. db.setHostName("localhost");
  20. db.setDatabaseName("axis");
  21. db.setUserName("root");
  22. db.setPassword("kosa1980");
  23. bool ret = db.open();
  24.  
  25. //m = new QSqlTableModel(this); // OK !!!!!!!!
  26. return ret;
  27. }
  28.  
  29. bool MainWindow::PushButton2Click()
  30. {
  31. m->setTable("wagi");
  32. m->setEditStrategy(QSqlTableModel::OnManualSubmit);
  33. m->select();
  34. m->removeColumn(0);
  35. m->setHeaderData(0, Qt::Horizontal, QObject::tr("numer_seryjny"));
  36. m->setHeaderData(1, Qt::Horizontal, QObject::tr("typ"));
  37.  
  38. ui->tableView->setModel(m);
  39.  
  40. return true;
  41. }
To copy to clipboard, switch view to plain text mode 

Why second version doesn't work?