If you need a read-write model that operates on an arbitrary result set, you can subclass QSqlQueryModel and reimplement flags() and setData() to make it read-write. QSqlTableModel is optimized to operate on a single table at a time. See Model/View Programming for more information. You can create your own delegates by subclassing QAbstractItemDelegate or QItemDelegate. The delegate is also responsible for providing editor widgets (e.g., a combobox) when the user starts editing an item in the view. The default delegate, QItemDelegate, handles the most common data types ( int, QString, QImage, etc.). The items in the view are rendered using a delegate. Likewise, if you remove rows using removeRows(), the rows will be marked with an exclamation mark (!) until the change is submitted. If you insert rows programmatically using QSqlTableModel::insertRows(), the new rows will be marked with an asterisk (*) until they are submitted using submitAll() or automatically when the user moves to another record (assuming the edit strategy is QSqlTableModel::OnRowChange). QTableView also has a vertical header on the left with numbers identifying the rows. Model - >setHeaderData( 3, Qt ::Horizontal, QObject ::tr( "Country")) Model - >setHeaderData( 2, Qt ::Horizontal, QObject ::tr( "City")) Model - >setHeaderData( 1, Qt ::Horizontal, QObject ::tr( "Name")) For example: model - >setHeaderData( 0, Qt ::Horizontal, QObject ::tr( "ID")) The header's labels default to the table's field names. To change the header texts, call setHeaderData() on the model. View classes display a header at the top to label the columns. The Table Model example shows how it works. If the user edits the model through one of the views, the other views will reflect the changes immediately. You can use the same model as a data source for multiple views. You can disable this by calling view - >setEditTriggers( QAbstractItemView ::NoEditTriggers) If the model is a read-write model (e.g., QSqlTableModel), the view lets the user edit the fields. The following example creates a view based on an SQL data model: QTableView *view = new QTableView In practice, QTableView is by far the most common choice, because an SQL result set is essentially a two-dimensional data structure. The QSqlQueryModel, QSqlTableModel, and QSqlRelationalTableModel classes can be used as a data source for Qt's view classes such as QListView, QTableView, and QTreeView.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |