kosa
20th June 2009, 16:08
I have a form with 2 PuchButtons and a TableView created using QTCreator. Now I want to populate TableView with data from SQL database using QSqlTableModel.
My question is: Why when i allocate QSqlTableModel in PushButton1Click everything is ok, and when I allacate it in MainWindow constructor the data from database doesn't show on TableView.
(First I click button 1 to open database, then I click button 2 to fetch data)
Here is declaration of mainwindow class:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QSqlTableModel* m;
private slots:
bool PushButton1Click();
bool PushButton2Click();
};
Here is code which works:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
// m = new QSqlTableModel(this); // NOT OK !!!!!!!!
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::PushButton1Click()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("localhost");
db.setDatabaseName("axis");
db.setUserName("root");
db.setPassword("kosa1980");
bool ret = db.open();
m = new QSqlTableModel(this); // OK !!!!!!!!
return ret;
}
bool MainWindow::PushButton2Click()
{
m->setTable("wagi");
m->setEditStrategy(QSqlTableModel::OnManualSubmit);
m->select();
m->removeColumn(0);
m->setHeaderData(0, Qt::Horizontal, QObject::tr("numer_seryjny"));
m->setHeaderData(1, Qt::Horizontal, QObject::tr("typ"));
ui->tableView->setModel(m);
return true;
}
Here is code which doesn't work:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
m = new QSqlTableModel(this); // NOT OK !!!!!!!!
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::PushButton1Click()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("localhost");
db.setDatabaseName("axis");
db.setUserName("root");
db.setPassword("kosa1980");
bool ret = db.open();
//m = new QSqlTableModel(this); // OK !!!!!!!!
return ret;
}
bool MainWindow::PushButton2Click()
{
m->setTable("wagi");
m->setEditStrategy(QSqlTableModel::OnManualSubmit);
m->select();
m->removeColumn(0);
m->setHeaderData(0, Qt::Horizontal, QObject::tr("numer_seryjny"));
m->setHeaderData(1, Qt::Horizontal, QObject::tr("typ"));
ui->tableView->setModel(m);
return true;
}
Why second version doesn't work?
My question is: Why when i allocate QSqlTableModel in PushButton1Click everything is ok, and when I allacate it in MainWindow constructor the data from database doesn't show on TableView.
(First I click button 1 to open database, then I click button 2 to fetch data)
Here is declaration of mainwindow class:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QSqlTableModel* m;
private slots:
bool PushButton1Click();
bool PushButton2Click();
};
Here is code which works:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
// m = new QSqlTableModel(this); // NOT OK !!!!!!!!
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::PushButton1Click()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("localhost");
db.setDatabaseName("axis");
db.setUserName("root");
db.setPassword("kosa1980");
bool ret = db.open();
m = new QSqlTableModel(this); // OK !!!!!!!!
return ret;
}
bool MainWindow::PushButton2Click()
{
m->setTable("wagi");
m->setEditStrategy(QSqlTableModel::OnManualSubmit);
m->select();
m->removeColumn(0);
m->setHeaderData(0, Qt::Horizontal, QObject::tr("numer_seryjny"));
m->setHeaderData(1, Qt::Horizontal, QObject::tr("typ"));
ui->tableView->setModel(m);
return true;
}
Here is code which doesn't work:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
m = new QSqlTableModel(this); // NOT OK !!!!!!!!
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::PushButton1Click()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("localhost");
db.setDatabaseName("axis");
db.setUserName("root");
db.setPassword("kosa1980");
bool ret = db.open();
//m = new QSqlTableModel(this); // OK !!!!!!!!
return ret;
}
bool MainWindow::PushButton2Click()
{
m->setTable("wagi");
m->setEditStrategy(QSqlTableModel::OnManualSubmit);
m->select();
m->removeColumn(0);
m->setHeaderData(0, Qt::Horizontal, QObject::tr("numer_seryjny"));
m->setHeaderData(1, Qt::Horizontal, QObject::tr("typ"));
ui->tableView->setModel(m);
return true;
}
Why second version doesn't work?