[Qt-interest] Problem displaying records from db in qt application

Eulalie Gey van Pittius geytjie at yahoo.com
Thu Sep 22 13:16:01 CEST 2011


I have adjusted the 'tables' example that comes with Qt 4.7.4, which uses 
sqlite. In the example, however, the data is hard-coded into the app, 
which is of course of no use if you need to build an application that 
can connect and update the application's db regularly.
I must be doing something wrong, but I just don't see where.


My code in .cpp file: 


#include <QtGui>
#include <QtSql>

#include "myCon.h"
 #include <QSqlQuery>

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

    model->setHeaderData(0, Qt::Horizontal,
 QObject::tr("id"));
    model->setHeaderData(1, Qt::Horizontal, QObject::tr("name"));
    model->setHeaderData(2, Qt::Horizontal, QObject::tr("winemaker_id"));
}

QTableView *createView(QSqlTableModel *model, const QString &title = "")
{
    QTableView *view = new QTableView;
    view->setModel(model);
#if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5) || defined(Q_WS_SIMULATOR)
    Q_UNUSED(title)
#else
    view->setWindowTitle(title);
#endif
    return view;
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    if (!createConnection())
        return 1;

    QSqlTableModel model;

    initializeModel(&model);

#if
 defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5) || defined(Q_WS_SIMULATOR)
    QTabWidget *tabWidget = new QTabWidget;
    tabWidget->addTab(createView(&model), "View 1");
    tabWidget->addTab(createView(&model), "View 2");

    tabWidget->showMaximized();
#else
    QTableView *view1 = createView(&model, QObject::tr("Table Model (View 1)"));
    QTableView *view2 = createView(&model, QObject::tr("Table Model (View 2)"));

    view1->show();
    view2->move(view1->x() + view1->width() + 20, view1->y());
    view2->show();
#endif

    return app.exec();
}

and the header file:

#ifndef CONNECTION_H
#define CONNECTION_H

#include <QMessageBox>
#include <QSqlDatabase>
#include
 <QSqlError>
#include <QSqlQuery>

/*
    This file defines a helper function to open a connection to an
    in-memory SQLITE database and to create a test table.

    If you want to use another database, simply modify the code
    below. All the examples in this directory use this function to
    connect to a database.
*/
//! [0]
static bool createConnection()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
     db.setDatabaseName("wyne.sqlite");
        if (!db.open()) {
        QMessageBox::critical(0, qApp->tr("Cannot open database"),
            qApp->tr("Unable to establish a database
 connection.\n"
                     "This example needs SQLite support. Please read "
                     "the Qt SQL driver documentation for information how "
                     "to build it.\n\n"
                     "Click Cancel to exit."), QMessageBox::Cancel);
        return false;
    }

    QSqlQuery query;
    query.exec("select id, name, winemaker_id FROM wines"&#41;;



    return true;
}
//!
 [0]

#endif


and the .pro file:

HEADERS = myCon.h
SOURCES = myTablemodel.cpp
QT += sql
#install
target.path = $$[QT_INSTALL_EXAMPLES]/sql/myTablemodel
sources.files = $$SOURCES * .h $$RESOURCES $$FORMS myTablemodel.pro
INSTALLS += target sources
 
symbian: include($$PWD/../../symbianpkgrules.pri)
maemo5: include($$PWD/../../maemo5pkgrules.pri)
 
OTHER_FILES += \
qtc_packaging/debian_harmattan/rules \
qtc_packaging/debian_harmattan/README\
qtc_packaging/debian_harmattan/control\
qtc_packaging/debian_harmattan/compat \
qtc_packaging/debian_harmattan/changelog \
QMAKE_MAC_SDK = /Developer/SDKs/MacOSX10.6.sdk


I
 would appreciate some help on this. From the very deepest bottom of my 
hart - still have a lot of work to do on this app and the deadline is 
next week.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110922/981a15c7/attachment.html 


More information about the Qt-interest-old mailing list