PDA

View Full Version : QSqlQueryModel and QTableView question



wbt_ph
20th September 2006, 07:05
Hello.
I am a newbie.

here is part of my code:

trans.h

#ifndef TRANS_H
#define TRANS_H

#include <QtSql>
#include "ui_fleTrans.h"

class trans : public QWidget, private Ui::frmTrans
{
Q_OBJECT
public:
trans(QWidget *parent = 0);

public slots:
...
void showTable(QSqlQueryModel *model);
void initializeModel(QSqlQueryModel *model);
...

private:
QSqlQueryModel *model;
...
};
#endif

trans.cpp

#include <QtGui>
#include <QtSql>
#include "trans.h"
#include "custsqlmodel.h" //basically the same as customsqlmodel in sql/querymodel
//example

trans::trans(QWidget *parent)
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("databasename");
db.setUserName("username");
db.setPassword("password");
if (!db.open())
{
QMessageBox::critical(0, "Database Error", db.lastError().text());
}

setupUi(this);

model = new QSqlQueryModel;

CustSqlModel custSqlModel;
initializeModel(&custSqlModel);
showTable(&custSqlModel);
}

void trans::initializeModel(QSqlQueryModel *model)
{
model->setQuery("select * from table");
}

void trans::showTableLedger(QSqlQueryModel *model)
{
QTableView *view = new QTableView;
view->setModel(model);
view->show();
}

main.cpp

#include <QApplication>
#include "trans.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
trans *dialog = new trans;
dialog->show();
return app.exec();
}

nothing wrong with my qsqlquerysublcass since it compiles without error and shows the data in the table using the sql/querymodel example.

The above code simply shows a window frame with scroll bars, no grid, no column header, no data - just white space.

Please help.:confused:

Thank you in advance.

wil

wysota
20th September 2006, 09:24
You have to call select() on the model for it to fetch data from the database.

wbt_ph
20th September 2006, 11:44
You have to call select() on the model for it to fetch data from the database.

Thank you for your reply but i must apologize since I am new to qt, i do not know how to implement a select() on the model.

here is my model:

custSqlModel.h

#ifndef CUSTSQLMODEL_H
#define CUSTSQLMODEL_H
#include <QSqlQueryModel>

class CustSqlModel : public QSqlQueryModel
{
Q_OBJECT
public:
CustSqlModel(QObject *parent = 0);
QVariant data(const QModelIndex &index, int role) const;
};
#endif /*LEDGERMODEL_H*/

custSqlModel.cpp


#include <QtGui>
#include "ledgermodel.h"

CustSqlModel::CustSqlModel(QObject *parent)
: QSqlQueryModel(parent)
{
}

QVariant CustSqlModel::data(const QModelIndex &index, int role) const
{
QVariant value = QSqlQueryModel::data(index, role);
if (value.isValid() && role == Qt::DisplayRole)
{
if (index.column() == 0)
return value.toString();
else if (index.column() == 1)
return QString("%L1").arg(value.toDouble(), 0, 'f', 2);
}
else if (role == Qt::TextAlignmentRole && index.column() == 2)
{
return int(Qt::AlignRight | Qt::AlignVCenter);
}

return value;
}

there seems to be no select() member in QSqlQueryModel.

Please help with code snippet or at least a hint on what to do.

Thank you for your patience.

wil

wysota
20th September 2006, 14:33
You don't implement it. I didn't notice you were using QSqlQueryModel, I somehow assumed it was QSqlTableModel.

I see you have a problem in your code. You create the model (custSqlModel) on stack and it gets deleted when the constructor returns. On the other hand you have another model there (model), which looks like is not used at all...

Try this:

trans::trans(QWidget *parent){
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("databasename");
db.setUserName("username");
db.setPassword("password");
if (!db.open())
{
QMessageBox::critical(0, "Database Error", db.lastError().text());
}
model = new CustSqlModel(this);
model->setQuery("select * from table");
view = new QTableView(this);
view->setModel(model);
}

wbt_ph
20th September 2006, 15:40
You're the man!!!

Code works now!!!

Thank you so much!!!