PDA

View Full Version : Model Or View problem



Nefastious
9th October 2009, 18:50
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:


mainwindow::mainwindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::mainwindowClass)
{
//This sets up the UI
ui->setupUi(this);
model = new QSqlTableModel(this);
//This pretty much says that the database will be of SQLite type, and opens it up from a given path
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("/Users/k12yp70n/Desktop/test");

//This "if" shows up a messagebox in case the program cannot load a given database
if (!db.open()) {
QMessageBox::warning(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
"This program requires a database to operate. Please contact "
"the developer of this software for more information on "
"how to solve this problem.\n\n"), QMessageBox::Ok);

}

//Note: "medadmin" is the name of the table in the SQLite3 database
model->setTable ("medadmin");
model->select();

//The following lines edit a couple of parameters in the table view "View" and bind it to the model
ui->View->setWindowTitle ("Patients List");
ui->View->setModel (model);
ui->View->setSelectionBehavior (QAbstractItemView::SelectRows);
ui->View->resizeColumnsToContents();
ui->View->setSelectionMode (QTableView::SingleSelection);
ui->View->setVisible (true);
ui->View->setColumnHidden(0, true);
ui->View->showGrid();
setting_up_toolbar_menus();
}

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

Lykurg
9th October 2009, 19:31
Where did you set the database to the model?

try:

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("/Users/k12yp70n/Desktop/test"); // is "test" a database or a path?
db.open();
model = new QSqlTableModel(this, db);

Nefastious
9th October 2009, 19:44
Ow lawd... I never thought it would be so simple to solve... Thank you very much...:)