Reputation: 45
There is a client (company). The client has contacts. You can link any number of contacts to a specific client.
Let's say you open the client you need. Then you select the contacts you need from the ComboBox, they are attached to this client.
Now the implementation is as follows, three tables in the database - contacts, clients and assignment, the third contains the contact id and the company id - everything is simple. There is also a QSqlQueryModel assignment_contacts model - I inserted it into the ComboBox. Her code:
QSqlQuery query;
query.prepare("SELECT id, fullName FROM contacts WHERE contacts.rowid NOT IN (SELECT contactId FROM assignment WHERE companyId = :nextId);");
query.bindValue(":nextId", nextId);
query.exec();
assignment_contacts->setQuery(std::move(query));
Roughly speaking, this model stores those contacts that are not yet tied to the selected company. The added contacts are displayed on the screen under the combo. Now, by clicking on the combobox element, a connection is added to the database and the assignment_contacts model is subsequently updated. That is, everything works properly, but I do not like the fact that it is added to the database before pressing the "save" button:
This confuses managers, they added contacts, clicked on cancel - and the contacts are already in the database. Plus, if another manager opens the same company, she will have these linked contacts, although the first manager has not clicked "save" yet. In general, I decided to implement a similar thing, only WITHOUT adding to the database, but storing the added id's in some list, and when you press the save button, add the links to the database. I started doing it, everything goes according to plan, but I can’t implement the logic with updating the model :-)
That is, you need to click on the name of the contact in the combo, and write the id of this contact to the list, with the subsequent removal of this entry from the model. I got the idea to make a QSortFilterProxyModel. I did it, put my original QSqlQueryModel in ->sourceModel, tried to delete rows from it - unsuccessfully, as with deleting from QSqlQueryModel, but it's not surprising. What should I do, what model can I use?
Upvotes: 0
Views: 148
Reputation: 179917
I think your problem is that you're trying to handle it in the model.
User input in a dialog is data which is subject to change - that's why it's presented in a dialog. You're not saving every individual digit of the phone number back to the model, when the user is editing a phone number in the view. Data, while it's being edited, belongs in the view. "Save" updates the model with the view. "Cancel" resets the view to the model.
Therefore, the list of contact changes should also be temporarily stored in the view, until it's saved.
Upvotes: 0