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
){
//This sets up the UI
ui->setupUi(this);
//This pretty much says that the database will be of SQLite type, and opens it up from a given path
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 "
}
//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->resizeColumnsToContents();
ui
->View
->setSelectionMode
(QTableView::SingleSelection);
ui->View->setVisible (true);
ui->View->setColumnHidden(0, true);
ui->View->showGrid();
setting_up_toolbar_menus();
}
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();
}
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
Bookmarks