I'm just started with the Qt's Model/View framework and I try to find the best solution to a problem that seems commun and probably very simple.
I have an Employee
table (Id, LastName, FirstName, Salary)
and I would like to display two different views of the same table:
- The first view: only a QListView
including the concatenation of the first and last names. This view should not be editable but only available to select one employee at a time.
- The 2nd view: use 3x QLineEdit
for the name, the first name and the salary. These three fields are used to change the content of the database.
I managed to correctly display the [Name, FirstName]
inside a QListView
by using a QSqlQueryModel
.
QSqlQueryModel* modelSQL = new QSqlQueryModel(this);
modelQuery->setQuery("SELECT CONCAT(LastName, ', ', FirstName) AS FullName, Id FROM Employee ORDER BY FullName ASC;");
empListView = new QListView;
empListView->setModel(modelSQL);
I managed to use QSqlTableModel
and QDataWidgetMapper
to display all three fields in QLineEdit
.
QSqlTableModel *model = new QSqlTableModel(this, this->db);
model->setTable("Employee");
model->select();
mapper = new QDataWidgetMapper(this);
mapper->setModel(modelSQL);
mapper->addMapping(this->lastNameEdit, 2);
mapper->addMapping(this->firstNameEdit, 3);
mapper->addMapping(this->salaryEdit, 4);
My question, what is the best strategy for the model to be updated from the modelQuery
. I mean, when I click the QListView
and selects, for example, the third employee; how to synchronize the three other fields accordingly?
Many thanks!