Results 1 to 2 of 2

Thread: Problems with allocating QSqlTableModel on heap

  1. #1
    Join Date
    Jun 2009
    Posts
    1
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question Problems with allocating QSqlTableModel on heap

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problems with allocating QSqlTableModel on heap

    Before you create the new model in the c-tor, also set up an default database connection! Then it will work. The model needs a default database connection (if you don't specify anything else...)


    EDIT: Even if your password is only for your localhost, it is not a good idea to post it here. (Since most people use a single password for multiple logins and occasions)

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

    kosa (22nd June 2009)

Similar Threads

  1. QSqlTableModel and QTableView selection problems
    By innerhippy in forum Qt Programming
    Replies: 5
    Last Post: 19th December 2008, 06:48
  2. QODBC, QSqlTableModel, and submit problems
    By darkadept in forum Qt Programming
    Replies: 3
    Last Post: 18th May 2008, 13:46

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.