PDA

View Full Version : Writing Data Aware Forms in QT4.



gsQT4
23rd May 2006, 15:09
I posted this question in the QTForums.org, but no one seems to have a suggestion.

In QT 3.2, I wrote a simple client application to view, update and insert service tickets into a database. I wanted the data displayed in a form and not a table, so I used the QSqlForm class. There are several QSqlForm examples shown in QT3 books and the online doc. QT 4.1 no longer uses QSqlForm. While the class is still available, QT 4.1 uses the model view concept for displaying data. There are a few model/view convenience classes, but those are table and list views.

I've re-written the app I had written in 3.2 to work with QT4. Instead of using QSqlForm, I have a function that populates all the fields with the data from a query's current record. I use a similar function to insert and update data records. But, I'm not sure this is the best approach for writing a client application. How does one write a form style application in QT4 using the model view architecture? The model view design interests me, because it appears to allow one to easily write a 3-tier client/server application in QT. If my understanding of the model/view is incorrect, please let me know.

Thanks,
Glenn

wysota
23rd May 2006, 16:18
I posted this question in the QTForums.org, but no one seems to have a suggestion.
QtForum is practically dead.


In QT 3.2, I wrote a simple client application to view, update and insert service tickets into a database. I wanted the data displayed in a form and not a table, so I used the QSqlForm class. There are several QSqlForm examples shown in QT3 books and the online doc. QT 4.1 no longer uses QSqlForm. While the class is still available, QT 4.1 uses the model view concept for displaying data. There are a few model/view convenience classes, but those are table and list views.

I've re-written the app I had written in 3.2 to work with QT4. Instead of using QSqlForm, I have a function that populates all the fields with the data from a query's current record. I use a similar function to insert and update data records. But, I'm not sure this is the best approach for writing a client application. How does one write a form style application in QT4 using the model view architecture? The model view design interests me, because it appears to allow one to easily write a 3-tier client/server application in QT. If my understanding of the model/view is incorrect, please let me know.

First step is to define your model properly. I assume you know (or will find a way) how to do it. The second step is to connect the model to your widgets. Unfortunately Qt4 itself doesn't provide an easy way to do this with all the widgets, but signals and slots are your friends here. In my opinion you should implement a QObject subclass which would serve as a controller between the model and widgets handling different fields. The controller has to do two things:
1. Make sure widgets always display up to date data
2. Commit changes made by the widgets to the model

For the first step, model signals like dataChanged(), layoutChanged() and reset() will be usefull.
For the second, you should connect signals emitted when the data changes from the widgets to your controller.

The last thing to do is to provide an architecture for connecting and disconnecting widgets from the controller.

I started to implement such a solution myself, but currently I don't have time to finish my controller architecture, but it's no big deal.

gsQT4
23rd May 2006, 20:03
Thanks for the suggestions wysota. I'll see if I can figure out how to make it work.

It sounds like this approach has the application displaying data real time, instead of working from the last query to the database. Is the model/view concept the trend for database client applications?

wysota
23rd May 2006, 20:24
Thanks for the suggestions wysota. I'll see if I can figure out how to make it work.
Sure you can.


It sounds like this approach has the application displaying data real time, instead of working from the last query to the database.
Yes.


Is the model/view concept the trend for database client applications?
The model/view concept doesn't assume any specific backend for storing data. It works with both dynamic and static data.

zlatko
21st March 2007, 09:07
Hi all !
So i also have this problem :)
Wysota is your approach correct today(becouse i see last post in thread near year old) ? Or maiby community find something else ? :rolleyes:
Thx...

wysota
21st March 2007, 10:35
QDataWidgetMapper is the proper solution since Qt 4.2 (it does exactly what I wanted it to).