QtablewidgetInherited fromQtableview. Qsqltablemodel can workQtableviewBIND, but cannotQtablewidgetBIND.
Qsqltablemodel * model = new qsqltablemodel;
Model-> settable ("employee ");
Model-> seteditstrategy (qsqltablemodel: onmanualsubmit );
Model-> select ();
Model-> removecolumn (0); // don't show the ID
Model-> setheaderdata (0, QT: horizontal, TR ("name "));
Model-> setheaderdata (1, QT: horizontal, TR ("salary "));
Qtableview* View = newQtableview;
View-> setmodel (model );
View-> show (); |
Model View Structure
The model/view framework is introduced in qt4 to separate data and performance. This is called the interview framework in qt4.
The MVC design pattern is a design pattern originated from Smalltalk related to the user interface. MVC consists of three elements: Model (model), view (view), which is the user interface, and controler defines the user's operations on the interface. By using the MVC mode, data and user interfaces are effectively isolated, making the design more flexible and adaptable to changes.
Model: All models are based on the qiniactitemmodel class, which is an abstract base class.
View: all views are inherited from the abstract base class q1_actitemview.
The interview Framework provides some common model classes and view classes, such as qstandarditemmodel, qdirmodel, qstringlistmodel, qcolumnview, qheaderview, qlistview,Qtableview, Qtreeview.
Compared with existing models and views, QT also provides more convenient classes to process common data models. They integrate models and views to facilitate the processing of some common data types. Although these types are simple and convenient to use, they also lose the flexibility of the model view structure. Therefore, you should select them based on the actual situation.
Differences
|
Differences |
Qtableview |
Qtablewidget |
| Inheritance relationship |
|
QtablewidgetInherited fromQtableview |
| Use the data model setmodel |
You can use setmodel to set the data model. |
Setmodel is a private function. It is not difficult to use this function to set the data model. |
| Show check box setcheckstate |
No function implementation check box |
Setcheckstate (QT: checked) in the qtablewidgetitem class; check boxes can be set. |
| Bind with qsqltablemodel |
QtableviewCan be bound to qsqltablemodel |
QtablewidgetCan be bound to qsqltablemodel |
| |
|
|
| |
|
|
Considerations for binding view and Model
However, when a view is bound to a model, the model must be created using new. Otherwise, the view cannot change as the model changes.
| Incorrect eorror syntax |
|
Qstandarditemmodel model (4, 2 );
Model. setheaderdata (0, QT: horizontal, TR ("label "));
Model. setheaderdata (1, QT: horizontal, TR ("quantity "));
Ui. tableview-> setmodel (& model );
For (int row = 0; row <4; ++ row ){
For (INT column = 0; column <2; ++ column ){
Qmodelindex Index = model. Index (row, column, qmodelindex ());
Model. setdata (index, qvariant (row + 1) * (column + 1 )));
}
}
|
| Right |
|
Qstandarditemmodel * model;
Model = new qstandarditemmodel (4, 2 );
Ui. tableview-> setmodel (model );
Model-> setheaderdata (0, QT: horizontal, TR ("label "));
Model-> setheaderdata (1, QT: horizontal, TR ("quantity "));
For (int row = 0; row <4; ++ row ){
For (INT column = 0; column <2; ++ column ){
Qmodelindex Index = model-> index (row, column, qmodelindex ());
Model-> setdata (index, qvariant (row + 1) * (column + 1 )));
}
}
|