PDA

View Full Version : QSqlQueryModel, tableView, SQLite problem



ohmyqt
28th September 2013, 10:22
Hello, I have a problem that I've spend the last 2 hours searching for answers but nothing seems to work. So, here goes...

I just created a new Gui application, and inserted a TableView in the MainWindow using the Designer. Here's the changes I made to the default code:

mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

// Changes done here
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("DB.sqlite");
db.open();

model = new QSqlQueryModel();
model->setQuery("select * from accounts", db);

ui->tableView->setModel(model);
}

MainWindow::~MainWindow()
{
delete ui;
}


mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

// added includes
#include <QtSql>
#include <QtGui>
#include <QtCore>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;

// Changes here
QSqlQueryModel *model;
QSqlDatabase db;
};

#endif // MAINWINDOW_H


My question is, how come nothing shows up in the tableview? Other programs such as python can access the data and show the columns, but this comes out blank? There are no error messages.

Help?

Added after 15 minutes:

Umm... nevermind. Solved it.