Results 1 to 3 of 3

Thread: Model Or View problem

  1. #1
    Join Date
    Sep 2009
    Posts
    34
    Thanks
    28
    Qt products
    Qt4
    Platforms
    MacOS X

    Question Model Or View problem

    Ok here is the deal, I have the following code for opening up a database, connecting it to model, which then displays its contents on a view:

    Qt Code:
    1. mainwindow::mainwindow(QWidget *parent)
    2. : QMainWindow(parent), ui(new Ui::mainwindowClass)
    3. {
    4. //This sets up the UI
    5. ui->setupUi(this);
    6. model = new QSqlTableModel(this);
    7. //This pretty much says that the database will be of SQLite type, and opens it up from a given path
    8. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    9. db.setDatabaseName("/Users/k12yp70n/Desktop/test");
    10.  
    11. //This "if" shows up a messagebox in case the program cannot load a given database
    12. if (!db.open()) {
    13. QMessageBox::warning(0, qApp->tr("Cannot open database"),
    14. qApp->tr("Unable to establish a database connection.\n"
    15. "This program requires a database to operate. Please contact "
    16. "the developer of this software for more information on "
    17. "how to solve this problem.\n\n"), QMessageBox::Ok);
    18.  
    19. }
    20.  
    21. //Note: "medadmin" is the name of the table in the SQLite3 database
    22. model->setTable ("medadmin");
    23. model->select();
    24.  
    25. //The following lines edit a couple of parameters in the table view "View" and bind it to the model
    26. ui->View->setWindowTitle ("Patients List");
    27. ui->View->setModel (model);
    28. ui->View->setSelectionBehavior (QAbstractItemView::SelectRows);
    29. ui->View->resizeColumnsToContents();
    30. ui->View->setSelectionMode (QTableView::SingleSelection);
    31. ui->View->setVisible (true);
    32. ui->View->setColumnHidden(0, true);
    33. ui->View->showGrid();
    34. setting_up_toolbar_menus();
    35. }
    To copy to clipboard, switch view to plain text mode 

    The problem is the view simply shows up blank! Since I do not get any errors related to the database, I am assuming that the problem lies either on the model, on the view, or both.

    But I have no idea on how to solve it,

    Can anyone help me please?

    Any help with be largely appreciated (some code samples would also be nice by the way)

    Thanks in advance

  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: Model Or View problem

    Where did you set the database to the model?

    try:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName("/Users/k12yp70n/Desktop/test"); // is "test" a database or a path?
    3. db.open();
    4. model = new QSqlTableModel(this, db);
    To copy to clipboard, switch view to plain text mode 

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

    Nefastious (9th October 2009)

  4. #3
    Join Date
    Sep 2009
    Posts
    34
    Thanks
    28
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Model Or View problem

    Ow lawd... I never thought it would be so simple to solve... Thank you very much...

Similar Threads

  1. Replies: 2
    Last Post: 10th August 2009, 09:45
  2. Problem with list View
    By yuvaraj.yadav in forum Qt Programming
    Replies: 9
    Last Post: 29th April 2009, 14:02
  3. A few queries about Model View Programming
    By montylee in forum Qt Programming
    Replies: 46
    Last Post: 2nd March 2009, 08:36
  4. Replies: 1
    Last Post: 14th January 2009, 10:10
  5. View update problem
    By prakash in forum Qt Programming
    Replies: 6
    Last Post: 17th March 2006, 10:13

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.