PDA

View Full Version : Strange problem with mysql and QTableView.



Datakim
23rd July 2011, 22:44
Hi everyone, a newbie at qt requesting a bit of assistance here.

My problem is that I am trying to connect to a mysql database, and then load data from a table into a QTableView. Unfortunately, I have run into some difficulties.

I have done all the necessary mysql bits such as compiling a plugin and such, and I believe I did things correctly since I have managed to create a connection. My problem is that while the code works in one specific circumstance, it does not in another.

My test program is composed of two files, main.cpp and mainwindow.cpp. I am using qtcreator to make this program. I have looked up on how to load data into QTableView and if I do the loading in the main.cpp, it works right. I get a window with the correct data.

However, if I try to do the EXACT same thing with the exact same code from mainwindow.cpp, it fails to work at all. I get no errors or such, the data simply fails to load.

Here is a sample of the code I am using to make a connection that works:

main.cpp:


#include <QtGui/QApplication>
#include "mainwindow.h"

void initializeModel(QSqlTableModel *model)
{
model->setTable("TABLE");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
}


QTableView *createView(const QString &title, QSqlTableModel *model)
{
QTableView *view = new QTableView;
view->setModel(model);
view->setWindowTitle(title);
return view;
}


int main(int argc, char *argv[])
{
QApplication a(argc, argv);


QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("HOSTNAME");
db.setDatabaseName("DATABASENAME");
db.setUserName("USERNAME");
db.setPassword("PASSWORD");

if (!db.open()) {
qDebug() << db.lastError();
return 1;
}

QSqlTableModel model;

initializeModel(&model);

QTableView *view1 = createView(QObject::tr("Table Model"), &model);
view1->show();

// MainWindow w;
// w.show();


return a.exec();
}


The code above works. However, if I comment the sql code here, and remove the comments that open the MainWindow, and put the same code there, things no longer work.

Mainwindow.cpp


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

#include <QtGui>
#include <QtSql>


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

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

void initializeModel(QSqlTableModel *model)
{
model->setTable("TABLE");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
}

QTableView *createView(const QString &title, QSqlTableModel *model)
{
QTableView *view = new QTableView;
view->setModel(model);
view->setWindowTitle(title);
return view;
}


void MainWindow::on_pushButton_clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("HOSTNAME");
db.setDatabaseName("DATABASENAME");
db.setUserName("USERNAME");
db.setPassword("PASSWORD");


if (!db.open()) {
qDebug() << db.lastError();
}

QSqlTableModel model;

initializeModel(&model);

QTableView *view1 = createView(QObject::tr("Table Model"), &model);
view1->show();
}



The code is exactly the same, except it runs from a button click rather than automatically. The information (hostname,databasename,etc are correct).
I have no idea whats wrong. :( Any advice or ideas would be much appreciated.
Thanks

ChrisW67
23rd July 2011, 23:41
The QSqlTableModel you create at line 49 is on the stack, goes out of scope at the end of that routine, and leaves the view without a model to display. Create it on the heap.

It works in the first example because the model stays in scope long fro the entire lifetime of the view.

CroOm
26th July 2011, 10:30
in other words, add the following line to mainwindow.h

QTableView *view1;
QSqlTableModel model;

and assign them in mainwindow.cpp just with the names view1 and model