PDA

View Full Version : QT4 : need database/form/window sample



marvaneke
18th July 2006, 12:17
Hi,

In front of the complexity to start with QT4, I want to build an application managing a database, with one form/window for one table, managing also the relation between table.

I know that I have to use QSqlTableModel, QSqlRelationalTableModel, etc explain in the page http://doc.trolltech.com/4.1/database.html.

I have read the tutorial from trolltech, digital fanatics, etc, but I have still the difficulty to START. :-(

Can someone give me more concrete simple sample of already exist applications, tutorial, or whatelse ?

Regards.

wysota
18th July 2006, 12:23
Probably the simplest example is something like this:



#include <QApplication>
#include <QSqlTableModel>
#include <QTableView>
#include <QSqlDatabase>

int main(int argc, char **argv){
QApplication app(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
db.setHostName("acidalia");
db.setDatabaseName("customdb");
db.setUserName("mojito");
db.setPassword("J0a1m8");
bool ok = db.open();
if(!ok) return 2;
QSqlTableModel model;
model.setTable("sometable");
model.setEditStrategy(QSqlTableModel::OnRowChange) ;
QTableView tv;
tv.setModel(&model);
tv.show();
return app.exec();
}

marvaneke
18th July 2006, 12:37
Thanks for the quick answer. :)

It is very interesting and I will try it. :cool:

Suppose I have a database with the table person and the table address, linked each other by the id of the person. Meaning that a person can have multiple address (one for the women and one for the mistress :) ).

Can you give me the code with the main.cpp, person.ui, person.cpp, person.h, adress.ui, adress.cpp, address.h.
I apologize if it is a too long sample to give. Maybe only part of this files is necessary to start ? :o
I suppose I have to use the "The Multiple Inheritance Approach" for the chain between the form/window, which is explain on :
http://doc.trolltech.com/4.1/designer-using-a-component.html#the-multiple-inheritance-approach

Regards.

wysota
18th July 2006, 13:04
Here you go... without ui files, as I didn't know what to put in them. I don't know if this is what you wanted but it is something to start with...

marvaneke
18th July 2006, 20:32
Hi wysota,

Once again thanks for the quick answer (and for the mistress tarball, very funny). :)

I will start with these two samples.

I realize that my question was not precise enough. :(
If I don't find a solution, I will come back with a more detailled little business case.

Regards.