Results 1 to 10 of 10

Thread: QTableView from SQLite, empty!

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2007
    Posts
    27
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question QTableView from SQLite, empty!

    I have broken the sql/relationaltablemodel example down into four distinct steps:

    1) Create the database connection:
    = sql/relationaltablemodel/relationaltablemodel.cpp:main() =
    Qt Code:
    1. if (!createConnection())
    2. return 1;
    To copy to clipboard, switch view to plain text mode 
    = sql/connection.h:createConnection() =
    Qt Code:
    1. static bool createConnection()
    2. {
    3. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    4. db.setDatabaseName(":memory:");
    5. if (!db.open()) {
    6. QMessageBox::critical(0, qApp->tr("Cannot open database"),
    7. qApp->tr("Unable to establish a database connection.\n"
    8. "This example needs SQLite support. Please read "
    9. "the Qt SQL driver documentation for information how "
    10. "to build it.\n\n"
    11. "Click Cancel to exit."), QMessageBox::Cancel);
    12. return false;
    13. }
    14.  
    15. return true;
    16. }
    To copy to clipboard, switch view to plain text mode 
    2) Create the table in the database and add some records:
    = sql/relationaltablemodel/relationaltablemodel.cpp:main() =
    Qt Code:
    1. createRelationalTables();
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void createRelationalTables()
    2. {
    3. QSqlQuery query;
    4. query.exec("create table employee(id int primary key, name varchar(20), city int, country int)");
    5. query.exec("insert into employee values(1, 'Espen', 5000, 47)");
    6. }
    To copy to clipboard, switch view to plain text mode 
    3) Create the relational table model and initialize it:
    = sql/relationaltablemodel/relationaltablemodel.cpp:main() =
    Qt Code:
    1.  
    2. initializeModel(&model);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void initializeModel(QSqlRelationalTableModel *model)
    2. {
    3. model->setTable("employee");
    4.  
    5. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    6.  
    7. model->select();
    8. }
    To copy to clipboard, switch view to plain text mode 
    4) Set the QTableView to use the model:
    = sql/relationaltablemodel/relationaltablemodel.cpp:main() =
    Qt Code:
    1. QTableView *view = createView(QObject::tr("Relational Table Model"), &model);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. QTableView *createView(const QString &title, QSqlTableModel *model)
    2. {
    3. QTableView *view = new QTableView;
    4. view->setModel(model);
    5. view->setItemDelegate(new QSqlRelationalDelegate(view));
    6. view->setWindowTitle(title);
    7. return view;
    8. }
    To copy to clipboard, switch view to plain text mode 

    Now here's my modified code, trying to do the same thing:
    1) Create the database connection:
    = mymuse/mainwindow.cpp:initMainWindow() =
    Qt Code:
    1. database = new SQLiteDB();
    2.  
    3. printf("1\n");
    4. database->createConnection();
    To copy to clipboard, switch view to plain text mode 
    = mymuse/sqlite.cpp =
    Qt Code:
    1. bool SQLiteDB::createConnection()
    2. {
    3. QString dbName = ":memory:";
    4.  
    5. QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE");
    6. database.setDatabaseName(dbName);
    7.  
    8. if(!database.open())
    9. {
    10. QMessageBox::critical(NULL, qApp->tr("Cannot open database"),
    11. qApp->tr("Unable to establish a connection with database '%1'.")
    12. .arg(dbName),
    13. QMessageBox::Cancel);
    14.  
    15. return false;
    16. }
    17.  
    18. return true;
    19. }
    To copy to clipboard, switch view to plain text mode 
    2) Create the table in the database and add some records:
    = mymuse/mainwindow.cpp:initMainWindow() =
    Qt Code:
    1. printf("2\n");
    2. database->createTable();
    To copy to clipboard, switch view to plain text mode 
    = mymuse/sqlite.cpp =
    Qt Code:
    1. bool SQLiteDB::execQuery(QString queryStr, bool showError)
    2. {
    3. QSqlQuery query;
    4.  
    5. if(!query.exec(queryStr))
    6. {
    7. queryErr = query.lastError();
    8. if(showError)
    9. {
    10. QMessageBox::critical(NULL, qApp->tr("Query error"),
    11. qApp->tr("%1\n%2")
    12. .arg(lastQueryError(SQL_DB_TEXT))
    13. .arg(lastQueryError(SQL_DRIVER_TEXT)),
    14. QMessageBox::Cancel);
    15. }
    16. return false;
    17. }
    18.  
    19. return true;
    20. }
    21.  
    22. void SQLiteDB::createTable()
    23. {
    24. execQuery("create table tracks (id integer primary key autoincrement, artist varchar(50), title varchar(80), "
    25. "rating integer, status integer)", true);
    26. execQuery("insert into tracks (artist, title, rating, status) "
    27. "values ('The Rolling Stones', 'Satisfaction', 33, 1)", true);
    28. }
    To copy to clipboard, switch view to plain text mode 
    3) Create the relational table model and initialize it:
    = mymuse/mainwindow.cpp:initMainWindow() =
    Qt Code:
    1. printf("3\n");
    2. database->initModel();
    To copy to clipboard, switch view to plain text mode 
    = mymuse/sqlite.cpp =
    Qt Code:
    1. void SQLiteDB::initModel()
    2. {
    3. model.setTable("tracks");
    4. model.setEditStrategy(QSqlTableModel::OnManualSubmit);
    5. model.select();
    6. }
    To copy to clipboard, switch view to plain text mode 
    4) Set the QTableView to use the model:
    = mymuse/mainwindow.cpp =
    Qt Code:
    1. printf("4\n");
    2. database->setView(trackTable);
    To copy to clipboard, switch view to plain text mode 
    = mymuse/sqlite.cpp =
    Qt Code:
    1. void SQLiteDB::setView(QTableView *table)
    2. {
    3. QSqlTableModel *tblModel = &model;
    4.  
    5. table->setModel(tblModel);
    6. table->setItemDelegate(new QSqlRelationalDelegate(table));
    7. }
    To copy to clipboard, switch view to plain text mode 

    I've created my UI using designer, and have used the multiple-inheritance model to implement it. I have a class (in mainwindow.h) called:

    class MainWindow : public QMainWindow, public Ui::MainWindow {};

    The code is in mainwindow.cpp.

    My application comes up, and numbers 1 thru 4 print out, but my QTableView remains empty, why?

    Sincerely,

    Gordon E.
    Last edited by grellsworth; 2nd July 2007 at 20: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.