Reputation: 25
I have a QTabWidget, each tab contains a QTableView. The tableviews use QTableModels which reads data from an sqlite database.
The models are kept as member variables, so I don't think it's a scope problem (which is the most frequent problem I've found by searching for the issue).
Other db operations works: I tried populating / creating tables in the code, and those operation are correctly executed.
The following is the class definition:
#include <QMainWindow>
#include <QtSql>
#include <QString>
namespace Ui {
class MainWindow;
}
class AlisnagMainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit AlisnagMainWindow(QWidget *parent = 0);
~AlisnagMainWindow();
protected:
bool createDB(const QString& filename);
bool loadDB(const QString& filename);
protected slots:
void newFile();
void openFile();
void addRow();
private:
void initModels();
Ui::MainWindow *ui;
QSqlDatabase db;
QSqlRelationalTableModel *loans_model;
QSqlTableModel *people_model;
QSqlRelationalTableModel *items_model;
};
This is the constructor:
AlisnagMainWindow::AlisnagMainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
loans_model( new QSqlRelationalTableModel(this) ),
people_model( new QSqlTableModel(this) ),
items_model( new QSqlRelationalTableModel(this) )
{
ui->setupUi(this);
db = QSqlDatabase::addDatabase("QSQLITE");
connect(ui->actionNew, SIGNAL(triggered()), this, SLOT(newFile()));
connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(openFile()));
}
This is how I initialized the models and the views:
void AlisnagMainWindow::initModels(){
people_model->setTable("people");
people_model->setEditStrategy(QSqlTableModel::OnRowChange);
people_model->select();
people_model->setHeaderData(0, Qt::Horizontal, tr("Name"));
people_model->setHeaderData(1, Qt::Horizontal, tr("Surname"));
people_model->setHeaderData(2, Qt::Horizontal, tr("Phone"));
people_model->setHeaderData(3, Qt::Horizontal, tr("Address"));
items_model->setTable("items");
items_model->setEditStrategy(QSqlTableModel::OnRowChange);
items_model->setRelation(3, QSqlRelation("categories","id","name"));
items_model->select();
loans_model->setTable("loans");
loans_model->setEditStrategy(QSqlTableModel::OnRowChange);
loans_model->setRelation(0, QSqlRelation("items","id","name"));
loans_model->setRelation(1, QSqlRelation("people","id","name"));
loans_model->select();
ui->peopleView->setModel(people_model);
ui->peopleView->hideColumn(0);
ui->itemsView->setModel(items_model);
ui->itemsView->hideColumn(0);
ui->loansView->setModel(loans_model);
ui->loansView->hideColumn(0);
}
The database is loaded as follows:
bool AlisnagMainWindow::loadDB(const QString &filename)
{
if(db.isOpen())
db.close();
db.setDatabaseName(filename);
if( !db.open() ){
QMessageBox::critical(this, tr("Error"),
tr("Could not open db %1").arg(filename),
QMessageBox::Ok);
return false;
}
ui->statusBar->showMessage("DB successfully opened!");
return true;
}
Then this is how is all combined:
void AlisnagMainWindow::openFile()
{
QString filename = QFileDialog::getOpenFileName(this, tr("Choose DB file"),
".", "*.db");
loadDB(filename);
initModels();
}
The file I'm opening has been populated externally. When I run the code, neither the data nor the headers are shown. Any insight?
Thanks in advance.
EDIT: Added some context as requested
Upvotes: 1
Views: 510
Reputation: 25
As Greenflow pointed out, problem was in the constructor, in particular in the order of initialization.
The constructor is modified as follows
AlisnagMainWindow::AlisnagMainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
db ( QSqlDatabase::addDatabase("QSQLITE")),
loans_model( new QSqlRelationalTableModel(this, db) ),
people_model( new QSqlTableModel(this, db) ),
items_model( new QSqlRelationalTableModel(this, db) )
{
ui->setupUi(this);
connect(ui->actionNew, SIGNAL(triggered()), this, SLOT(newFile()));
connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(openFile()));
}
Upvotes: 1