Strange problem with mysql and QTableView.
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:
Code:
#include <QtGui/QApplication>
#include "mainwindow.h"
{
model->setTable("TABLE");
model->select();
}
{
view->setModel(model);
view->setWindowTitle(title);
return view;
}
int main(int argc, char *argv[])
{
db.setHostName("HOSTNAME");
db.setDatabaseName("DATABASENAME");
db.setUserName("USERNAME");
db.setPassword("PASSWORD");
if (!db.open()) {
qDebug() << db.lastError();
return 1;
}
initializeModel(&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
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>
#include <QtSql>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
{
model->setTable("TABLE");
model->select();
}
{
view->setModel(model);
view->setWindowTitle(title);
return view;
}
void MainWindow::on_pushButton_clicked()
{
db.setHostName("HOSTNAME");
db.setDatabaseName("DATABASENAME");
db.setUserName("USERNAME");
db.setPassword("PASSWORD");
if (!db.open()) {
qDebug() << db.lastError();
}
initializeModel(&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
Re: Strange problem with mysql and QTableView.
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.
Re: Strange problem with mysql and QTableView.
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